Forums Register Login

When the HttpSessionBindingListener get called?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
glad to know that I can be of your help

Happy Learning !!
Shiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2385 times.
Similar Threads
doubt on HttpSessionBindingListener
Session Listeners Questions
HttpSessionBindingListener
Session Listeners confusion?
log method in servlet
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:04:13.