Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpSessionAttributeListener and HttpSessionBindingListener

 
riohk kurn
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the session of attribute that implements HttpSessionListener is destoryed, what will happen? (provided that it is not included in descriptor)
As, I am not sure whether descripting them in web.xml is a must.
Moreover, OK, suppose all required listeners have been defined in web.xml. What are the differences between attribute object implemented HttpSessionAttributeListener and HttpSessionBindingListener regardless the correct using way.
 
Rama Raghavan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the session of attribute that implements HttpSessionListener is destoryed, what will happen? (provided that it is not included in descriptor)

1.To handle events raised when adding/removing attributes to/from HttpSession, the web application must provide a class that implements the necessary interface. The class must be placed appropriately so as to be available for the webapp.
The handler class has nothing to do with attribute being bound to session.
If you are asking what happens when the session is destroyed, and there is a listener implementation provided, then appropriate methods will be invoked on the implementation.

As, I am not sure whether descripting them in web.xml is a must.

2. You have to provide a <listener> descriptor entry in order for the container to listen for events, and invoke appropriate methods. If no listener element exists in web.xml, then the app server will ignore (all) such events.

What are the differences between attribute object implemented HttpSessionAttributeListener and HttpSessionBindingListener

3. The only differences that I see are:
a. HttpSessionAttributesListener is 2.3 spec while HttpSessionBindingListerner has been since servlet 2.2.
b. When an attribute is replaced, valueUnbound() is called, but NOT valueBound().
Don't know if this is a bug in my app server or the spec.
c. HttpSessionAttributeListener interface has more meaningful method names.
Rama
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to think of it is
HttpSessionAttributeListener pays attention to what is happening to the HttpSession as a whole, independent of what type of object is being placed into it.
HttpSessionBindingListener pays attention to a specific type of object, and is notified when that object type is added or removed from a session.
Also, HttpSessionBindingListeners aren't defined in the deployment descriptor, since it's the specific object class that is handling the events, and not the web app.
[ November 20, 2002: Message edited by: jason adam ]
 
friso jonge
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, considering the above, what is the answer to the following question:
which of the following statements is/are true ?
A) the listener tag is used to define all context and session listeners
b) the listener interface name must be defined within the deployment descr.
c) The httpsessionActivationListener must be defined withint he originating server only
d) the listener-name tag is used to define the listener class
guess what my book says....
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by friso jonge:
ok, considering the above, what is the answer to the following question:
which of the following statements is/are true ?
A) the listener tag is used to define all context and session listeners

This is true, considering the perspective. Since HttpSessionBindingListener doesn't actually listen to a session, then it isn't really considered a session listener, it instead listens to the object that's being put into the session.
So yes, listeners of the session and context must be in your web.xml, but not ALL listeners are defined there.

b) the listener interface name must be defined within the deployment descr.

False, the class that <b>implements</b> the listener must be defined.

c) The httpsessionActivationListener must be defined withint he originating server only

False. This interface is defined on the objects that are being put into a session, and therefore isn't tied to the server.

d) the listener-name tag is used to define the listener class

False, the listener-class tag defines this.

guess what my book says....

Well, I ain't Dion Warwick, so reading minds isn't my forte, but I'm thinking it's A.
 
Maha Annadurai
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding Rama's question , I made a test with Tomcat 4.0, for HttpSessionAttributeListener.
I had my sevlet code like below:
session.setAttrubute("name" , obj1);
session.setAttrubute("name" , obj2);
When I replace the attribute "name", the 'valueUnbound(event)' on obj1 is called first and then valueBound(event) of obj2 is called.
Just an update.
Regards,
Maha Anna
 
Kees van Oosterhout
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, just that i understand this correctly:
HttpSessionBindingListener concentrates on what happens to the object being placed in the session and HttpSessionAttributeListener concentrates on what happens to the session itself. Right?
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your first statement about HttpSessionBindingListener is right. One small correction on the 2nd one. HttpSessionAttributeListener listens to ALL sessions for attributes add/replace/remove event s in that particular web application.
Regards,
Maha Anna
 
Vinay Salehithal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an observation:
class A implements HttpSessionBindingListener
{//implement methods}
class B implements HttpSessionAttributeListener
{//implement methods}
Configure both classes in web.xml... and in a servlet do the following
session.setAttribute("A",new A());
The reaction in different servers is as follows:
In weblogic 6.1 sp3, the valuebound method is invoked twice, the attributeadded method is invoked once.
In Tomcat 4.1.12 , the valuebound method is invoked once, the attributeadded method is invoked once.
The 2.3 specs is silent abt such a situation, whats the correct way out?
cheers...
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After your post, I opened up the Servlet 2.3 spec and read more closely. The spec says to provide only one of the 4 listener type classes in web.xml. They are,
1. javax.servlet.ServletContextListener
2. javax.servlet.ServletContextAttributeListener
3. javax.servlet.HttpSessionListener
4. javax.servlet.HttpSessionAttributeListener
It does not specifically say anything about how the container should behave when we add HttpSessionBindingListener to web.xml.
Here is extract from the spec.
*************************************************
SRV.10.3.1 Provision of Listener Classes
The developer of the web application provides listener classes implementing one or
more of the four listener classes
in the servlet API. Each listener class must have a
public constructor taking no arguments. The listener classes are packaged into the
WAR, either under the WEB-INF/classes archive entry, or inside a JAR in the WEBINF/
lib directory.
****************************************
Since you have added a listener class of type HttpSessionBindingListener which is NOT of the listener types given in spec, I guess weblogic, notified the object which implements it, 2 times but Tomcat only once.
Please try to make a test by removing the HttpSessionBindingListener class out of web.xml and let us know how weblogic behaves this time.
Thanks!
Regards,
Maha Anna
[ November 25, 2002: Message edited by: maha anna ]
 
Vinay Salehithal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Removing the HttpSessionBindingListener class from web.xml in weblogic, leads to one time invocation of the methods of the listener class, as expected.
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by maha anna:
............
Thanks!
Regards,
Maha Anna

Maha..
oppss.. I know this is not the place for it , but still I am great fan of you ..... could not stop myself ..
Its great to see you around
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ravish.
Yes. I am the same old Maha Anna
-Maha
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by maha anna:
Thanks Ravish.
Yes. I am the same old Maha Anna
-Maha

Thanks for acknowledgement ....
*same old Maha Anna* - you are always fresh and new
Catch you later in some good discussion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic