如何通过监听器统计在线人数?

public class HellServlet implements HttpSessionListener{

	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("HttpSession被建立");
		ServletContext application=arg0.getSession().getServletContext();
		             Integer count = (Integer)application.getAttribute("count");
		             if(count==null){
		            	 count=1;
		            	 application.setAttribute("count", count);
		             }else{
		            	 count++;
		            	 application.setAttribute("count", count);
		             }
	}


	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("HttpSession被销毁");
		ServletContext application=arg0.getSession().getServletContext();
        Integer count = (Integer)application.getAttribute("count");
        count--;
        application.setAttribute("count", count);
	}



}




//配置web.xml
 <listener>
 	<listener-class>com.kgc.listener.HellServlet </listener-class> 
  </listener>



jsp 页面

<%
    Integer count=(Integer)application.getAttribute("count");
    out.println(“当前在线人数:”+count);
    %>
<--下面等价getAttribute()-->
当前在线用户人数:${applicationScope.count}人
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值