Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between HttpSessionBindingListener and HttpSessionAttributeListener

 
Arjun Anand
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Arjun Anand
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Done Chintan
Congratulations
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Arjun and Harpreet
 
Rohit Poddar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1072
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here Object is nothing but attribute so why we have two HttpSessionBindingListener and HttpSessionAttributeListener
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic