使用Servlet监听器统计在线用户
来源:互联网 发布:数据恢复软件 知乎 编辑:程序博客网 时间:2024/05/08 15:07
使用Servlet监听器可以统计在线用户,具体实现方法如下:
1.通过ServletContext监听初始化一个application对象,保存在线用户列表;
2.通过Session监听当用户登录成功设置Session属性时将用户名保存在列表中;
3.通过Session监听当用户注销登录时将用户名从列表中删除。
OnlineListener.java:
- 1. package mgc.listener.test;
- 2.
- 3. import java.util.*;
- 4. import javax.servlet.*;
- 5. import javax.servlet.http.*;
- 6.
- 7. publicclass OnlineListener implements ServletContextListener,ServletContextAttributeListener,HttpSessionListener,HttpSessionAttributeListener {
- 8.
- 9. private ServletContext application =null ;
- 10. private HttpSession session =null ;
- 11.
- 12. publicvoid contextInitialized(ServletContextEvent sce) {
- 13.
- 14. //初始化一个application对象
- 15. this.application=sce.getServletContext() ;
- 16. //设置一个列表属性,用于保存在线用户名
- 17. this.application.setAttribute("online",new ArrayList()) ;
- 18. }
- 19.
- 20. publicvoid contextDestroyed(ServletContextEvent sce) {
- 21.
- 22. }
- 23.
- 24. publicvoid attributeAdded(ServletContextAttributeEvent scab) {
- 25.
- 26. }
- 27.
- 28. publicvoid attributeRemoved(ServletContextAttributeEvent scab) {
- 29.
- 30. }
- 31.
- 32. publicvoid attributeReplaced(ServletContextAttributeEvent scab) {
- 33.
- 34. }
- 35.
- 36. publicvoid sessionCreated(HttpSessionEvent se) {
- 37.
- 38. }
- 39.
- 40. publicvoid sessionDestroyed(HttpSessionEvent se) {
- 41.
- 42. //取得用户名列表
- 43. List online=(List)this.application.getAttribute("online") ;
- 44. //取得当前用户名
- 45. String username=(String)se.getSession().getAttribute("username") ;
- 46. //将此用户名从列表中删除
- 47. online.remove(username) ;
- 48. //将删除后的列表重新设置到application属性中
- 49. this.application.setAttribute("online", online) ;
- 50. }
- 51.
- 52. publicvoid attributeAdded(HttpSessionBindingEvent se) {
- 53.
- 54. //取得用户名列表
- 55. List online=(List)this.application.getAttribute("online") ;
- 56. //将当前用户名添加到列表中
- 57. online.add(se.getValue()) ;
- 58. //将添加后的列表重新设置到application属性中
- 59. this.application.setAttribute("online", online) ;
- 60. }
- 61.
- 62. publicvoid attributeRemoved(HttpSessionBindingEvent se) {
- 63.
- 64. }
- 65.
- 66. publicvoid attributeReplaced(HttpSessionBindingEvent se) {
- 67.
- 68. }
- 69. }
web.xml:
- <listener>
- <listener-class>mgc.listener.test.OnlineListener</listener-class>
- </listener>
sessionlistener.jsp:
- 1. <%@ page contentType="text/html;charset=GB2312" %>
- 2. <%@ page import="java.util.*" %>
- 3. <html>
- 4. <head>
- 5. <title>sessionlistener</title>
- 6. </head>
- 7.
- 8. <body>
- 9. <form action="onlinelistener.jsp" method="post" >
- 10. 用户名:<input type="text" name="username"/>
- 11. <input type="submit" value="登录"/>
- 12. <a href="logout.jsp" mce_href="logout.jsp">注销</a>
- 13. </form>
- 14. <%
- 15. String username=request.getParameter("username") ;
- 16. if(username !=null) {
- 17.
- 18. session.setAttribute("username",username) ;
- 19. }
- 20. %>
- 21. <p>
- 22. <h3>在线用户:</h3>
- 23. <hr>
- 24. <%
- 25. List online = (List)getServletContext().getAttribute("online") ;
- 26. Iterator iter = online.iterator();
- 27. while(iter.hasNext()) {
- 28.
- 29. %>
- 30. <li><%=iter.next() %></li>
- 31. <%
- 32. }
- 33. %>
- 34. </body>
- 35. </html>
logout.jsp:
- 1. <%@ page contentType="text/html;charset=GB2312" %>
- 2. <html>
- 3. <head>
- 4. <title>logout</title>
- 5. </head>
- 6.
- 7. <body>
- 8. <%
- 9. session.invalidate() ;
- 10. response.setHeader("refresh","3;URL=onlinelistener.jsp") ;
- 11. %>
- 12. <h3>注销成功!</h3>
- 13. 3秒后自动返回登录页面<br>
- 14. 如果没有跳转,请点<a href="onlinelistener.jsp" mce_href="onlinelistener.jsp">这里</a>
- 15. </body>
- 16. </html>
- 使用Servlet监听器统计在线用户
- 使用Servlet监听器统计在线用户
- Servlet监听器及在线用户
- Servlet监听器的简单使用实现统计在线人数
- 如何使用Servlet监听器实现网站在线人数统计
- java使用监听器实现对在线用户的统计
- (转)Servlet监听器统计在线人数
- Servlet监听器实现网站在线人数统计
- Servlet监听器(统计在线人数案例)
- Servlet监听器,统计网站在线人数实例
- 监听器,统计在线用户及人数
- HttpSessionListener监听器,实现在线用户统计
- 基于Servlet监听器的在线用户计数器
- 对jsp的一个小结(8)使用servlet控制新闻管理、使用过滤器(解决乱码)和监听器(统计在线用户人数)
- 使用java 监听器实现在线人数统计
- 使用监听器统计当前的在线用户数
- Servlet监听器简介与实现在线人数统计
- 用Servlet监听器实现对在线人数的统计
- 从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响
- 如何在搜索结果出来之前,让页面显示“等待中...” _asp.net技巧
- Android NDK: 用 NDK 编写 JNI
- MM基本配置(二)
- PHP基础教程-PHP简介
- 使用Servlet监听器统计在线用户
- alumnus system - 查询后分页显示的思想
- 博弈
- Linux学习笔记之服务器篇(二)
- shell生成seq
- 安装develop
- C++中的单例模式及按需释放模型(一)
- Could not find destination factory for transport解决方法
- 双向多对多映射(参考张龙老师的)