File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Listener question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Listener question" Watch "Listener question" New topic
Author

Listener question

Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
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?


ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
anyone?!
Ram Dhan Yadav K
Ranch Hand

Joined: Aug 13, 2001
Posts: 321
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


Ram Dhan Yadav (SCJP, SCWCD, SCJA-I, IBM EC(483))
"We are what we repeatedly do. Excellence, then, is not an act, but a habit."
Vaishali Joshi
Greenhorn

Joined: Dec 28, 2001
Posts: 21
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

Joined: Aug 13, 2001
Posts: 321
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

Joined: Aug 03, 2001
Posts: 51
I tried small example by using HttpSessionAttributeListener with Tomcat 4.0.
attributeRemoved method called just before session timed out(i.e session invalidated)


Jyothi<br /> <br />Sun Certified Business Component Developer<br />Sun Certified Web Component Developer<br />Sun Certified Java Programmer<br />Oracle Certified SQL & PL-SQL Programmer
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
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

Joined: Dec 28, 2001
Posts: 21
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

Joined: Aug 03, 2001
Posts: 51
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

Joined: Dec 28, 2001
Posts: 21
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

Joined: Aug 19, 2001
Posts: 341
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

Joined: Dec 28, 2001
Posts: 21
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

Joined: Aug 13, 2001
Posts: 321
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

Joined: Aug 19, 2001
Posts: 341
The forum on Sun does not seem to be as good as forum on Javaranch. No answer yet
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
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


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Listener question