![]() |
![]() |
||||
A session attribute listener receives notifications when a session is changed.
This is useful for general debugging, and especially useful when writing a distributable web application, to ensure that session replication to other members of the cluster is working as expected.
If you want to be notified when sessions are created or invalidated, see Session Listener. Both of these interfaces can be implemented in the same class, to create a single listener which is notified about create, destroy and change events.
This page focuses only on the Session Attribute Listener interface.
To create a Session Attribute Listener, create a class which implements the HttpSessionAttributeListener interface.
This involves implementation of the following three methods:
public void attributeAdded(HttpSessionBindingEvent sessionBindingEvent);
|
Each listener class must also have a public constructor taking no arguments.
This method is called whenever a new session attribute is added with HttpSession.setAttribute().
When attributes are replaced, attributeReplaced() is called instead (see below).
This method is called whenever a session attribute is removed with HttpSession.removeAttribute().
This method is called whenever an existing session attribute is replaced with a new value, using HttpSession.setAttribute().
The session is passed to both of the above methods in the HttpSessionBindingEvent object.
It can be retrieved as follows:
HttpSession session = sessionBindingEvent.getSession();
|
The name and value of the changed attribute are passed in the HttpSessionBindingEvent object.
They can be retrieved with getName() and getValue() as follows:
String attributeName = sessionBindingEvent.getName();
|
import javax.servlet.*;
|
Add the above class to WEB-INF/classes, and add the following in web.xml to activate our new SessionAttributeListener.
<listener> <listener-class>MySessionAttributeListener</listener-class> </listener>
Note that the above class was not put in a package, so no package name is specified in the <listener-class> tag.
[SessionAttr] Wed Dec 06 23:33:39 EST 2006 Attribute added, session org.apache.catalina.cluster.session.DeltaSessionFacade@3d8f1be9: name=Cassie [SessionAttr] Wed Dec 06 23:33:39 EST 2006 Attribute replaced, session org.apache.catalina.cluster.session.DeltaSessionFacade@3d8f1be9: marketSettings=portal.market.Settings@3302fc5 [SessionAttr] Wed Dec 06 23:33:39 EST 2006 Attribute replaced, session org.apache.catalina.cluster.session.DeltaSessionFacade@3d8f1be9: javax.servlet.jsp.jstl.fmt.request.charset=ISO-8859-1This is what showed when the session expired, as Tomcat cleaned up the variables by removing them all from the session:
[SessionAttr] Thu Dec 07 00:22:45 EST 2006 Attribute removed, session org.apache.catalina.cluster.session.DeltaSessionFacade@3d8f1be9: name [SessionAttr] Thu Dec 07 00:22:45 EST 2006 Attribute removed, session org.apache.catalina.cluster.session.DeltaSessionFacade@3d8f1be9: marketSettings [SessionAttr] Thu Dec 07 00:22:45 EST 2006 Attribute removed, session org.apache.catalina.cluster.session.DeltaSessionFacade@3d8f1be9: javax.servlet.jsp.jstl.fmt.request.charset