• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

When the HttpSessionBindingListener get called?

 
Ranch Hand
Posts: 35
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm Preparing for OCEJWCD6.0 and while studying the Headfirst I got confused on one question in Chapter 6 - Session Management.

Mock Question no. 9 :

And the answer is F (HttpSessionBindingListener and HttpSessionAttributeListener)

I've configured the HttpSessionAttributeListener and HttpSessionBindingListener in web.xml and HttpSessionAttributeListener is getting called up(It's fine, I got the concept). But for HttpSessionBindingListener, It didn't get called even though i've configured it in web.xml.
In the answers, there is an explanation:

because an HttpSessionAttributeListener is notified any time an attribute is added and the value object will also be notified if it implements an HttpSessionBindingListener.



My Question is that When the HttpSessionBindingListener methods :
public void valueBound(HttpSessionBindingEvent event)
public void valueUnbound(HttpSessionBindingEvent event)
get called up?

Code snippet :


Please put your comments on it or correct me if i'm doing something wrong.
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How have you used HttpSessionBindingListenerTest class in your code ? btw please post sample code which you are trying to use.

Answer to your query lies in your question.

because an HttpSessionAttributeListener is notified any time an attribute is added and the value object will also be notified if it implements an HttpSessionBindingListener.


Read this quote carefully.
Here, it says that Attribute need to implement HttpSessionBindingListener so that valueBound() and other callback methods can be called by servlet container.
First of all , answer toy your sample code
For this to work, you need to add HttpSessionBindingListenerTest object in Session as attribute.
so , below line should call callback methods of both HttpSessionBindingListener and HTTPSessionAttributeListener.

Now coming to your mock question
At the first, it might seem a bit confusing .. you might question why we need two listener for same thing .. well .. it is not same .....you need to understand and remember the difference between two Listeners in terms of who gets notified or who is the main point of concern.
HTTPSessionAttributeListener allows you to listen when any new attribute is added in [ or existing attribute is removed from] Session. Here, out main context is Session and we want to keep track [ listen] all attributes that are getting added/removed in Session. Session is our main point of concern over which we want to keep an eye [ or ear ].

HttpSessionBindingListener allows you to listen an particular object [ or instance of class] when that object is added in/remove from Session.
For example - I can create an class - UserInfo class which implements HttpSessionBindingListener. Now, when I add object of UserInfo class in HTTP Session, callback methods of HttpSessionBindingListener gets called. So , here , out main point of concern is User Object which get added in Session. We may have number of attributes getting added in [ or removed from ] Session, here, we want to listen only particular objects of a class.

Now, if you add String value in Session, HttpSessionBindingListener methods shall not get called as String class in Java do not implement HttpSessionBindingListener.

Now I think you shall be able to answer this question yourself


Since in this question, it is not specified whether 'value' is an object implementing HttpSessionBindingListener or not. value can be any thing - an Integer, String, an object implementing HttpSessionBindingListener .
So , here answer is F (HttpSessionBindingListener and HttpSessionAttributeListener)

Reply back if you have any queries
 
Hitesh Kumar C
Ranch Hand
Posts: 35
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for so much delay in the reply.

Thanks a lot for writing in so much detail.

I got understood the flow, using your statement, that actually when it calls the HttpSessionBindingListener.



Thanks a bunch.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
glad to know that I can be of your help

Happy Learning !!
 
You got style baby! More than this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic