• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with session.invalidate()

 
Anirban Das
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a servlet to get the session details as follows :

//Import Servlet packages
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
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();
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Session Lifecycle</TITLE></HEAD>");
out.println("<BODY>");
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>");
out.println("</BODY></HTML>");
}
else {
HttpSession session = req.getSession(true);
res.setContentType("text/html");
PrintWriter out = res.getWriter();

out.println("<HTML>");
out.println("<HEAD><TITLE>Session Lifecycle</TITLE></HEAD>");
out.println("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no cache\">");
out.println("<BODY>");
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.println(session.getId());
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) - ");
out.println(session.getMaxInactiveInterval());

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>");
out.println("</BODY></HTML>");
out.close();
}
}
}
***************************************************************************

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
Marshal
Posts: 34214
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you acually call session.invalidate()? I did a search on the page and I can't find it.
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As already posted, you are not calling the invalidate methos in the servlet you posted.
 
Anirban Das
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thannk you very much Jeanne and Bosun for pointing me out the mistake.U ppll r realy life saver.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic