aspose file tools*
The moose likes Servlets and the fly likes problem with servlet listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem with servlet listeners" Watch "problem with servlet listeners" New topic
Author

problem with servlet listeners

Gorby Green
Greenhorn

Joined: Nov 23, 2002
Posts: 12
Hi!
I have some problem with servlet listerner.
When a user exit I want to save the user object stored in HTTPSession.
And I have read that sessionDestroyed is called when the session realy is destroyed.
But i want to save the context in user object just before session is destroyed.

Is there any chance to do that with servlet listeners?
Thanks in advance!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Under Servlets 2.4 (Tomcat 5) the listener is called just before the session is destroyed.
[ January 19, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Not Tomcat specific.
Moving to Servlets.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Gorby Green
Greenhorn

Joined: Nov 23, 2002
Posts: 12
Hi!
I would like to save MyObject before session is destroyed.
But sessionDestroyed() is called just after the session is destroyed.
Is there any listener where i can save MyObject before it is removed from session?

public class HttpListner implements HttpSessionListener{

public void sessionCreated(HttpSessionEvent se) {
System.out.println("HttpListner sessionCreated");
}

public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = arg0.getSession();
MyObject object = (MyObject) session.getAttribute("myObject");
System.out.println("name:" + myObject.getName());
}
}
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
It looks like a bug in a servlet container you are using:
For example I use session listener for saving user data, like:

I use a servlet container different than Tomcat.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Since you started this thread in Tomcat, I'll assume that that's your container.
What version of Tomcat are you using?

As Bear mentioned, Servlet Spec 2.4 requires that the sessionDestroyed method be called before the session expires. This was not the case in earlier servlet spec releases.

From:
javax.servlet.http.HttpSessionEvent&#41;" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSessionListener.html#sessionDestroyed(javax.servlet.http.HttpSessionEvent)



From:
javax.servlet.http.HttpSessionEvent&#41;" target="_blank" rel="nofollow">http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSessionListener.html#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
Gorby Green
Greenhorn

Joined: Nov 23, 2002
Posts: 12
Thanks!
I worked with the wrong version.
Thanks for all help.
/Gorby
Antonio Giovanni
Ranch Hand

Joined: Jun 27, 2005
Posts: 85
Ok,.. is there any work around way, of doing what you do in

public void sessionDestroyed(HttpSessionEvent se) of
HttpSessionListener from J2ee 1.4 specs

in J2ee 1.3 specs.
because in j2ee 1.3 the session is invalidated before HttpSessionListener is invoked. I really do not understand the need for having sessionDestroyed(HttpSessionEvent se) in J2ee 1.3

Thanks for the ideas
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Antonio Giovanni:
Ok,.. is there any work around way, of doing what you do in

public void sessionDestroyed(HttpSessionEvent se) of
HttpSessionListener from J2ee 1.4 specs

in J2ee 1.3 specs.



I guess it would depend on what you want to use it to do.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with servlet listeners