permaculture playing cards
The moose likes JSP and the fly likes Clean up after sessions end Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Clean up after sessions end" Watch "Clean up after sessions end" New topic

Clean up after sessions end

L. Riley

Joined: Mar 20, 2001
Posts: 28
I have the need to do some cleanup work after a session ends. In ASP (oh my!) this can be done using session.onEnd in the global.asa.
How can this be done in JSP?
mounika paruchuri

Joined: Feb 12, 2002
Posts: 26
Hi Liz,
do you want to know when the session is destroyed ,that is ends?? If that is the case then check out API documentation for HttpSessionListener interface in package javax.servlet.http
L. Riley

Joined: Mar 20, 2001
Posts: 28
Yes...I want to clean up some records in a table if the user ends the session. Do you know of another place that describes this more clearly than the API?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63873

Everything you could ever want to know: Java Servlet 2.3 Specification.

[Asking smart questions] [About Bear] [Books by Bear]
Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
You can use class which act as session listener in your application and register it in deployment descriptor (web.xml) . It extends HttpSessionListener and implements method sessionCreated(HttpSessionEvent se) and sessionDestroyed(HttpSessionEvent se).
You can put your code for clean up in method sessionDestroyed().
The web container will invoke or notify sessionDestroyed() when your application invalidates sessions or the sessions are timeout.
Hope this help...
Correct me if i am wrong...
[ April 02, 2003: Message edited by: Fisher Daniel ]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
If you make your class that contains the data a SessionBindingListener, the object will be notified when the session is destroyed and you will have the data right there. You don't have to register a SessionBindingListener.
Mehta Saryu

Joined: Dec 12, 2002
Posts: 13

Saryu Mehta
L. Riley

Joined: Mar 20, 2001
Posts: 28
No...I want to delete some records from a database if the session ends...
I agree. Here's the link:
subject: Clean up after sessions end
It's not a secret anymore!