Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Session expiry in servlet

 
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?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • 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.
 
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.
 
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 VI Editor Redhat
    • 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.
     
    Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic