使用Servlet监听器统计在线用户

来源:互联网 发布:数据恢复软件 知乎 编辑:程序博客网 时间:2024/05/08 15:07

使用Servlet监听器可以统计在线用户,具体实现方法如下:
1.通过ServletContext监听初始化一个application对象,保存在线用户列表;
2.通过Session监听当用户登录成功设置Session属性时将用户名保存在列表中;
3.通过Session监听当用户注销登录时将用户名从列表中删除。

OnlineListener.java:

view plaincopy to clipboardprint?
  1. 1. package mgc.listener.test;   
  2.    2.    
  3.    3. import java.util.*;   
  4.    4. import javax.servlet.*;   
  5.    5. import javax.servlet.http.*;   
  6.    6.    
  7.    7. publicclass OnlineListener implements ServletContextListener,ServletContextAttributeListener,HttpSessionListener,HttpSessionAttributeListener {   
  8.    8.        
  9.    9.     private ServletContext application =null ;   
  10.   10.     private HttpSession session =null ;   
  11.   11.        
  12.   12.     publicvoid contextInitialized(ServletContextEvent sce) {   
  13.   13.            
  14.   14.         //初始化一个application对象  
  15.   15.         this.application=sce.getServletContext() ;   
  16.   16.         //设置一个列表属性,用于保存在线用户名  
  17.   17.         this.application.setAttribute("online",new ArrayList()) ;   
  18.   18.     }   
  19.   19.        
  20.   20.     publicvoid contextDestroyed(ServletContextEvent sce) {   
  21.   21.            
  22.   22.     }   
  23.   23.        
  24.   24.     publicvoid attributeAdded(ServletContextAttributeEvent scab) {   
  25.   25.            
  26.   26.     }   
  27.   27.        
  28.   28.     publicvoid attributeRemoved(ServletContextAttributeEvent scab) {   
  29.   29.            
  30.   30.     }   
  31.   31.        
  32.   32.     publicvoid attributeReplaced(ServletContextAttributeEvent scab) {   
  33.   33.            
  34.   34.     }   
  35.   35.        
  36.   36.     publicvoid sessionCreated(HttpSessionEvent se) {   
  37.   37.            
  38.   38.     }   
  39.   39.        
  40.   40.     publicvoid sessionDestroyed(HttpSessionEvent se) {   
  41.   41.        
  42.   42.         //取得用户名列表  
  43.   43.         List online=(List)this.application.getAttribute("online") ;   
  44.   44.         //取得当前用户名  
  45.   45.         String username=(String)se.getSession().getAttribute("username") ;   
  46.   46.         //将此用户名从列表中删除  
  47.   47.         online.remove(username) ;   
  48.   48.         //将删除后的列表重新设置到application属性中  
  49.   49.         this.application.setAttribute("online", online) ;   
  50.   50.     }   
  51.   51.        
  52.   52.     publicvoid attributeAdded(HttpSessionBindingEvent se) {   
  53.   53.            
  54.   54.         //取得用户名列表  
  55.   55.         List online=(List)this.application.getAttribute("online") ;   
  56.   56.         //将当前用户名添加到列表中  
  57.   57.         online.add(se.getValue()) ;   
  58.   58.         //将添加后的列表重新设置到application属性中  
  59.   59.         this.application.setAttribute("online", online) ;   
  60.   60.     }   
  61.   61.        
  62.   62.     publicvoid attributeRemoved(HttpSessionBindingEvent se) {   
  63.   63.            
  64.   64.     }   
  65.   65.        
  66.   66.     publicvoid attributeReplaced(HttpSessionBindingEvent se) {   
  67.   67.            
  68.   68.     }   
  69.   69. }   

web.xml:

view plaincopy to clipboardprint?
  1. <listener>   
  2. <listener-class>mgc.listener.test.OnlineListener</listener-class>   
  3. </listener>  

sessionlistener.jsp:

view plaincopy to clipboardprint?
  1. 1. <%@ page contentType="text/html;charset=GB2312" %>   
  2.    2. <%@ page import="java.util.*" %>   
  3.    3. <html>   
  4.    4.   <head>   
  5.    5.     <title>sessionlistener</title>   
  6.    6.   </head>   
  7.    7.      
  8.    8.   <body>   
  9.    9.       <form action="onlinelistener.jsp" method="post" >   
  10.   10.           用户名:<input type="text" name="username"/>   
  11.   11.           <input type="submit" value="登录"/>   
  12.   12.           <a href="logout.jsp" mce_href="logout.jsp">注销</a>   
  13.   13.       </form>   
  14.   14. <%   
  15.   15.     String username=request.getParameter("username") ;   
  16.   16.     if(username !=null) {   
  17.   17.        
  18.   18.         session.setAttribute("username",username) ;       
  19.   19.     }       
  20.   20. %>   
  21.   21.     <p>   
  22.   22.     <h3>在线用户:</h3>   
  23.   23.     <hr>   
  24.   24. <%   
  25.   25.     List online = (List)getServletContext().getAttribute("online") ;   
  26.   26.     Iterator iter = online.iterator();   
  27.   27.     while(iter.hasNext()) {   
  28.   28.        
  29.   29. %>   
  30.   30.     <li><%=iter.next() %></li>   
  31.   31. <%   
  32.   32.     }   
  33.   33. %>   
  34.   34.   </body>   
  35.   35. </html>   

logout.jsp:

view plaincopy to clipboardprint?
  1. 1. <%@ page contentType="text/html;charset=GB2312" %>   
  2.    2. <html>   
  3.    3.   <head>   
  4.    4.     <title>logout</title>   
  5.    5.   </head>   
  6.    6.      
  7.    7.   <body>   
  8.    8. <%   
  9.    9.     session.invalidate() ;   
  10.   10.     response.setHeader("refresh","3;URL=onlinelistener.jsp") ;   
  11.   11. %>   
  12.   12.     <h3>注销成功!</h3>   
  13.   13.     3秒后自动返回登录页面<br>   
  14.   14.     如果没有跳转,请点<a href="onlinelistener.jsp" mce_href="onlinelistener.jsp">这里</a>   
  15.   15.   </body>   
  16.   16. </html>   

原创粉丝点击