How do I get a notification when session attribute was changed?
Category: javax.servlet, viewed: 811 time(s).
Implementing the HttpSessionAttributeListener will make the servlet container inform you about session attribute changes. The notification is in a form of HttpSessionBindingEvent object. The getName() on this object tell the name of the attribute while the getValue() method tell about the value that was added, replaced or removed.
package org.kodejava.example.servlet; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; public class SessionAttributeListener implements HttpSessionAttributeListener { public void attributeAdded(HttpSessionBindingEvent event) { // // This method is called when an attribute is added to a session. // The line below display session attribute name and its value. // System.out.println("Session attribute added: [" + event.getName() + "] = [" + event.getValue() + "]"); } public void attributeRemoved(HttpSessionBindingEvent event) { // // This method is called when an attribute is removed from a session. // System.out.println("Session attribute removed: [" + event.getName() + "] = [" + event.getValue() + "]"); } public void attributeReplaced(HttpSessionBindingEvent event) { // // This method is invoked when an attibute is replaced in a session. // System.out.println("Session attribute replaced: [" + event.getName() + "] = [" + event.getValue() + "]"); } } |
After your listener is ready don't forget to configure it in the web application deployment descriptor, the web.xml file.
Related Examples
- How do I get servlet request header information?
- How do I get servlet request URL information?
- How do I read servlet context initilization parameters?
- How do I know session last access time?
- How do I capture session creation and removal events?
- How do I invalidate user's session?
- How do I get client IP and hostname in Servlet?
- How do I set the maximum age of a cookie?
- How do I send a response status in servlet?
- How do I read servlet init parameter?
|
|