FAQ

Java

JSP

Servlet


Advertisement



When do I use HttpSessionAttributeListener?

The HttpSessionAttributeListener interface is used to monitor changes (added, removed, and replaced) to attributes in any session on the server.

This can be useful when you know the name assigned to a specific object that gets put into the session and you want to track how often it's being used. For example,

package mypackage;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public final class MySessionAttributeListener
 implements HttpSessionAttributeListener {

public void attributeAdded(HttpSessionBindingEvent event) {
System.out.println("attributeAdded");
System.out.println(event.getSession().getId());
System.out.println(event.getName() + "', '" + event.getValue());
}

public void attributeRemoved(HttpSessionBindingEvent event) {
System.out.println("attributeRemoved");
System.out.println(event.getSession().getId());
System.out.println(event.getName() + "', '" + event.getValue());
}

public void attributeReplaced(HttpSessionBindingEvent event) {
System.out.println("attributeReplaced");
System.out.println(event.getSession().getId());
System.out.println(event.getName() + "', '" + event.getValue());
}
}

As with HttpSessionListener, HttpSessionAttributeListener also requires an entry in the deployment descriptor for the server. This entry tells the server which class to call when an attribute in a session has changed. The class implementing HTTPSessionAttributeListener will be specified in the deployment descriptor and the container will register this internally. For example,


mypackage.MySessionAttributeListener

The HttpSessionAttributeListener interface has three methods that are called by the server when attributes of a session are changed:

  • public void attributeAdded(HttpSessionBindingEvent se) : Notification that an attribute has been added to a session. Called after the attribute is added.
  • public void attributeRemoved(HttpSessionBindingEvent se) : Notification that an attribute has been removed from a session. Called after the attribute is removed.
  • public void attributeReplaced(HttpSessionBindingEvent se) : Notification that an attribute has been replaced in a session. Called after the attribute is replaced.

The HttpSessionListener and HttpSessionAttributeListener is defined in in your web.xml file. They are "application-wide", they manage all the session in your web-application! And they are instanticated by your web-container.

Even if your session attribute implements HttpSessionListener or HttpSessionAttributeListener, but you do not define that in web.xml, there is NO HttpSessionListener or HttpSessionAttributeListener instance in your web-application at all! (If you just create an HttpSessionListener instance by your own, it won't work because your web-application does not know at all, it only checks the web.xml).


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |