• 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

The difference between HttpSessionAttributeListener and HttpSessionBindingListener

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello:
I am confusing about these two listeners. I am just wondering if you may help me.
Thanks in advance
Maria
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HttpSessionBindingListener is implemented by session attributes.
HttpSessionAttributeListener instance is created by Web-Container, based on <listern> element in web.xml. For each <listern-class>, one listern instance is created.
Now you can see, if your session has 3 attributes, each of them implements HttpSessionBindingListener, you got 3 ***Binding listeners.
But if you only define one <listener-class> in web.xml, you only get one ***Attribute listener.

another important difference, HttpSessionAttributeListener monitors ALL Sessions in the web-app, whenver any session has an attribute added/removed/replace, the listener got notified.
HttpSessionBingListener only cares about itself. If itself is added into or removed from a session, it is notified.
 
Maria Tan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. It's clear.
Maria
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some examples
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question regarding HttpSessionBindingListener and HttpSessionAttributeListener.
Say the definition of classes MyBindingListener , TestServlet and snippet of web.xml are as follows:

where class MySessionAttributeListener implements HttpSessionAttributeListener interface.
My qustion is:
When we access the TestServlet from a browser, which listener classes will be notified by
session.setAttribute("countObject", countObject) statement?
(A) MySessionAttributeListener
(B) MyBindingListener
(C) Both listeners(MySessionAttributeListener and MyBindingListener)
Thanks in advance
Thambi
 
Kyle Tang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
both
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kyle Tang:
both



Kylie
why is it both and not just the class specified in web.xml?
cheers
Dan
 
Daniel Roach
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops kyle, sorry about misspelling your name
why is it both and not just the class specified in web.xml?
cheers
 
Kyle Tang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is by definition of these 2 listeners. there is no reason why only one of them get notified, and the other get blocked. your web-container will follow what the spec says. if the attribute that is added to a session implements HttpSessionBindingListener, it will get notified. In the mean time, if there is a HttpSessionAttributeListener defined in web.xml, that listener instance will also get notified.
please refer to my earlier reply to the original post.
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic