wood burning stoves 2.0*
The moose likes Servlets and the fly likes Session expiry in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session expiry in servlet" Watch "Session expiry in servlet" New topic
Author

Session expiry in servlet

Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
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?


Nirmal Kumar<br />If you have to aim, aim for the moon. Even if you miss you will land among the stars
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Amar Deep Singh Chaudhary
Ranch Hand

Joined: Dec 26, 2006
Posts: 49
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.


"A thinking mind never dies"
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
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?


    Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
    Nirmal Mekala Kumar
    Ranch Hand

    Joined: Aug 27, 2005
    Posts: 63
    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

    Joined: Dec 11, 2004
    Posts: 13410

    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

    Joined: Aug 27, 2005
    Posts: 63
    Hmmm ... thanks Ben. That would definitely save a lot of my time future.
     
     
    subject: Session expiry in servlet
     
    Similar Threads
    Handle Session Expire in JSP
    Session problem or the browser problem?
    Automatic Redirect to Login page on Session Expire
    Session Handling
    Is my code running twice? JSP, java beans, db updates - strange happenings