Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to know when a session is removed?

 
Andreas Johansson
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Andreas Johansson
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic