![]() |
![]() |
||||
At line 7 changed 1 line. |
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 listener which is notified about create, destroy and change events. |
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. |
At line 13 changed 1 line. |
To create a Session Attribute Listener, create a class which implements the [HttpSessionAttributeListener|http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSessionAttributeListener.html] interface. |
To create a Session Attribute Listener, create a class which implements the [HttpSessionAttributeListener|http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/http/HttpSessionAttributeListener.html] interface. |
At line 15 changed 1 line. |
This involves implementation of the following two methods: |
This involves implementation of the following three methods: |
At line 23 added 2 lines. |
Each listener class must also have a public constructor taking no arguments. |
At line 25 changed 1 line. |
This method is called whenever a new session attribute is added with [HttpSession.setAttribute()|http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.html#setAttribute(java.lang.String,%20java.lang.Object)]. |
This method is called whenever a new session attribute is added with [HttpSession.setAttribute()|http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/http/HttpSession.html#setAttribute(java.lang.String,%20java.lang.Object)]. |
At line 31 changed 1 line. |
This method is called whenever a session attribute is removed with [HttpSession.removeAttribute()|http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.html#removeAttribute(java.lang.String)]. |
This method is called whenever a session attribute is removed with [HttpSession.removeAttribute()|http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/http/HttpSession.html#removeAttribute(java.lang.String)]. |
At line 35 changed 1 line. |
This method is called whenever an existing session attribute is replaced with a new value, using [HttpSession.setAttribute()|http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.html#setAttribute(java.lang.String,%20java.lang.Object)]. |
This method is called whenever an existing session attribute is replaced with a new value, using [HttpSession.setAttribute()|http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/http/HttpSession.html#setAttribute(java.lang.String,%20java.lang.Object)]. |
At line 37 changed 1 line. |
![HttpSession|http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.htm] |
![HttpSession|http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/http/HttpSession.htm] |
At line 39 changed 1 line. |
The session is passed to both of the above methods in the [HttpSessionBindingEvent|http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSessionBindingEvent.html] object. |
The session is passed to both of the above methods in the [HttpSessionBindingEvent|http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/http/HttpSessionBindingEvent.html] object. |
At line 49 changed 1 line. |
The name and value of the changed attribute are passed in the [HttpSessionBindingEvent|http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSessionBindingEvent.html] object. |
The name and value of the changed attribute are passed in the [HttpSessionBindingEvent|http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/http/HttpSessionBindingEvent.html] object. |
At line 68 added 3 lines. |
public MySessionAttributeListener() { |
} |
At line 72 changed 1 line. |
System.out.println("[SessionInfo] "+new java.util.Date()+" Attribute added, session "+session+": "+sessionBindingEvent.getName()+"="+sessionBindingEvent.getValue()); |
System.out.println("[SessionAttr] "+new java.util.Date()+" Attribute added, session "+session+": "+sessionBindingEvent.getName()+"="+sessionBindingEvent.getValue()); |
At line 81 changed 1 line. |
System.out.println("[SessionInfo] "+new java.util.Date()+" Attribute removed, session "+session+": "+sessionBindingEvent.getName()); |
System.out.println("[SessionAttr] "+new java.util.Date()+" Attribute removed, session "+session+": "+sessionBindingEvent.getName()); |
At line 90 changed 1 line. |
System.out.println("[SessionInfo] "+new java.util.Date()+" Attribute replaced, session "+session+": "+sessionBindingEvent.getName()+"="+sessionBindingEvent.getValue()); |
System.out.println("[SessionAttr] "+new java.util.Date()+" Attribute replaced, session "+session+": "+sessionBindingEvent.getName()+"="+sessionBindingEvent.getValue()); |
At line 107 changed 1 line. |
!See Also |
!Sample Output |
This is some output from a real application that uses the above code. The session shows as DeltaSessionFacade because this application is clustered using [Tomcat] clustering (see the <[distributable|web.xml.Distributable]> tag). The logs below were used to detect a problem with session replication, and verify that session attributes were really being replicated across to the other members of the cluster. |
{{{ |
[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-1 |
}}} |
This 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 |
}}} |
!Other Listener Examples |
At line 109 changed 3 lines. |
* [HttpSessionAttributeListener|http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSessionAttributeListener.html] Java Docs at Sun |
* [HttpSessionBindingEvent|http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSessionBindingEvent.htm] Java Docs at Sun |
* [HttpSession|http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.htm] Java Docs at Sun |
* [Context Listener] |