别人写的,收藏下【统计在线人数】

HttpSessionListener有2个接口需要实现

sessionCreated //新建一个会话时候触发也可以说是客户端第一次和服务器交互时候触发

sessionDestroyed //销毁会话的时候 一般来说只有某个按钮触发进行销毁 或者配置定时销毁 ( 很多文献中提到说浏览器关闭时候会销毁 但是楼主通过各种现行主流浏览器测试效果不尽如人意)

HttpSessionAttributeListener有3个接口需要实现

attributeAdded //在session中添加对象时触发此操作 笼统的说就是调用setAttribute这个方法时候会触发的

attributeRemoved //修改、删除session中添加对象时触发此操作 笼统的说就是调用 removeAttribute这个方法时候会触发的

attributeReplaced //在Session属性被重新设置时

以下是一个统计在线会话数的功能,并且让超时的自动销毁

web.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6. <listener>
  7. <listener-class>
  8. org.xiosu.listener.onlineListener
  9. </listener-class>
  10. </listener>
  11. <!--默认的会话超时时间间隔,以分钟为单位 -->
  12. <session-config>
  13. <session-timeout>1</session-timeout>
  14. </session-config>
  15. <welcome-file-list>
  16. <welcome-file>index.jsp</welcome-file>
  17. </welcome-file-list>
  18. </web-app>
[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  6.     <listener>  
  7.         <listener-class>  
  8.             org.xiosu.listener.onlineListener  
  9.         </listener-class>  
  10.     </listener>  
  11.   
  12.   
  13.     <!--默认的会话超时时间间隔,以分钟为单位  -->  
  14.     <session-config>  
  15.         <session-timeout>1</session-timeout>  
  16.     </session-config>  
  17.     <welcome-file-list>  
  18.         <welcome-file>index.jsp</welcome-file>  
  19.     </welcome-file-list>  
  20.   
  21. </web-app>  
[html]  view plain copy
[html]  view plain copy
  1.   

onlineListener.java

[java]  view plain copy
  1. package org.xiosu.listener;
  2. import java.util.ArrayList;
  3. import javax.servlet.ServletContext;
  4. import javax.servlet.http.HttpSessionAttributeListener;
  5. import javax.servlet.http.HttpSessionBindingEvent;
  6. import javax.servlet.http.HttpSessionEvent;
  7. import javax.servlet.http.HttpSessionListener;
  8. public class onlineListener implements HttpSessionListener,
  9. HttpSessionAttributeListener {
  10. // 参数
  11. ServletContext sc;
  12. ArrayList list = new ArrayList();
  13. // 新建一个session时触发此操作
  14. public void sessionCreated(HttpSessionEvent se) {
  15. sc = se.getSession().getServletContext();
  16. System.out.println("新建一个session");
  17. }
  18. // 销毁一个session时触发此操作
  19. public void sessionDestroyed(HttpSessionEvent se) {
  20. System.out.println("销毁一个session");
  21. if (!list.isEmpty()) {
  22. list.remove((String) se.getSession().getAttribute("userName"));
  23. sc.setAttribute("list", list);
  24. }
  25. }
  26. // 在session中添加对象时触发此操作,在list中添加一个对象
  27. public void attributeAdded(HttpSessionBindingEvent sbe) {
  28. list.add((String) sbe.getValue());
  29. System.out.println(sbe.getValue());
  30. sc.setAttribute("list", list);
  31. }
  32. // 修改、删除session中添加对象时触发此操作
  33. public void attributeRemoved(HttpSessionBindingEvent arg0) {
  34. System.out.println("5555555");
  35. }
  36. public void attributeReplaced(HttpSessionBindingEvent arg0) {
  37. System.out.println("77777777");
  38. }
  39. }
[java]  view plain copy
  1. package org.xiosu.listener;  
  2.   
  3. import java.util.ArrayList;  
  4. import javax.servlet.ServletContext;  
  5. import javax.servlet.http.HttpSessionAttributeListener;  
  6. import javax.servlet.http.HttpSessionBindingEvent;  
  7. import javax.servlet.http.HttpSessionEvent;  
  8. import javax.servlet.http.HttpSessionListener;  
  9.   
  10. public class onlineListener implements HttpSessionListener,  
  11.         HttpSessionAttributeListener {  
  12.     // 参数  
  13.     ServletContext sc;  
  14.   
  15.     ArrayList list = new ArrayList();  
  16.   
  17.     // 新建一个session时触发此操作  
  18.     public void sessionCreated(HttpSessionEvent se) {  
  19.         sc = se.getSession().getServletContext();  
  20.         System.out.println("新建一个session");  
  21.     }  
  22.   
  23.     // 销毁一个session时触发此操作  
  24.     public void sessionDestroyed(HttpSessionEvent se) {  
  25.         System.out.println("销毁一个session");  
  26.         if (!list.isEmpty()) {  
  27.             list.remove((String) se.getSession().getAttribute("userName"));  
  28.             sc.setAttribute("list", list);  
  29.         }  
  30.     }  
  31.   
  32.     // 在session中添加对象时触发此操作,在list中添加一个对象  
  33.     public void attributeAdded(HttpSessionBindingEvent sbe) {  
  34.         list.add((String) sbe.getValue());  
  35.         System.out.println(sbe.getValue());  
  36.         sc.setAttribute("list", list);  
  37.     }  
  38.   
  39.     // 修改、删除session中添加对象时触发此操作  
  40.     public void attributeRemoved(HttpSessionBindingEvent arg0) {  
  41.           
  42.         System.out.println("5555555");  
  43.     }  
  44.   
  45.     public void attributeReplaced(HttpSessionBindingEvent arg0) {  
  46.         System.out.println("77777777");  
  47.     }  
  48. }  


index.jsp

[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme() + "://"
  5. + request.getServerName() + ":" + request.getServerPort()
  6. + path + "/";
  7. %>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <title>My JSP 'index.jsp' starting page</title>
  13. <meta http-equiv="pragma" content="no-cache">
  14. <meta http-equiv="cache-control" content="no-cache">
  15. <meta http-equiv="expires" content="0">
  16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  17. <meta http-equiv="description" content="This is my page">
  18. <!--
  19. <link rel="stylesheet" type="text/css" href="styles.css">
  20. -->
  21. </head>
  22. <body>
  23. <%
  24. session = request.getSession(false);
  25. if (session != null)
  26. session.invalidate();
  27. %>
  28. <form action="isOnline.jsp" method="post">
  29. 用户名:
  30. <input type="text" name="uName" />
  31. <input type="submit" value="上线">
  32. </body>
  33. </html>
[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3.     String path = request.getContextPath();  
  4.     String basePath = request.getScheme() + "://"  
  5.             + request.getServerName() + ":" + request.getServerPort()  
  6.             + path + "/";  
  7. %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.     <head>  
  12.         <base href="<%=basePath%>">  
  13.   
  14.         <title>My JSP 'index.jsp' starting page</title>  
  15.         <meta http-equiv="pragma" content="no-cache">  
  16.         <meta http-equiv="cache-control" content="no-cache">  
  17.         <meta http-equiv="expires" content="0">  
  18.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.         <meta http-equiv="description" content="This is my page">  
  20.         <!-- 
  21.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  22.     -->  
  23.     </head>  
  24.   
  25.     <body>  
  26.         <%  
  27.             session = request.getSession(false);  
  28.   
  29.             if (session != null)  
  30.                 session.invalidate();  
  31.         %>  
  32.         <form action="isOnline.jsp" method="post">  
  33.             用户名:  
  34.             <input type="text" name="uName" />  
  35.             <input type="submit" value="上线">  
  36.     </body>  
  37. </html>  


isOnline.jsp

[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'isOnline.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%
  22. session=request.getSession();
  23. session.setAttribute("userName",request.getParameter("uName"));
  24. response.sendRedirect("showOnline.jsp");
  25. %>
  26. </body>
  27. </html>
[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'isOnline.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.     <%  
  27.   
  28. session=request.getSession();  
  29.   
  30. session.setAttribute("userName",request.getParameter("uName"));  
  31.   
  32. response.sendRedirect("showOnline.jsp");  
  33. %>  
  34.   </body>  
  35. </html>  


showOnline.jsp

[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'showOnline.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%
  22. ArrayList showList=(ArrayList)(getServletContext().getAttribute("list"));
  23. out.print("在线人数 "+showList.size()+"<br>");
  24. for(int i=0;i<showList.size();i++){
  25. out.print(showList.get(i)+"在线"+"<br>");
  26. }
  27. %>
  28. <br>
  29. <a href="index.jsp">退出</a>
  30. </body>
  31. </html>

你可能感兴趣的:(浏览器,测试)