Causes an object to be notified when it is bound to or unbound from a session. The object is notified by an HttpSessionBindingEvent object. This may be as a result of a servlet programmer explicitly unbinding an attribute from a session, due to a session being invalidated, or due to a session timing out.
package com.bbs.util;import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Listener implements HttpSessionListener{private static int counter=0;
private static int activeCount=0;public void sessionCreated(HttpSessionEvent evt){
long times=evt.getSession().getCreationTime();
System.out.println("A new session was created at"+
new Date(times));
counter++;
activeCount++;
}public void sessionDestroyed(HttpSessionEvent evt){
activeCount--;
}public static int getActiveCount(){
return activeCount;
}
}web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<listener>
<listener-class>
com.bbs.util.Listener
</listener-class>
</listener>
<session-config>
<session-timeout>
1
</session-timeout>
</session-config>
</web-app>
在jsp页面中调用bean的getActiveCount()方法。