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 Difference between HttpSession BindingListener and HttpSession AttributeListener.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Difference between HttpSession BindingListener and HttpSession AttributeListener...." Watch "Difference between HttpSession BindingListener and HttpSession AttributeListener...." New topic
Author

Difference between HttpSession BindingListener and HttpSession AttributeListener....

Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi,

Could someone clarify the exact difference between HttpSessionBindingListener and HttpSessionAttributeListener.
I am having two listener classes each one is extending one of the above listeners. If an attribute is added to the session will both the classes are notified? because the action is equal to - value is bounding to the session and at the same time it is equal to attribute is adding to the session. Similarly in the case of attribute is removed from the session.

Thanks in Advance,


Narasimha
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You will implement HttpSessionAttributeListener when you want to know when ANY atributes are added, removed, or replaced in the session.

You will implement HttpSessionBindingListener when you have a particular class for an object that can be stored as an attribute, and you want objects of this type to be notified whhen they are bound to or removed from the session. Say you have a class called "myClass" that implements HttpSessionBindingListener, whenever myClass is added or removed from the session, the value bound and value unbound methods will be called respectively.

The experts in the forum will probably add more.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Mikalai Zaikin
Ranch Hand

Joined: Jun 04, 2002
Posts: 3233
    
    7
Bosun,

You gave a great explanation !

Just a small addition:
1) HttpSessionAttributeListener has to be registered in deployment descriptor (web.xml) in 'listener' element
2) HttpSessionBindingListener must not be registered in DD (web.xml)

regards,
MZ


Free SCDJWS 5.0 Study Guide - SCDJWS 5.0 Quiz (How to get SCDJWS 5.0 Quiz)
Java Platform, Enterprise Edition 6 Web Services Developer Certified Expert Exam Study Guide and Quiz
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi,

Thanks to Bosun, MZ for replies.
But my basic doubt is still not clarified, i.e.
" I am having two listener classes each one is extending one of the (HttpSessionBindingListener and HttpSessionAttributeListener)listeners. If an attribute is added to the session will both the classes are notified? because the action is equal to - value is bounding to the session and at the same time it is equal to attribute is adding to the session"

Thanks,
Samuel Jaeson
Greenhorn

Joined: Dec 23, 2004
Posts: 9
With Reference to Page 182 and 183 of HFJS

HttpSessionAttributeListener is for HttSessionAttributeListener Class to recieve the event and HttSessionBindingListener is for the attribute itself to know about the event.
Technically speaking
Both HttSessionBindingListener and HttpSessionAttributeListener take the same event HttpSessionBindingEvent. So in case the event occurs both should recieve it or else neither of them should recieve it.
As both parties should know.
But i doubt what happens when the attibute is replaced.
Samuel Jaeson
Greenhorn

Joined: Dec 23, 2004
Posts: 9
With Reference to Page 182 and 183 of HFJS

HttpSessionAttributeListener is for HttSessionAttributeListener Class to recieve the event and HttSessionBindingListener is for the attribute itself to know about the event.
Technically speaking
Both HttSessionBindingListener and HttpSessionAttributeListener take the same event HttpSessionBindingEvent. So in case the event occurs both should recieve it or else neither of them should recieve it.
As both parties should know.
But i doubt what happens when the attibute is replaced?

Probably in such a case ValueBound of the new attribute will be called and valueUnbound of the old attribute will be called
Jaime Yong
Greenhorn

Joined: Oct 05, 2005
Posts: 20
Hi Narasimha

But my basic doubt is still not clarified, i.e.
" I am having two listener classes each one is extending one of the (HttpSessionBindingListener and HttpSessionAttributeListener)listeners. If an attribute is added to the session will both the classes are notified? because the action is equal to - value is bounding to the session and at the same time it is equal to attribute is adding to the session"


See if I can make sense out of this :

If the attribute that added to the session is the one that implement HttpSessionBindingListener then both listeners will be notified. If the the attribute(any attribute) that is added to the session does not implement HttpSessionBindingListener then only the classes that implement HttpSessionAttributeListener is notified. By the way, please remember this:
when valueUnbound is called because an attribute is replaced and you do event.getValue(), the value returned is the old value not the new value. I have a good feeling this will be in the exam.
Rizwan Mohammad
Ranch Hand

Joined: Sep 02, 2005
Posts: 445
Hi Jaime,
I think you are correct. even in HttpSessionAttributeListener in attributeReplaced() method HttpSessionAttributeEvent also returns the oldvalue not the newvalue.


Rizwan
SCJA, SCJP, SCWCD, SCBCD, SCDJWS.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between HttpSession BindingListener and HttpSession AttributeListener....