I have a web application in which most of the things are handled in JSP pages. The user logs in from JSP page. The session expires in some time. I have a JSP page from where I submit the request to a servlet. When I keep the JSP page open till session expires and then click submit, the request is submitted to the servlet. How do I handle the session expiry in the servlet. Currently i'm doing this
My understanding: if session has expired it should redirect to the page. Current Behviour: Application throws NullPointerException at line
What am i doing wrong? Any suggestions to handle this better?
Nirmal Kumar<br />If you have to aim, aim for the moon. Even if you miss you will land among the stars
I never trust the session.isNew or or getSession(true). Any hit to a JSP before this test is made will skew the results. Rather, I test for the existence of an object bound to session.
Upon a successful login, bind an object to session ("userBean" for instance). Then, for each request requiring a valid login, test to see if session.getAttribute("userBean") returns null. If so, redirect to the login page, if not, you know they are currently logged in.
This is good idea to bind an attribute to the session after getting successfull login by using the session.set Attribute method after that when the user ask any thing to server every time checks for that binded attribute by using the session.getAttribute method if the user is same then give the service to the user otherwise redirect the user to the session expires page or login page.you can also expires the session by calling the session.Invalidate() method.
I don't think this is a session problem at all. It's just a slight misunderstaning about the servlet API. What happens is:
you check if session is null
if it is null, you call sendRedirect.
when the call to sendRedirect returns, it drops through to the code following the "if".
following the "if" you (try to) call a method on "session", which might be null.
The simple solution is to put the session.removeAttribute("xxx"); in an else:
The long-term solution is to remember that sendRedirect is not magical, it's just a method call which returns like any other. The actual redirection only happens when the HTTP response is sent back to the browser. All that the sendRedirect call does is set some response headers.
Thanks everyone. Franks reply was especially helpful. I always thought that response.sendRedirect() takes away the control to redirected page like a RequestDispatcher.forward(). but that that doesnt seem to be the case. I figured that out the hard way though :
Originally posted by Nirmal Mekala Kumar: Thanks everyone. Franks reply was especially helpful. I always thought that response.sendRedirect() takes away the control to redirected page like a RequestDispatcher.forward(). but that that doesnt seem to be the case. I figured that out the hard way though :
RequestDispatcher.forward does not take control away either. Hey, you learned this one the easy way!
Nirmal Mekala Kumar
Joined: Aug 27, 2005
Hmmm ... thanks Ben. That would definitely save a lot of my time future.