File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Question about HttpSessionActivationListener implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Question about HttpSessionActivationListener implementation" Watch "Question about HttpSessionActivationListener implementation" New topic

Question about HttpSessionActivationListener implementation

Christopher Elkins
Ranch Hand

Joined: Oct 26, 2004
Posts: 45
I need to track when a users session is about to timeout. After searching through the topics in "The Ranch" and doing a little reading on my own it appears that my best bet is to implement HttpSessionActivationListener. I can then perform my cleanup in the sessionWillPassivate() method. My understanding is that this will serve a double purpose by being called not only when the session is about to timeout but also on a call to session.invalidate(). I know that when you use an HttpSessionListener you have to register it with the web.xml. Is this true of the ActivationListener as well? If not, how do the servlets recognize where to send the event?
vasu venki

Joined: Oct 05, 2005
Posts: 19
Well good Question christoper

U know HttpSession migrate from one jvm to another,so the spec designers design an listener to find whether the attributes in the HttpSession were also about to move.So there came a HttpSessionActivationListener.

If all ur attributes r straight forward serializable then u don't need to care about this listener.

So ur question is about ur using HttpSessionActivation listener,and how do we register it.right.My opinion is for any listener we need to register the class in web.xml.I hope this might me true.

SCJP:70%<br />SCWCD:86%<br />SCBCD:90%<br />scdjws:98%<br />OCA<br />Nothing is impossible.Try and try until you achieve it.<br /> <br />Interstesigning and developing web Components
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

Two things "vasugivenkatesh vasugivenkatesh",

Firstly, JavaRanch is a community of people from all over the world, many of who are not native English speakers. While using abbreviations like "u" instead of spelling out "you" is convenient when text messaging your friends on a cell phone or in a chat room, it presents an extra challenge to those that are already struggling with English. Additionally, such shortcuts may confound automated translation tools that patrons of the Ranch may be making use of.

I would like to ask for your help in making the content of JavaRanch a little easier to read for everybody that visits here by not using such abbreviations.

Please read this for more information.

And secondly...

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Forum Bartender

[Asking smart questions] [About Bear] [Books by Bear]
Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121

To track session invalidation and time-out, use HttpSessionListener, in which sessionDestroyed() is called when the session is about to be destroyed.

HttpSessionActivationListener's are notified of session migration across JVM's, not time-outs and invalidations.

HttpSessionActivationListener is implemented by attributes that are bound to a Session and not configured in web.xml

keep smilin :: sravan<br /><a href="" target="_blank" rel="nofollow">I scribble here</a>
Christopher Elkins
Ranch Hand

Joined: Oct 26, 2004
Posts: 45
Alright, I'm implementing HttpSessionListener. I've declared my class in web.xml and set the session to timeout in 5 minutes for testing purposes. When the session times out I get the following exception thrown to the console. The web page seems unaffected.

The process that I followed was to log in to the application so that my session is loaded with the appropriate objects and then sit on that web page until session timeout is reached. Also, it doesn't appear that the code to finalize session objects I placed in sessionDestroyed() is reached. It enters the method and then...

Any help? Thanks guys (and gals).
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Can you tell us what container and version you are using?
Also, if you know what version of the Servlet Spec your container supports, that would be helpful.

I know there was a change in the J2EE API (between 1.3 and 1.4) in the way (when) the sessionDestroyed method is called.
In a nutshell sessionDestroyed used to be called after the session was destroyed and now get's called before.

This thread has links to the two APIs and discusses their differences.

[added: you might actually want the link]
[ October 06, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Christopher Elkins
Ranch Hand

Joined: Oct 26, 2004
Posts: 45
I'm using the version 1.4.2 Java APIs running against a SunOne Web Server (6.1) and a BEA WebLogic Application Server (8.1).

Basically the problem is that some of our users can run reports in the background. What I need to do is check to make sure that the reports stop if the session times out. They run in a seperate thread that's referenced from an object stored in the session. That's the finalization code I spoke of previously which isn't being reached. Any help would be tremendously appreciated. In the meantime I'll look at those links. Thanks.
I agree. Here's the link:
subject: Question about HttpSessionActivationListener implementation
It's not a secret anymore!