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

Problem with session.invalidate()

Anirban Das
Ranch Hand

Joined: Nov 22, 2003
Posts: 63
I have written a servlet to get the session details as follows :

//Import Servlet packages
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class SessionLifeCycleServlet extends HttpServlet {
protected void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {
String action = req.getParameter("action");
if(action!=null && action.equals("invalidate")) {
HttpSession session = req.getSession();
PrintWriter out = res.getWriter();
out.println("<HEAD><TITLE>Session Lifecycle</TITLE></HEAD>");
out.println("<P>Your Session has been invalidated.</p>");

String lifeCycleURL = "/session/servlet/lifeCycle";
out.print("<A HREF=\""+lifeCycleURL+"?action=newSession\">");
out.println("Create new Session</A>");
else {
HttpSession session = req.getSession(true);
PrintWriter out = res.getWriter();

out.println("<HEAD><TITLE>Session Lifecycle</TITLE></HEAD>");
out.println("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no cache\">");
out.println("<H! Session LifeCycle</CENTER></H1>");
out.print("<BR> Session Status - ");
if (session.isNew()) {
out.println("New Session.");
} else {
out.println("Old Session.");
out.print("<BR> Session Id - ");
out.print("<BR> Creation Time - ");
out.println(new Date(session.getCreationTime()));
out.print("<BR> Last Accessed Time - ");
out.println(new Date(session.getLastAccessedTime()));
out.print("<BR> Maximun Inactive Interval (seconds) - ");

String lifeCycleURL = "/session/servlet/lifeCycle";
out.print("<BR><BR><BR><BR><A HREF=\""+lifeCycleURL+"?action=invalidate\">");
out.println("Invalidate the session</A>");
out.print("<BR><A HREF=\""+lifeCycleURL+"\">");
out.println("Reload this page</A>");

Now when iam "Invalidating" the session and going to create a new session through the link provided in the "Session Invalidate notification page" it is returning as a old session for "TRUE" for the "isNew()".
Can any1 help me out?
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 31808

Where do you acually call session.invalidate()? I did a search on the page and I can't find it.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
As already posted, you are not calling the invalidate methos in the servlet you posted.

So much trouble in the world -- Bob Marley
Anirban Das
Ranch Hand

Joined: Nov 22, 2003
Posts: 63
Thannk you very much Jeanne and Bosun for pointing me out the mistake.U ppll r realy life saver.
I agree. Here's the link:
subject: Problem with session.invalidate()