permaculture playing cards
The moose likes Servlets and the fly likes session time out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "session time out" Watch "session time out" New topic

session time out

gas das

Joined: May 14, 2006
Posts: 5
Hi to all,
I have problems with session time out,
I'd like to know if there's a way to intercept session expire event.
In my web xml I have <session-timeout>20</session-timeout>
and it works, but I want to do call a function that use a session attribute before the session will be invalidated.
This function get userId from session and notify on database the user logout.
Do you know if I can map in my web-xml an handler,listener or something else?
Thanks a lot
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
If you keep reference to everything that needs to be cleaned up in an object that implements SessionBindingListener and is held in the session, then all of your cleanup can be done in the valueUnbound() method. This does not have to be declared in web.xml - in contrast to other types of listener.

When valueUnbound is called, you can NOT rely on any other part of the session being valid, thats why I say this object has to keep its own references to things that are used in cleanup.

Darren Edwards
Ranch Hand

Joined: Aug 17, 2005
Posts: 69
Create a class which implements the HttpSessionLIstener interface and add it to the web.xml

Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
Declare the listener in web.xml as described by Darren.
In the implementation class, put your cleanup-code in sessionDestroyed(HttpSessionEvent) method. This method is called by the container just before the session is invalidated.
gas das

Joined: May 14, 2006
Posts: 5
ok great! I used HttpSessionListener and it works. thanks to all
I agree. Here's the link:
subject: session time out
It's not a secret anymore!