您的位置:首页 > 其它

Servlet监听------------例子:统计当前在线人数

2012-12-06 17:47 471 查看
统计当前在线人数:

1、web.listener下的CountLineListener:

public class CountLineListener implements HttpSessionListener {

public void sessionCreated(HttpSessionEvent se) {

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

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

if(count==null){

count=1;

context.setAttribute("count",count);

}else{

count++;

context.setAttribute("count", count);

}

}

public void sessionDestroyed(HttpSessionEvent se) {

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

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

count--;

context.setAttribute("count", count);

}

}

2、index.jsp

<body>

当前网站人数:${applicationScope.count} <br>

<%

application.setAttribute("name","zhangsan");

application.setAttribute("name","lisi");

application.removeAttribute("name");

session.setAttribute("city","baoding");

session.setAttribute("city","beijin");

session.removeAttribute("city");

request.setAttribute("name1","aaa");

request.setAttribute("name1","bbb");

request.removeAttribute("name1");

%>

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