web.xml中配置 < listener > < listener-class > listener.SessionCounter </ listener-class > </ listener > java类,实现HttpSessionListener接口 package listener; import java.util. * ; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** */ /** * activeSessions 当前在线用户数 * all 总访问量 * today 今日访问量 * day 今日日期 */ public class SessionCounter implements HttpSessionListener ... { private static int activeSessions=0; private static long today=0; private static long all=0; private static int day=0;//session创建 public void sessionCreated(HttpSessionEvent arg0) ...{ if(activeSessions<0)...{ activeSessions=0; }else...{ activeSessions++; } all++; Date date=new Date(); int tday=date.getDate();//如果日期发生变化则将today置为1,否则将today加1 if(tday!=day)...{ day=tday; today=1; }else...{ today++; } }//减少在线人数 public void sessionDestroyed(HttpSessionEvent arg0) ...{ if(activeSessions>0)...{ activeSessions--; } } public static int getOnlines()...{ return activeSessions; } public static long getAll()...{ return all; } public static long getToday()...{ return today; }} 当关闭浏览器窗口时,session不会被清除,只有等到过了session有效时间后(默认为30分钟)SessionListener才会执行sessionDestroyed方法。