FileDocCategorySizeDatePackage
SessionListen.javaAPI DocExample1536Wed Feb 11 14:31:16 GMT 2004com.jspservletcookbook

SessionListen

public class SessionListen extends Object implements HttpSessionListener

Fields Summary
private int
sessionCount
Constructors Summary
public SessionListen()

        this.sessionCount = 0;
    
Methods Summary
public voidsessionCreated(javax.servlet.http.HttpSessionEvent se)

        HttpSession session = se.getSession();
        session.setMaxInactiveInterval(60);
        synchronized(this){
            sessionCount++;
        }
        String id = session.getId();
        Date now = new Date();
        String message = new StringBuffer(
          "New Session created on ").
            append(now.toString()).append("\nID: ").
              append(id).append("\n").append("There are now ").
                append(""+sessionCount).append(
                  " live sessions in the application."). toString();
        
        System.out.println(message);
     
public voidsessionDestroyed(javax.servlet.http.HttpSessionEvent se)

        
        HttpSession session = se.getSession();
        String id = session.getId();
        synchronized(this){
                --sessionCount;
        }
        String message = new StringBuffer("Session destroyed" + 
          "\nValue of destroyed session ID is").
              append(""+id).append("\n").append(
                "There are now ").append(""+sessionCount).append(
                  " live sessions in the application.").toString();
        System.out.println(message);