This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HttpSessionBindingListener question  RSS feed

 
Stephen Peterson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading from my copy of Professional Java Server Programming j2ee edition page 408, which says attirbute objects can optionally implement hte HttpSessionBindingListener interface, which allows the attirbute objects to be notified when you add them to or remove them from the HttpSession. When the attribute implementing this interface is added to the HttpSession the web container notifies the attribute that it is being bound to the sessoin, simmilarly unbound.
The 2 methods in this interface are valueBound and value unbound. An argument is passed to each: HttpSessionBindingEvent.
My question pertains to the 2 methods on this HttpSessionBindingEvent class that are provided ;
String getName() returns the name of the attribute that was used in the bind or unbind, and
HttpSession getSession() returns the HttpSession object from which the object is bound or unbound.
Why these 2 methods? If the event is sent to the object that is getting bound or unbound, then why would anyone need the name within that object? And what would anyone use the getSession method for?
I am storing objects in the servlet context to lock resources on a web-container-wide basis, and it would be nice if a session-timeout would release the locks for me in the servlet-context-held object. But since I don't get what these two HttpSessionBindingEventClass methods are good for, I am wondering if I am missing something important in my understanding?

------------------
United Health Group
Hartford, CT, USA
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I imagine that getName is there to allow the same listener class to handle different kinds of session data, depending on the name used when it was attached to the session.
You have to have getSession in order to get at other information attached to the session. For example, suppose you wanted to be able to detect an incomplete purchase order so you could save it and present it again the next time the user logged on.
Bill

------------------
author of:
 
Stephen Peterson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My feeble mind finds your first sentence a bit confusing. Is the 'same listener class' the object implementing the HttpSessionBindingListener Interface, which would be the object that is getting bound/was bound to the session? What situation would suggest a need to access a different session-bound object than the one receiving the event?
It sounds like the example in your second paragraph is implying a valueUnbound event from the session getting invalidated, per the phrase 'the next time the user logged on'. If the session is getting invalidated, then there wouldn't be any other session-attached information to get at, right?
Could you clarify a bit more?
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephen,
Originally posted by Stephen Peterson:
[..]It sounds like the example in your second paragraph is implying a valueUnbound event from the session getting invalidated, per the phrase 'the next time the user logged on'. If the session is getting invalidated, then there wouldn't be any other session-attached information to get at, right?

When you valueUnbound() a session, by say, invalidating the session, you may want to save some important information of that session before this.The method, getSession() will allows you to do so.The next time the user logs in, you may provide him with that information.For example, say you could store the full name of a person associated with a userName, so you don't need to go the DB to get this information.
I am also confused about the getName() method.It makes sense, when you are playing with more than one session instances on the JSP page.But why would you want this to happen?William Brogden, could you explain?
Thanks,
Sandeep

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!