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

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
 
 	com.kgc.listener.HellServlet  
  



jsp 页面

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

你可能感兴趣的:(JDBC)