File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between HttpSession BindingListener and HttpSession AttributeListener....

 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mikalai Zaikin
Ranch Hand
Posts: 3329
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jaime,
I think you are correct. even in HttpSessionAttributeListener in attributeReplaced() method HttpSessionAttributeEvent also returns the oldvalue not the newvalue.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic