Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpSessionBindingListener

 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does HttpSessionBindingListener do ?
what does valueBound() and valueUnbound() amd when it's called?
 
Daniel Dalton
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gaurav Gambhir
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
Sathish Nagappan
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !! :-)
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aswanth Sorry for the inconvinience
I have the book but. But some of the things are confusing without examples.
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

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...

ken
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic