-1
我不知道爲什麼會話只獲得stackOfProducts
一次,並且在頁面重新加載之後StackOf Products==null
但此會話的另一個對象像boolean was
工作正常嗎? 屏幕短褲 獲取數據 http://s57.radikal.ru/i155/1411/74/2922b55d6887.png 不要有數據 http://s019.radikal.ru/i614/1411/d3/b4975bb3bd60.png第一次加載頁面時,它獲取變量的數據,但重新加載後變量爲空
這裏是我的servlet:
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 2014-11-22 20:09:12
現在好點? @ user3791372 – 2014-11-22 20:17:43
你一直在忽略異常,因此你已經設置了一個空的列表,然後在JSP中'null'。如果你有一個堆棧跟蹤打印,那麼將它貼在帖子上會很好。 – 2014-11-22 20:18:23