Servlet: session attribute listener : Session « Servlets « Java






Servlet: session attribute listener

  
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class SessionAttribListen implements HttpSessionAttributeListener {

  /** Creates new SessionAttribListen */
  public SessionAttribListen() {

    System.out.println(getClass().getName());
  }

  public void attributeAdded(HttpSessionBindingEvent se) {

    HttpSession session = se.getSession();
    String id = session.getId();
    String name = se.getName();
    String value = (String) se.getValue();
    String source = se.getSource().getClass().getName();
    String message = new StringBuffer("Attribute bound to session in ")
        .append(source).append("\nThe attribute name: ").append(name)
        .append("\n").append("The attribute value:").append(value)
        .append("\n").append("The session ID: ").append(id).toString();
    System.out.println(message);
  }

  public void attributeRemoved(HttpSessionBindingEvent se) {

    HttpSession session = se.getSession();
    String id = session.getId();
    String name = se.getName();
    if (name == null)
      name = "Unknown";
    String value = (String) se.getValue();
    String source = se.getSource().getClass().getName();
    String message = new StringBuffer("Attribute unbound from session in ")
        .append(source).append("\nThe attribute name: ").append(name)
        .append("\n").append("The attribute value: ").append(value)
        .append("\n").append("The session ID: ").append(id).toString();
    System.out.println(message);
  }

  public void attributeReplaced(HttpSessionBindingEvent se) {

    String source = se.getSource().getClass().getName();
    String message = new StringBuffer("Attribute replaced in session  ")
        .append(source).toString();
    System.out.println(message);
  }
}



           
         
    
  








Related examples in the same category

1.Using Sessions in Servlet
2.Session Tracker
3.Servlet: simple session
4.Session logger
5.Servlet: Session display
6.Servlet: session listener
7.Servlet : session filter
8.Servlet: Session bind listener
9.Servlet Session Example
10.Use cookie to save session data
11.Use hidden fields to save session data
12.Use URL rewrite to save session data
13.Session Events: implements HttpSessionBindingListener
14.Session Expiration Filter
15.Map adaptor for HttpSession objects
16.Fake session