This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
Joined: Sep 27, 2002
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
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