Я не знаю, почему сеанс получает stackOfProducts
только один раз, а после перезагрузки страницы StackOf Products==null
, но другие объекты этой сессии boolean was
работа хорошо? шорты экрана Получить данные http://s57.radikal.ru/i155/1411/74/2922b55d6887.png Не понимайте есть данные http://s019.radikal.ru/i614/1411/d3/b4975bb3bd60.pngКогда страница загружается впервые, она получает данные для переменной, но после перезагрузки переменная равна нулю
Вот мой сервлет:
package ua.big_market;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ua.big_market.db.connecting.DataBaseConnect;
import ua.big_market.db.product.GetProductDataDB;
import ua.big_market.product.Product;
import ua.big_market.product.StackOfProducts;
@WebServlet(asyncSupported = false, name = "ServletGetProductData", urlPatterns = { "/xmlGetProductData"})
public class XmlGetProductData extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//TODO: It's must have every servlet. Because russian and Ukraine words has a UTF-8
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
Connection connection = null;
DataBaseConnect dataBaseConnect = new DataBaseConnect();
try{
connection = dataBaseConnect.getConnectToDB();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Get Product Started!");
Product product;
GetProductDataDB getProductDataDB = new GetProductDataDB();
StackOfProducts stackOfProducts = new StackOfProducts();
try {
//all work fine
LinkedList<Integer> listofId = getProductDataDB.getAllIdFromDB(connection, "computers_notebooks");
while(!listofId.isEmpty()) {
product = getProductDataDB.getProduct(connection, "computers_notebooks", listofId.getFirst());
listofId.removeFirst();
stackOfProducts.addProduct(product);
}
} catch (SQLException e) {
e.printStackTrace();
}
boolean was = true;
request.getSession().setAttribute("stackOfProducts", stackOfProducts);
request.getSession().setAttribute("was", was);
// work fine
response.sendRedirect("computers-notebooks.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
public void init(ServletConfig config) throws ServletException {
// this.context = config.getServletContext();
}
}
Вот мой HTML:
<%
boolean was = false;
try {
was = (Boolean) session.getAttribute("was");
} catch (Exception e) {
was = false;
}
if (!was)
response.sendRedirect("xmlGetProductData");
StackOfProducts stackOfProducts = (StackOfProducts) session.getAttribute("stackOfProducts"); **// at first i have a getting data there but after reload i have null**
StackOfProducts stackOfProducts2 = null;
if (stackOfProducts != null && !stackOfProducts.isEmpty()) {
stackOfProducts2 = new StackOfProducts();
stackOfProducts2 = stackOfProducts;
}
// stackOfProducts = stackOfProducts2;
Product product = new Product();
if (stackOfProducts2 != null) {
while (!stackOfProducts2.isEmpty()) {
product = stackOfProducts2.getNext();
%>
<%=product.getProductName()%> **//only once i'm there**
<%
stackOfProducts2.removeCurrent();
}
} else {
int i = 0;
%>
123456789 **//and after reload i'm always here**
<%
}
%>
Слишком много кода, пожалуйста, вставьте только то, что релевантно. – user3791372
теперь лучше? @ user3791372 –
Все время вы игнорируете исключение, поэтому вы установили пустой список, который затем «null» в JSP. Если у вас напечатан стек, то было бы неплохо прикрепить его к сообщению. –