首先写个类:
importjavaxservlet*;
importjavaxservlet*;
publicclassSessionCounterimplementsHttpSessionListener{
privatestaticintactiveSessions=;
publicvoidsessionCreated(HttpSessionEventse){
activeSessions++;
}
publicvoidsessionDestroyed(HttpSessionEventse){
if(activeSessions>)
activeSessions;
}
publicstaticintgetActiveSessions(){
returnactiveSessions;
}
}
然后配置webxml
<?xmlversion=""encoding="UTF"?>
<webappversion=""
xmlns="
xmlns:xsi="
xsi:schemaLocation="
****************************************
<!Listeners>
<listener>
<listenerclass>
SessionCountSessionCounter(注意此处)
</listenerclass>
</listener>
*****************************************
</webapp>
建个JSP测试:
testjsp
<%@pagelanguage="Java"contentType="text/html;charset=GBK"%>
<%@pageimport="javasql*"%>
<%@pageimport="SessionCountSessionCounter"%>
<html>
<head>
<metahttpequiv="ContentType"content="text/html;charset=GBK">
<title>无标题文档</title>
<bodybgcolor="#FFFFFF">
在线人数:<%=SessionCountergetActiveSessions()%>
</body>
</html>
测试以下会发现程序已经统计出结果了