This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with session.invalidate()" Watch "Problem with session.invalidate()" New topic
Author

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.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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

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


[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.


Bosun (SCJP, SCWCD)
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.
 
jQuery in Action, 2nd edition
 
subject: Problem with session.invalidate()
 
Similar Threads
How to disable cookies
Servlet User Roles
Session Tracker
problem with setContentType() method
problem in logout procedure