aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes When the HttpSessionBindingListener get called? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "When the HttpSessionBindingListener get called?" Watch "When the HttpSessionBindingListener get called?" New topic
Author

When the HttpSessionBindingListener get called?

Hitesh Kumar C
Ranch Hand

Joined: Sep 12, 2008
Posts: 34

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.


OCJP 6.0 - 88%
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1162
    
    1

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

Joined: Sep 12, 2008
Posts: 34

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

Joined: Feb 29, 2008
Posts: 1162
    
    1

glad to know that I can be of your help

Happy Learning !!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: When the HttpSessionBindingListener get called?