File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HttpSessionBindingListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "HttpSessionBindingListener" Watch "HttpSessionBindingListener" New topic


Dilshan Edirisuriya
Ranch Hand

Joined: Apr 22, 2006
Posts: 299
what does HttpSessionBindingListener do ?
what does valueBound() and valueUnbound() amd when it's called?

Dilshan Edirisuriya SCJP1.4, SCWCD1.4, SCBCD 5
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Its to be implemented by objects that you add to a session if you want the object itself to be aware that it is being added or removed from a session.

For example, you might have a User object that you bind to the session when a User logs in. You could make it the responsibility of the User object itself to log that event, which it could do in the valueBound method. Likewise, when the object is removed from the session, it could log this too. Notice that this gives you an opportunity to do something when a session times-out, which is hard to get a hook into otherwise.

Hope that helps,
Balasubramani Dharmalingam
Ranch Hand

Joined: Dec 06, 2004
Posts: 116
For each time you put the object of type HttpSessionBindingListener into the HttpSession valueBound() will be called. When you remove those object from session valueUnbound() will be called.

Employee emp = new Employee() ;
session.setAttribute( "Employee" , emp ) ; // valueBound() of emp will be called
session.removeAttribute( "Employee" ) ; // valueUnbound() of emp will be called

Balasubramani SD,<br />SCJP 1.4,SCWCD 1.4,SCJP 5.0<br /><a href="" target="_blank" rel="nofollow"></a>
Gaurav Gambhir
Ranch Hand

Joined: Mar 04, 2006
Posts: 256
HttpSessionBindingListener exist so that the attribute itself can find out when it has been added to or removed from a session.
Specifically used to synchronize the contents of bean and database.

When setAttribute(String name, Object value) is executed the container calls HttpSessionBindingListener.valueBound, to notify object is bound to session,If an object was already bound to this session of this name that implements HttpSessionBindingListener, its HttpSessionBindingListener.valueUnbound method is called.
Dilshan Edirisuriya
Ranch Hand

Joined: Apr 22, 2006
Posts: 299
Thank you
Sathish Nagappan
Ranch Hand

Joined: Nov 05, 2005
Posts: 76
Hey Dilshaan !! Just curious, Do you really have the Head First JSP and Servlets Book or are you looking at the specs and asking the questions here. Get the book man !! Its much easier to learn from that. Whatever questions you are asking are very simple in the book and I'm sure most of us here would feel like nutcases trying to answer your questions. Even though the Java Ranch blood (with me) is nice people all the way !! :-)

SCJP, SCWCD, SCBCD, IBM XML, IBM OOAD, SCEA 5, ITAC - Distinguished IT Architect.
Dilshan Edirisuriya
Ranch Hand

Joined: Apr 22, 2006
Posts: 299
Aswanth Sorry for the inconvinience
I have the book but. But some of the things are confusing without examples.
Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130

never be embarrassed about asking questons. that's what the forums are for. what is perfectly clear to one person in a book may be quite murky to another. when you ask a question you might very well be asking that others have wondered about as well.

httpsessionbindinglistener has always seemed a bit odd to me and i'm sure it has to some other people as well. so i'm not surprised that you asked the question, and i think you got some informative answers.

so keep asking questions...

Dilshan Edirisuriya
Ranch Hand

Joined: Apr 22, 2006
Posts: 299
Thank you Ken for your opinion. Yesterday I got upset after I saw that. But they must understand that everybody doesn't have the same mental ability.
I agree. Here's the link:
subject: HttpSessionBindingListener
It's not a secret anymore!