• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HttpSessionAttributeListener and HttpSessionBindingListener???

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
raymond yadao
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sathya, have u taken the scwcd?
 
Sathya Sankar
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not yet. Just completed my SCJD essay exam, anxiously awaiting results. Planing to take SCWCD by end November.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HttpSessionBindingListener is not configured in the deployment descriptor.
-Sainudheen
 
Shreyas Reddy
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Replace the word "snake" with "danger noodle" in all tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic