This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Difference between HttpSessionBindingListener and HttpSessionAttributeListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Difference between HttpSessionBindingListener and HttpSessionAttributeListener" Watch "Difference between HttpSessionBindingListener and HttpSessionAttributeListener" New topic
Author

Difference between HttpSessionBindingListener and HttpSessionAttributeListener

Arjun Anand
Greenhorn

Joined: May 24, 2001
Posts: 25
What is the difference between HttpSessionBindingListener and HttpSessionAttributeListener other than that Attributelistener is invoked for adding,replacing and removing objects in session and Bindinglistener is invoked when an object is bound (adding) or unbound (removing) from a session. I dont understand the reason of having 2 Listener classes serving the same purpose.
Clarifications please.


Arjun
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
From what I understand...........

AttributeListner notifies the application of changes to its Attribute list.
BindingListner notifies the Object that it is being tracked (bound or otherwise) by an application.
Doesn't look like they are the same.
Any other intrepretations are welcome....
regds.
- satya


HttpSessionAttributeListener:
This listener interface can be implemented in order to get notifications of changes to the attribute lists of sessions within this web application.
HttpSessionBindingListener:
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.


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
Good question:
HttpSessionBindingListener:
If an object implements HttpSessionBindingListener, it is notified when it is bound to or unbound from a session. For example,
MyObject implements HttpSessionBindingListener
{
// class definition
}
If I call
session.setAttribute ("Object", MyObject)
or
session.removeute ("Object", MyObject)
and so on, methods valueBound and/or valueUnbound (defined in HttpSessionBindingListener, implemented in MyObject are called)
HttpSessionAttributeListener:
When any class implements HttpSessionAttributeListener interface it is notified when any change in the attribute list of session occurs. For example
MyClass implements HttpSessionAttributeListener
{
// implementations of methods
}
session.setAttribute ("anything", AnyObjectNotOnlyMyClass);
or
session.removeAttribute ("anything", AnyObjectNotOnlyMyClass);
indicates change in the list of attributes and hence appropriate method is called
DIFFERENCES:
Implementing HttpSessionBindingListener works only for the object that implements it whereas implementing HttpSessionAttributeListener listens to any attribute added, removed or replaced.
Hope this helps. This is important. Let me know if this is not clear, I will explain it again with simple words. I had questions from these topics.
Chintan
[ January 04, 2002: Message edited by: Chintan Rajyaguru ]

ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
Arjun Anand
Greenhorn

Joined: May 24, 2001
Posts: 25
Thanks Madhav and Chintan for your prompt replies.
And Congratulations Chintan on becoming a SCWCD. Way to go pal.
I know the working of HttpSessionBindingListener and HttpSessionAttributeListener. But my question is:
Why should I need a HttpSessionBindingListener to notify the object. Instead I can use a class that implements HttpSessionAttributeListener and find out the object that was affected from the event object and then take appropriate action on the affected object. Sort of a controller class listening for all the events and dispatching it to the respective objects.
Hope I was clear in my question.
Any comments
Originally posted by Chintan Rajyaguru:
Good question:
HttpSessionBindingListener:
If an object implements HttpSessionBindingListener, it is notified when it is bound to or unbound from a session. For example,
MyObject implements HttpSessionBindingListener
{
// class definition
}
If I call
session.setAttribute ("Object", MyObject)
or
session.removeute ("Object", MyObject)
and so on, methods valueBound and/or valueUnbound (defined in HttpSessionBindingListener, implemented in MyObject are called)
HttpSessionAttributeListener:
When any class implements HttpSessionAttributeListener interface it is notified when any change in the attribute list of session occurs. For example
MyClass implements HttpSessionAttributeListener
{
// implementations of methods
}
session.setAttribute ("anything", AnyObjectNotOnlyMyClass);
or
session.removeAttribute ("anything", AnyObjectNotOnlyMyClass);
indicates change in the list of attributes and hence appropriate method is called
DIFFERENCES:
Implementing HttpSessionBindingListener works only for the object that implements it whereas implementing HttpSessionAttributeListener listens to any attribute added, removed or replaced.
Hope this helps. This is important. Let me know if this is not clear, I will explain it again with simple words. I had questions from these topics.
Chintan
[ January 04, 2002: Message edited by: Chintan Rajyaguru ]
Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
Well Done Chintan
Congratulations
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
Oh! Now I understand your question. I cannot think of many practical situations where you would need HttpSessionBindingListener over HttpSessionAttributeListener. However, if you wanted to do some internal processing such as calling private methods to do specific stuff (this is not a strong justification though), you would not be able to do so from the class implementing HttpSessionAttributeListener.
I would love to see more comments on this.
Chintan
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
Thanks Arjun and Harpreet
Rohit Poddar
Ranch Hand

Joined: Aug 18, 2001
Posts: 36
Let me try to give an argument why do we have these two listener and let's see if I succeed
HttpSessionBindingListener was there before Servlet 2.3 came out. And HttpSessionAttributeListener, which serves a broader purpose was added in 2.3. And now to maintain backward compatibility Sun must have left HttpSessionBindingListener in there.
Any takers ?
[ January 10, 2002: Message edited by: Rohit Poddar ]

SCJP, SCWCD
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
FWIW - On Tomcat 4.0.1, the sequence of calls made to these objects is:
BindingListener
First (valueBound/valueUnbound)
AttributeListener
Second(attributeAdded/attributeRemoved)
Is that sequence specified? I don't know.
Regards, Guy
Arjun Anand
Greenhorn

Joined: May 24, 2001
Posts: 25
Originally posted by Rohit Poddar:
Let me try to give an argument why do we have these two listener and let's see if I succeed
HttpSessionBindingListener was there before Servlet 2.3 came out. And HttpSessionAttributeListener, which serves a broader purpose was added in 2.3. And now to maintain backward compatibility Sun must have left HttpSessionBindingListener in there.
Any takers ?
[ January 10, 2002: Message edited by: Rohit Poddar ]

Then Sun would have deprecated the listener, wouldn't they?
Rohit Poddar
Ranch Hand

Joined: Aug 18, 2001
Posts: 36
I thought a little more about it. Although you can achieve the same purpose of taking some action on the object being bound/ unbound from attribute listener but that doesn’t seem to be such a clean solution. I would rather leave the code localized in the class for the object being bound/ unbound.
If you want to do it from attribute listener then either you would have to find out what type of object is being bound/ unbound (using instanceof, which doesn’t seem so elegant) or all your objects’ classes will have to implement a common interface (which is an unnecessary step for all those classes which to not need any action on being bound/ unbound).
-Rohit
ersin eser
Ranch Hand

Joined: Feb 22, 2001
Posts: 1072
I guess this separation might help while decoupling the dependencies ? I think you can make your pool class implement binding listener and let it take care of the business ( maintanence, reclaims etc by itself ) Looks like in attributeListener you are going to need lots of instanceof checks thus too many dependencies might be created.
Any input ?
[ January 11, 2002: Message edited by: ersin eser ]
Rohit Poddar
Ranch Hand

Joined: Aug 18, 2001
Posts: 36
Originally posted by ersin eser:
I guess this separation might help while decoupling the dependencies ? I think you can make you pool class implement binding listener and let it take care of the business ( maintanence, reclaims etc by itself ) Looks like in attributeListener you are going to need lots of instanceof checks thus way too much dependencies might be created.
Any input ?
[ January 11, 2002: Message edited by: ersin eser ]


Yes, I agree with you Ersin.
sandhya ch
Greenhorn

Joined: Oct 04, 2009
Posts: 5
Here Object is nothing but attribute so why we have two HttpSessionBindingListener and HttpSessionAttributeListener
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between HttpSessionBindingListener and HttpSessionAttributeListener
 
Similar Threads
AttributeListeners VS BindingListeners
Difference between HttpSession BindingListener and HttpSession AttributeListener....
need help on question on sessions
Quest About Session Manger
Listener Question