您的位置:首页 > 产品设计 > UI/UE

监听器--ServletRequestListener接口实现统计在线人数

2011-11-25 15:17 771 查看


MyServletRequestListener

案例:统计当前在线人数

OnlineListener

public class OnlineListener implements HttpSessionListener {

public void sessionCreated(HttpSessionEvent se) {

ServletContext context=se.getSession().getServletContext();

Integer count=(Integer)context.getAttribute("peopleOnline");

if(count==null){//如果是第一个用户ServletContext域中创建属性,并且赋值为1

count=1;

}else{

count++;

}

context.setAttribute("peopleOnline",count);

}

public void sessionDestroyed(HttpSessionEvent se) {

ServletContext context=se.getSession().getServletContext();

Integer count=(Integer)context.getAttribute("peopleOnline");

count--;

context.setAttribute("peopleOnline",count);

}

Index.jsp

<body>

当前在线人数为:${applicationScope.peopleOnline}

%>

</body>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: