首次加载页面时,它会获取变量的数据,但重新加载后该变量为null

时间:2014-11-22 19:37:37

标签: java html jsp

我不知道为什么会话只获得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**
                    <%  
                        }
                    %>

1 个答案:

答案 0 :(得分:0)

问题在于您重定向was时检查的变量xmlGetProductData。之后,您应该将其设置为falsenull,就像处于初始状态一样。

if (!was) 
  response.sendRedirect("xmlGetProductData");
request.getSession().setAttribute("was", false);
相关问题