Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session expiry in servlet

 
Nirmal Mekala Kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
session.removeAttribute("xxx");


What am i doing wrong? Any suggestions to handle this better?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

BTW: filters are great for this.
 
Amar Deep Singh Chaudhary
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nirmal Mekala Kumar:


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.

    Does that make sense?
     
    Nirmal Mekala Kumar
    Ranch Hand
    Posts: 63
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 :
     
    Ben Souther
    Sheriff
    Posts: 13411
    Firefox Browser Redhat VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Ranch Hand
    Posts: 63
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hmmm ... thanks Ben. That would definitely save a lot of my time future.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic