0
votes

The website I'm creating needs to access the same session attribute across multiple .jsp pages. Initially the attribute is set in my Login.java servlet, and after the first page redirection, I am able to get the correct value for the attribute however once the page is redirected again, the attribute is always null. The two relevant servlets are Login.java, where I first give the attribute a value and the Controller.java, where I navigate between the pages. My initial thoughts were that the problem was due to the way I was switching .jsp pages (response.redirect(...)) and so I modified it to use the RequestDispatcher, however that did not change the resulting error, as the CustomerObject attribute is still null in every page past the one immediately following the redirect from Login.java.

Login.java

public void doPost(HttpServletRequest request,
     HttpServletResponse response)
     throws ServletException, IOException {
  String email = request.getParameter("email");
  String password = request.getParameter("password");

  HttpSession session = request.getSession();
  Customer customer = (Customer)session.getAttribute("CustomerObject");
  if (customer == null) {
     customer = CustomerBean.login(email, password);
     if (customer != null) {
        request.getSession().setAttribute("CustomerObject", customer);
     }
  }
  response.setHeader("Cache-Control", "no-cache");
  response.setContentType("text/html");
  RequestDispatcher rd = request.getRequestDispatcher("testdynamicbuild.jsp");
  rd.forward(request, response);
}

Controller.java

public void doPost(HttpServletRequest request,
     HttpServletResponse response)
     throws ServletException, IOException {
  String forward = "";
  String temp = request.getParameter("pageLink");

  if(temp != null){
      temp = temp.toLowerCase();
      if(temp.equals("graph")){
        forward = "graphdisplay.jsp";

      }else if(temp.equals("sideMenu")){
        forward = "sidemenu.html";

      }else if(temp.equals("log")){
        forward = "logdisplay.jsp";

      }else if(temp.equals("vardisp")){      
        forward = "testdynamicbuild.jsp";

      }else if(temp.equals("home")){      
        forward = "index.html";

      }else if(temp.equals("side")){      
        forward = "sidemenu.jsp";

      }else{      
        forward = "index.html";

      }
  }else{
    forward = "errorpage.html";
  }

  response.setHeader("Cache-Control", "no-cache");
  response.setContentType("text/html");
  RequestDispatcher rd = request.getRequestDispatcher(forward);
  rd.forward(request, response);
 }

Example access from a .jsp page

<%
Customer c = null;
if( request.getSession().getAttribute("CustomerObject") != null){
        c = (Customer)request.getSession().getAttribute("CustomerObject")
}else{
    // handle null object
}
%>

UPDATE 0: I printed out the session.getId() and request.getSession(false).getId() on every page and found that, as expected, within each page, the ID's are the same but the ID's for testdynamicbuild.jsp for example were different than the ID's for graphdisplay.jsp. If my understanding is correct, this implies that a new session is created every time the page changes, even though I use request.getSession(false). Any input on this?

UPDATE 1: I added a print statement to my Controller.java to print whether or not request.getSession(false) was null and every time that Controller.java is executed, request.getSession(false) returns a null value. This would mean that the error is directly related to the session object if I'm not mistaken. Would it be the case that the session object needs to be created explicitly in, say, index.html?

UPDATE 2: When searching around a bit more, I came across a session objects tutorial found at this link and I deployed this to the server I'm working with. Upon visiting the page, it still creates a new session on every refresh. Is there anything server-side that might be causing an error such as this?

1
Can you debug and check if the setAttribute method is called, may be the login method returned a null. *Session attributes persist across a user session, navigating between pages doesn't affect it. There should be a bug somewhere in your code. - vinay
When the Login redirects to the testdynamicbuild.jsp, I am able to do session.getAttribute("CustomerObject") and have it return the correct value within the .jsp however when testdynamicbuild.jsp redirects to another .jsp, such as graphdisplay.jsp, then the session.getAttribute("CustomerObject") always returns a null. - tedris
looks like your code is creating new session when it should be using current session. Check the link stackoverflow.com/questions/11201081/… - vinay
use request.getSession(false) in your jsp scriplets. - vinay
I tested it with request.getSession(false) in all of the .jsp pages, however the same null error still occurs. Perhaps it has something to do with @bluedevil2k's answer below? - tedris

1 Answers

-1
votes

Attributes are only good for one scope, so when you do a forward/redirect, the scope is lost. Then all the Attributes get deleted, and your reference to them in the code will return null.

You'll need to store your values in the Session object, and then they will stay accessible throughout your redirects. Just don't forget to delete it from the Session (if you need to) when you access it.