首先写个类:
import javaxservlet*;
import javaxservlethttp*;
public class SessionCounter implements HttpSessionListener {
private static int activeSessions = ;
public void sessionCreated(HttpSessionEvent se) {
activeSessions++;
}
public void sessionDestroyed(HttpSessionEvent se) {
if(activeSessions > )
activeSessions;
}
public static int getActiveSessions() {
return activeSessions;
}
}
然后配置webxml
<?xml version= encoding=UTF?>
<webapp version=
xmlns=
xmlns:xsi=instance
xsi:schemaLocation=
app__xsd>
****************************************
<! Listeners >
<listener>
<listenerclass>
SessionCountSessionCounter (注意此处)
</listenerclass>
</listener>
*****************************************
</webapp>
建个JSP测试:
testjsp
<%@ page language=Java contentType=text/html;charset=GBK%>
<%@ page import=javasql*%>
<%@ page import=SessionCountSessionCounter %>
<html>
<head>
<meta httpequiv=ContentType content=text/html; charset=GBK>
<title>无标题文档</title>
<body bgcolor=#FFFFFF>
在线人数:<%=SessionCountergetActiveSessions()%>
</body>
</html>
看看可以实现不?我也是找到的我用了可以统计出来