別人寫的,收藏下【統計在線人數】

HttpSessionListener有2個接口需要實現

sessionCreated //新建一個會話時候觸發也可以說是客戶端第一次和服務器交互時候觸發

sessionDestroyed //銷燬會話的時候 一般來說只有某個按鈕觸發進行銷燬 或者配置定時銷燬 ( 很多文獻中提到說瀏覽器關閉時候會銷燬 但是樓主通過各種現行主流瀏覽器測試效果不盡如人意)

HttpSessionAttributeListener有3個接口需要實現

attributeAdded //在session中添加對象時觸發此操作 籠統的說就是調用setAttribute這個方法時候會觸發的

attributeRemoved //修改、刪除session中添加對象時觸發此操作 籠統的說就是調用 removeAttribute這個方法時候會觸發的

attributeReplaced //在Session屬性被重新設置時

以下是一個統計在線會話數的功能,並且讓超時的自動銷燬

web.xml

[html] view plaincopy
  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 plaincopy
  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 plaincopy
[html] view plaincopy
  1.   

onlineListener.java

[java] view plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章