File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Checking to see if my session has expired Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Checking to see if my session has expired" Watch "Checking to see if my session has expired" New topic

Checking to see if my session has expired

david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
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 allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
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

Joined: Jan 10, 2002
Posts: 63839

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.

[Asking smart questions] [About Bear] [Books by Bear]
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
you have a semi-colon at the end of your if statement which stops it from ever executing

Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
I agree. Here's the link:
subject: Checking to see if my session has expired
It's not a secret anymore!