Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking to see if my session has expired

 
david allen
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the top of my servlet I have the following code to check if the session has expired. If it has I imagine the code stops executing at the response.sendReirect() function call. But the code keeps executing an generates a null pointer error.
/*validate Session*/
HttpSession session = request.getSession(false);
if(session == null);
response.sendRedirect("login.jsp?error=session expired");
/* now i am thinking if the session has expired then the following code will not execute*/
Album album = (Album)session.getAttribute("album");
/*null pointer error generated at the next line of code when session has expired*/
out.write("Album id = " + album.getID());
The null pointer session is generated because the variable album is equal to null (becuase session has expired) and then tries to make a function call. How do I get servlet to stop execution of code at the response.sendRedirect() call.???
thanks guys
david
 
david allen
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem might be that the line of code
if(session == null)

it never returns true!
The session has expired as I have set it to one minute in web.xml. How do I test if the session has expired??
Regards david
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sendRedirect will not cause execution to stop. Even if your code did call sendRedirect, the code following will still execute. All sendRedirect does is to add headers to the response that will instruct the browser to initiate a new request to the specified URL.
bear
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have a semi-colon at the end of your if statement which stops it from ever executing
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic