File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HttpSessionAttributeListener and HttpSessionBindingListener??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "HttpSessionAttributeListener and HttpSessionBindingListener???" Watch "HttpSessionAttributeListener and HttpSessionBindingListener???" New topic
Author

HttpSessionAttributeListener and HttpSessionBindingListener???

raymond yadao
Ranch Hand

Joined: Jan 29, 2001
Posts: 88
Hi.Based from SCWCD book of sybex, it says that when a session timeout, the HttpSessionBindingListener will be called not the HttpSessionAttributeListener.
I have tried some sample web application, both have a HttpSessionAttributeListener and HttpSessionBindingListener.I have created a session in my servlet using request.getSession(), then added an attribute( request.getSession().setAttribute("test","test")).The attributeAdded of the HttpSessionAttributeListener was called ( not the valueBound of HttpSessionBindingListener) and when i try to call the invalidate() method(HttpSession), still the HttpSessionAttributeListener was called (thru the method attributeRemoved).
can anybody explain about this?or there's something wrong about the explanation of sybex?
btw, im using tomcat4.1.8.
thanks.
raymond
Sathya Sankar
Ranch Hand

Joined: Sep 16, 2000
Posts: 67
Hi Raymond,
I do not have the book you mentioned. So am not in a position to say if the book is correct or not.
However, here's how the two listeners work.
1. HttpSessionAttributeListener should be specified in the deployment descriptor
2. HttpSessionBindingListener is implemented by the object that is being added to the session.
When an object is added to the session:
i. attributeAdded of HttpSessionAttributeListener is invoked
ii. if the object added implements HttpSessionBindingListener, valueBound method of the object is invoked.
When an object is removed from the session od the session gets invalidated:
i. attributeRemoved of HttpSessionAttributeListener is invoked
ii. if the object added implements HttpSessionBindingListener, valueUnbound method of the object is invoked.
In your example, the attribute added is a String object "test". Now since String does not implement HttpSessionBindingListener, valueBound and valueUnbound methods will not be invoked.
Ciao,
GSS


SCJP, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
raymond yadao
Ranch Hand

Joined: Jan 29, 2001
Posts: 88
HI Sathya. thanks for your reply.
"In your example, the attribute added is a String object "test". Now since String does not implement HttpSessionBindingListener, valueBound and valueUnbound methods will not be invoked"
so you mean, in order for the HttpSessionBindingListener to be invoked, the Object to be added in a session must implement this interface?I thought that you just declare a listener and then configure it in the deployment descriptor and its up to the container to register the listener using reflection.
raymond
Sathya Sankar
Ranch Hand

Joined: Sep 16, 2000
Posts: 67
Hi Raymond,
so you mean, in order for the HttpSessionBindingListener to be invoked, the Object to be added in a session must implement this interface?

Yes, that is right. Only HTTPSessionAttributeListener will be specified in the deployment descriptor and the container will register this internally. For HttpSessionBindingListener, the object added to the session has to implement it. The container will introspect the object when it is added/removed from the session to see if it implements HttpSessionBindingListener. If so, the valueBound and valueUnbound methods will be invoked.
Ciao,
GSS
raymond yadao
Ranch Hand

Joined: Jan 29, 2001
Posts: 88
Sathya, have u taken the scwcd?
Sathya Sankar
Ranch Hand

Joined: Sep 16, 2000
Posts: 67
Not yet. Just completed my SCJD essay exam, anxiously awaiting results. Planing to take SCWCD by end November.
Shreyas Reddy
Ranch Hand

Joined: Oct 09, 2003
Posts: 49
Does that mean HttpSessionBindingListener need not be registered in web.xml?
I thought all the listerners must be declared in the web.xml.
Thx ina dvance
Sainudheen Mydeen
Ranch Hand

Joined: Aug 18, 2003
Posts: 218
HttpSessionBindingListener is not configured in the deployment descriptor.
-Sainudheen
Shreyas Reddy
Ranch Hand

Joined: Oct 09, 2003
Posts: 49
I am sorry for asking so many questions.. but if the listner is not registered in web.xml, how does the container know that there IS a listner.
I was looking at the java servlet 2.3 api docs, for the following three listners, the docs doesn't say we have to decalre the listeners in web.xml.
HttpSessionAttributeListener
HttpSessionBindingListener
HttpSessionActivationListener

thanks
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Whenever an object is added to or removed from a container, the container introspects the object's implemented interfaces. If HttpSessionBindingListener is implemented, the container will invoke valueBound or valueUnbound.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Ryan Wilson
Ranch Hand

Joined: Apr 16, 2003
Posts: 65
When you create a class that implements one on the listeners (other than HttpSessionBindingListener) you need to add this class in the deployment descriptor.
<listener>
<listener-class>com.abc.MyListener</listener-class>
</listener>
The container will look at this class and check to see what listener interfaces it implements. You do not have to explicitly state in the deployment descriptor that you are going to use this class for the HttpSessionAttributeListener or HttpSessionActivationListener but you do have to put the class that implements these interfaces in the web.xml.
Shreyas Reddy
Ranch Hand

Joined: Oct 09, 2003
Posts: 49
So Except for classes that implement HttpSessionBindingListener, all the classes which implement the other 5 listeners must be registered in web.xml as follows:
<listener>
<listener-class>mylisnterclass</listener-class>
</listener>
Is that correct?
Sathya Sankar
Ranch Hand

Joined: Sep 16, 2000
Posts: 67
Shreyas,
So Except for classes that implement HttpSessionBindingListener, all the classes which implement the other 5 listeners must be registered in web.xml as follows:
<listener>
<listener-class>mylisnterclass</listener-class>
</listener>
Is that correct?

Ans : Nope.
There is one more listener - HttpSessionActivationListener that is similar to HttpSessionBindingListener. This listener will not be declared in the deployment descriptor. But should be implemented by the oibject being added/removed from the session. The container at runtime will introspect this object to see if it implements the HttpSessionActivationListener and/or HttpSessionBindingListener and fires appropriate events to the object.
So that leaves the remaining 4 listeners that could be present in the deployment descriptor:
1. ServletContextAttributeListener
2. ServletContextListener
3. HttpSessionAttributeListener
4. HttpSessionListener
Ciao,
GSS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpSessionAttributeListener and HttpSessionBindingListener???