File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to know when a session is removed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to know when a session is removed?" Watch "How to know when a session is removed?" New topic
Author

How to know when a session is removed?

Andreas Johansson
Ranch Hand

Joined: Feb 05, 2001
Posts: 43
Hi!
I have a servlet that talks with my server to get the pages to show. For the server to know how to generate the pages from the database it stores one data object for every session.
But how do I know when the sessions end? (either when the user logs off, are inactive for 30min or closes the browser). I then want to remove the object on the server and also "log off" the user.
Can I get an event or is some method automatically called when an session ends?
I use servlets with tea and tomcat.
I hope you can help me soon!!
/Andreas
Frances Teng
Greenhorn

Joined: Oct 29, 2001
Posts: 9
This might help:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class SessionTracker
implements HttpSessionBindingListener
{
public void valueBound(HttpSessionBindingEvent event)
{
System.out.println("ADDED session " + event.getSession().getId() );
}

public void valueUnbound(HttpSessionBindingEvent event)
{
String sqlStatement = new String("delete from session_data ");
sqlStatement += " where session_id = '" + event.getSession().getId() + "' " ;
super.sendCommand(sqlStatement);
super.sendCommand("commit");
System.out.println("REMOVED session " + event.getSession().getId() );
}
}

Originally posted by Andreas Johansson:
Hi!
I have a servlet that talks with my server to get the pages to show. For the server to know how to generate the pages from the database it stores one data object for every session.
But how do I know when the sessions end? (either when the user logs off, are inactive for 30min or closes the browser). I then want to remove the object on the server and also "log off" the user.
Can I get an event or is some method automatically called when an session ends?
I use servlets with tea and tomcat.
I hope you can help me soon!!
/Andreas

Frances Teng
Greenhorn

Joined: Oct 29, 2001
Posts: 9
You can add your session listener to you session
HttpSession session = request.getSession(false);
//...
if (session.getValue("SESSION_TRACKER") == null)
{
session.putValue("SESSION_TRACKER",new SessionTracker());
}

or you can directly add your object to the session
session.putValue("NAME",myObj);
This way you don't have to worry about listning to the HttpSessionBindingEvent
Frances Teng
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

This thread might be of some help too:
http://www.javaranch.com/ubb/Forum7/HTML/006983.html
------------------
- Jessica Bradley
HP Bluestone
[This message has been edited by Jessica Bradley (edited December 12, 2001).]


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Andreas Johansson
Ranch Hand

Joined: Feb 05, 2001
Posts: 43
Just wanted to thank for your replies! =)
We will change to Tomcat 4.0 which supports the Servlet 2.3 API. And in that they seem to have a new listener "HttpSessionListener" which have a sessionDestroyed() method! Very nice...
But thanks again!
/Andreas
jawwad ahmed
Ranch Hand

Joined: Dec 01, 2001
Posts: 179
Well there is interface named
HttpSessionAttributeListener in servlet 2.3
and has following methods by which imlementing this u can know many things.
public void attributeAdded(HttpSessionBindingEvent ev);
public void attributeRemoved(HttpSessionBindingEvent ev);
public void attributeReplaced(HttpSessionBindingEvent ev);
remember by useing this u should make changes in web.xml file.
<listener>
<listener-class>implemented class name<listener-class>
</listener>
Value bound and unbound is used when object that is value is added or removed while HttpSessionAttributeListener is used when changes take place in the key.As all attributes are maintaining hashtable implicitly.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to know when a session is removed?
 
Similar Threads
How to set JSESSIONID's maxAge?
Session Tracking
Design Issue(Urgent)
Simple login/logout tutorial for Tapestry?
Allowing a Session to stay alive when browser closes