Source Code : Servlet: session attribute listener

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

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("
The attribute name: ").append(name)
        .append("
").append("The attribute value:").append(value)
        .append("
").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("
The attribute name: ").append(name)
        .append("
").append("The attribute value: ").append(value)
        .append("
").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);
  }
}



           
         
    
  

Thank with us