统计在线人数和登录人数 | 您所在的位置:网站首页 › 查询网站访问人数的软件 › 统计在线人数和登录人数 |
思想:
1:统计在线人数。 只是访问了这个网页的人员。 一个session的创建就是一个在线人数+1。让所有人都可以看到。把在线人数到ServletContext中。 2:统计登录人数。 是指输出的正确的用户名和密码以后用户的数量。 登录以后:将用户以xxx为key值(如以user为key)放到session的属性中去。监听器监听到此session属性的变化, 当属性名为user时,登录人数+1并放到ServletContext中 第一步:先开发统计在线人数一个session的创建就是一个在线人数+1。让所有人都可以看到。把在线人数放到ServletContext中。
思想:实现HttpSessionListener,在sessionCreated的方法中+1,sessionDestroyed的方法中-1. publicclass SListener implements HttpSessionListener { /** * 当有人访问时,就会有一个session被创建,监听到session创建,count就+1 */ publicvoidsessionCreated(HttpSessionEvent se) { //从application中获取已经存在的数量 ServletContext app =se.getSession().getServletContext(); AtomicInteger count = (AtomicInteger)app.getAttribute("online"); if(count==null){ count= newAtomicInteger(1); }else{ count.addAndGet(1); } //再把count放到application中去 app.setAttribute("online",count); publicvoidsessionDestroyed(HttpSessionEvent se) { ServletContextapp = se.getSession().getServletContext(); AtomicIntegercount = (AtomicInteger) app.getAttribute("online"); count.addAndGet(-1); app.setAttribute("online",count); } }
第二步:什么是登录人数(用session的属性监听) 是指输入了用户名和密码的用户。 登录以后:将用户以xxx为key值(如以user为key)放到session的属性中去。 就可以监听登录人数。 应该是实现一个HttpSessionAttributeListner监听器。 监听属性的变化:
1:开发登录页面 主页: 在线人数:${online.get()}
登录人数:${logined.get()}
|
CopyRight 2018-2019 实验室设备网 版权所有 |