An object which may be stored in a session may need to be notified
of events which occur in the session lifecycle, as well as it being bound or unbound
to the
session.
If the object implements the HttpSessionBindingListener interface, the container will
notify the
object via the method
public abstract void valueBound(javax.servlet.http.HttpSessionBindingEvent)
when the object is bound ,or unbound via the method
public abstract void valueUnbound(javax.servlet.http.HttpSessionBindingEvent).
You would normally bind the object using HttpSession.setAttribute(java.lang.String,
java.lang.Object) and unbind using HttpSession.removeAttribute(java.lang.String). All
objects are
unbound at session expiration.
The code implementing the valueBound and valueUnbound methods defines the object's
behaviour on
the event. You can grok the reason for the event, if needed, from it's
javax.servlet.http.HttpSessionBindingEvent argument, though I usually ignore it.
For example, if you need to, open or close an arbitrary resource :
class Foo implements HttpSessionBindingListener{
private Resource resource = newResource;
public Resource getResource(){
return resource;
}
public void valueBound(javax.servlet.http.HttpSessionBindingEvent){
resource.open();
}
public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent){
resource.close();
resource = null;
}
}
In the servlet,
Foo foo = new Foo();
session.setAttribute("AFoo", foo); // foo.valueBound(...) is called by container
session.removeAttribute("AFoo"); // foo.valueUnbound(...) is called by container
------------------------------------------------------------------------------------------
I use this technique to manage JDBC connections, statements and resultsets between
view result
pages; using a bound object to contain query state and using the valueUnbound method
to close the
connection/resultset, etc. when the session expires.
--- Maan Jitendra <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> can you please explain me how can we implement the
> HttpSessionBindingListener in our application. Thanks in advance
>
> Regards,
>
> jitendra maan
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
=====
Mark Zawadzki Performance Engineer/DBA/Programmer extraordinaire� [EMAIL PROTECTED]
[EMAIL PROTECTED]
"Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the universe trying to build bigger and better idiots.
So far, the universe is winning"
Robert Cringle (columnist, author, host of "Triumph of the Nerds")
__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html