FileDocCategorySizeDatePackage
SessionCounter.javaAPI DocExample1202Wed Jun 30 17:12:44 BST 2004com.oreilly.strutsckbk.ch07

SessionCounter

public class SessionCounter extends Object implements HttpSessionListener

Fields Summary
Constructors Summary
Methods Summary
public voidsessionCreated(javax.servlet.http.HttpSessionEvent event)

        ServletContext ctx = event.getSession().getServletContext();
        Integer numSessions = (Integer) ctx.getAttribute("numSessions");
        if (numSessions == null) {
            numSessions = new Integer(1);
        }
        else {
            int count = numSessions.intValue();
            numSessions = new Integer(count + 1);
        }
        ctx.setAttribute("numSessions", numSessions);
    
public voidsessionDestroyed(javax.servlet.http.HttpSessionEvent event)

        ServletContext ctx = event.getSession().getServletContext();
        Integer numSessions = (Integer) ctx.getAttribute("numSessions");
        if (numSessions == null) {
            numSessions = new Integer(0);
        }
        else {
            int count = numSessions.intValue();
            numSessions = new Integer(count - 1);
        }
        ctx.setAttribute("numSessions", numSessions);