Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference b/w HttpSessionAttributesListener and HttpSessionBindingListener

 
Iftikhar Arain
Ranch Hand
Posts: 95
Flex Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to specification.
HttpSessionBindingListener
Causes an object to be notified when it is bound to or unbound from a session.
HttpSessionAttributesListener
This listener interface can be implemented in order to get notifications of changes to the attribute lists of sessions within this web application.
What is the difference b/w
bound to or unbound from a session AND changes to the attribute lists of session
if i will use setAttribute to HttpSession object then which one will be notified.
Pls explain me properly.Thanks in advanced.
 
Mikalai Zaikin
Ranch Hand
Posts: 3371
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.
Core diffrences about these 2 listeners are:
1.
HttpSessionBindingListener is implemented by a session attribute, not special listener class.
HttpSessionAttributeListener is implemented by any Java class.
2.
HttpSessionBindingListener is not configured in the deployment descriptor.
HttpSessionAttributeListener should be configured in the deployment descriptor.
-----
During session.setAttribute() I think both of listeners will be notified. But one of the receiver will be the object itself that is added to session, and another receiver will be the standalone java class that implements session attribute listener interface and which is configured via web.xml
[ October 12, 2002: Message edited by: Mikalai Zaikin ]
 
Barry Kushnir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ara,
HttpSessionAttributeListener
----------------------------
HttpSessionAttributeListener is an interface. You code a regular java class that implements this interface supplying the three required callback methods (attributeAdded, attibuteRemoved and attributeReplaced). You then specify the name of this class in the web app deployment descriptor under the <listener> tag.
Whenever an attribute is added, replaced or removed from the HttpSession, the servlet engine will call the appropriate method on the class (attributeAdded, attributeReplace, attribute Removed).
HttpSessionBindingListener
----------------------------
HttpSessionBindingListener is also an interface. You can implement the methods of this interface (valueBound, valueUnbound) in any class, to receive notifications whenever an instance of the class is added or removed from a session. The name of the class does not get specified in the web-app deployment descriptor.
The main difference between the two is that the attribute listener listens in on a session, while the binding listener listens in on specific objects that you implement.
Hope this helps.
-BK
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To quote an older post:
Originally posted by Madhav Lakkapragada:
The bottomline is who speaks and who listens....
HttpSessionBingdingListener: Session talks and the Class (implementing HttpSessionBingdingListener) listens.

HttpSessionAttributeListener: Application talks and Session object listens.
- satya
[ February 13, 2002: Message edited by: Madhav Lakkapragada ]
 
Timber Lee
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this topic,I don't understand the difference between 'unbound' and 'remove' , 'bound' and 'add'.pleas clarify my trouble.
 
Jagdeep Singh
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the order in which they are called :
After setAttribute() method executes
1. If the new object implements HttpSessionBindingListener, the container calls HttpSessionBindingListener.valueBound.
( NOTE:IF an object was already bound to this session of this name that implements HttpSessionBindingListener, its HttpSessionBindingListener.valueUnbound method will be called. )
2. THEN container notifies any HttpSessionAttributeListeners in the web application.
 
Iftikhar Arain
Ranch Hand
Posts: 95
Flex Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for explanations.
But can any one give me some examples for both listeners.
And how to use them.
:roll:
 
shwetha bol
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here's a small example according to what i understood

Eg for HttpSessionBindingListener

eg for class implementing HttpSessionAttributeListener

Eg of the servlet where these listeners will be
invoked

In the above example, myListener should be configured in web.xml
Hope this helps,
 
Hafizur Rahman
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand in a different way. When a setAttribute is called to a session object, HttpSessionAttributeListener will be notified if specified in web.xml. If the object added as attribute implements HttpSessionBindingListener, then that object will also be notified. This is particularly useful in making sure that apporiate action and cleanup occurs before the object is discarded.
Again when an attribute is removed or replaced, the HttpSessionAttributeListener will be notified. If that object is an HttpSessionBindingListener, it will also be notified.
To say clearly, as far as I know,suppose
list1-implements HttpSessionAttributeListener
obj1- does not implement HttpSessionBindingListener
obj2-implements HttpSessionBindingListener
Now if,
1. session.setAttribute("att1",obj2)
2. session.setAttribute("att1",obj1)
3. session.removeAttribute("att1")
4. session.invalidate()
If list1 is configured in web.xml, it will be notified for every event. When a session is invalidated, every object/attribute bound to it gets unbound. [if called sequencially]The attributeAdded (case 1), attributeReplaced (case 2)and attributeRemoved (case 3,4)will be called for this listener class.
As obj1 does not implement HttpSessionBindingListener, it will never be notified.
For obj2, in case 2, the valueBound will be called. In both 3 and 4 the valueUnbound will be called.
[ November 06, 2002: Message edited by: Hafizur Rahman ]
[ November 06, 2002: Message edited by: Hafizur Rahman ]
 
shwetha bol
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might be wrong, but as far as i understand , setAttribute is the way of adding the object to any scope.Is there any other method??
 
Hafizur Rahman
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You r right, but several class/interface has the same setAttribute method. If it is called for a session object, then the HttpSessionBindingEvent will occur.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic