• Post Reply Bookmark Topic Watch Topic
  • New Topic

HttpSessionListener interface  RSS feed

 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello in HttpSessionListener there is 2 methods: sessionCreated and sessionDestroyed. I want to know is for sessionDestroyed is it called after a session timeout or just before timeout.
Thanks
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this what javadoc said ..

sessionDestroyed

void sessionDestroyed(HttpSessionEvent se)

Notification that a session is about to be invalidated.

Parameters:
se - the notification event



So the answer i think, is "Just before the Session timeout .."
 
Mike Kaufman
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's actually a more interesting question than it seems...

The javadoc for Servlet 2.4 onwards is indeed as quoted above. However, in Servlet 2.3 it was different - it said that this method is invoked to indicate that the session "was" invalidated. That sounds far more like it's called after the invalidation.

In practice there probably isn't much you could safely use this for if it's not called until the HttpSession has already been invalidated. So the "was" might have been deliberate, or it might have been a small mistake in the wording of the javadoc, or it might not have been thought about at all and was just that way by accident. I wouldn't be suprised if the actual behaviour differed between servlet containers (whether by mistake or by deliberately choice). In Servlet 2.4 this was "clarified" (fixed? changed?) to clearly state that the method is called before the invalidation.

So I think the answer is:
- For Servlet 2.4 and above it is called before the invalidation.
- In 2.3 (where it was first introduced) it's probably supposed to be after the invalidation, but you maybe can't particularly rely on it actually being done that way.
 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I agree with Mike.

Whether its called before or after, I ultimately what to know given HttpSessionEvent event (used in sessionDestroyed) and HttpServletRequest request (used in servlet classes), should event.getSession() same as request.getSession() returning the "same" HttpSession instance? Cos the API said event.getSession() will return the session that has "changed".

If sessionDestroyed called before session invalidated, then in theory if I do event.getSession().getAttribute() should give me some data but i currently get null.
 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I finally found that it is called before and my earlier question about getAttribute is also solved... it can getAttriute.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!