Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Listener question

 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class implementing HttpSessionAttributeListener will be notified when an attribute is removed from the session. This is done by the following method call
public void attributeRemoved (HttpSessionBindingEvent se)
QUESTION: What happens when the session times out or invalidated? Is this method called?
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone?!
 
Ram Dhan Yadav K
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chintan,
Real good question!
Even i am not sure, i tried to search documentations, but could not find it. probably should test explicity.
Ramdhan Yadav K
 
Vaishali Joshi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the API, if the attribute implement HttpSessionBindingListener, valueUnbound will be called if the session is invalidated.
The API does not say this for HttpSessionAttributeListener. So I believe, it's methods would not be called if the session is invalidated.
However, a quick program in Tomcat4.0 showed that even attributeRemoved is called when the session is invalidated.
Any comments?
 
Ram Dhan Yadav K
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishali,
thats a good feedback. right now i am on holiday and so don't have access to app. server can you please check the phenomenon and confirm it!
thanks,
Ramdhan Yadav K
 
jyothi ve
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried small example by using HttpSessionAttributeListener with Tomcat 4.0.
attributeRemoved method called just before session timed out(i.e session invalidated)
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think when the session is invalidated (due to time out or anything) all the attributes are unbound from it. This indicates a change of list of attributes in the session and hence the method is called.
Conclusion: I agree with all of you now.
 
Vaishali Joshi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what's the answer??? According to the API, there is no reason to call the attributeRemoved() method when the session is invalidated. But tomcat calls it. So either Tomcat is wrong or the API is wrong!!!
 
jyothi ve
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vaishali Joshi,
HttpSessionBindingListener's valueBound & valueUnbound method called only objects/beans which implementing above interface(i.e HttpSessionBindingListener), when you bound/unbound them to the session.
where as the HttpSessionAttributeListener methods attributeAdded, arttributeRemoved methods called when you add any object(no need to implement HttpSessionAttributeListener ) to session or remove from session(by explicit or due to session invalidate) .
I hope this helps ...
 
Vaishali Joshi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing that up, Jyothi. But that's not what the question is about. The question is whether attributeRemoved() of HttpSessionAttributeListener should be called at all if the session is invalidate. Although, Tomcat calls it but the API does not say so (like it says so clearly for HttpSessionBindingListener).
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what API says
"Notification that an attribute has been removed from a session. Called after the attribute is removed."
Now I think both API and Tomcat are correct because the above statement from API does not mean in anyway that this method should NOT be called when session is invalidated. Logically speaking, when the session is invalidated, all the attributes are autometically removed (you may want to disagree with that because this has not been explicitly mentioned anywhere ) and hence the method is called.
In one of the test simulator that I am using, (I think it is SCWCD@Whiz) there was a question something like this:
"Which of the following methods will be called when session times out? Select two answers."
I selected sessionDestroyed of SessionListener and attributeRemoved of SessionAttributeListener and my answer was correct (again, we are believing a test simulator)
So, it seems there is not clear answer althaugh it makes sense to believe that when a session is invalidated attributes are removed (hence the method call). May be I will try putting this question on Sun's website or Javaworld's Q&A.
 
Vaishali Joshi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think putting this up on the Sun's site would be a good idea.

The cause of confusion is that in the Servlet 2.3 Specification, the description for HttpSessionBindingListener excplitily says, "Causes an object to be notified when it is bound to or unbound from a session.
The object is notified by an HttpSessionBindingEvent object.This may be as a
result of a servlet programmer explicitly unbinding an attribute from a session,
due to a session being invalidated, or due to a session timing out.".
But it does not say so for HttpSessionAttributeListener.
Please do let us know what they say.
 
Ram Dhan Yadav K
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Chintans arguement. Logically and realistically speaking it should be the case. Chintan please let us know the answers you get from the other forums!
thanks,
Ramdhan YK
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The forum on Sun does not seem to be as good as forum on Javaranch. No answer yet
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vaishali Joshi:
I think putting this up on the Sun's site would be a good idea.

The cause of confusion is that in the Servlet 2.3 Specification, the description for HttpSessionBindingListener excplitily says, "Causes an object to be notified when it is bound to or unbound from a session.
The object is notified by an HttpSessionBindingEvent object.This may be as a
result of a servlet programmer explicitly unbinding an attribute from a session,
due to a session being invalidated, or due to a session timing out.".
But it does not say so for HttpSessionAttributeListener.
Please do let us know what they say.

as per API setAttribute(param,null) results in removal of attribute.
I think they have implemented "attributeRemoved" method like this only.
So I think when session is destroyed then they unbound object from session by setting param to null, thus it results in call to attributeRemoved().
CMIW
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic