Session is Null when using response.sendRedirect method
843841Apr 23 2003 — edited Sep 15 2008Hello,
I am having a weird situation... weird because it does not happen all the time. I have a Servlet that redirects to another Servlet, but in the second servlet, when I get the sesion using request.getSession(), the session object is null. I know that my session is not expiring (I set my session timeout to 30 minutes!), hence cannot figure out why the session object is Null? Shouldn't the method request.getSession() return the current session associated with this request, or if the request does not have a session, create one, but instead I am getting a NullPointerException, since its returning a Null for the session. Of course, I need to check that the session returned is NOT NULL< but still why should the request return a Null session?
I read somewhere that using response.sendRedirect(response.encodeRedirectURL(theServletURL)) will solve the problem of losing the session. Is this true?
I am unable to recreate the error all the time, and hence cannot be sure if the session is being lost because of the sendRedirect() method!
Please clarify if this is the real behaviour of response.sendRedirect() and if I should use RequestDispatcher.forward() instead!
Thanks in advance