用监听器来实现网络在线人数

来源:互联网 发布:c语言函数调用的例子 编辑:程序博客网 时间:2024/04/28 05:39
监听器:
Java代码 复制代码 收藏代码
  1. package org.listenerdemo; 
  2. import java.util.*; 
  3. import javax.servlet.*; 
  4. import javax.servlet.http.*; 
  5. public class OnlineUserListimplements ServletContextListener,HttpSessionAttributeListener,HttpSessionListener 
  6.     private ServletContext app = null
  7.     public void contextInitialized(ServletContextEvent sce){ 
  8.         this.app = sce.getServletContext(); 
  9.         this.app.setAttribute("online",new TreeSet());//准备集合 
  10.     } 
  11.     public void contextDestroyed(ServletContextEvent sce){} 
  12.     public void attributeAdded(HttpSessionBindingEvent se){ 
  13.         Set all = (Set) this.app.getAttribute("online"); 
  14.         all.add(se.getValue()); 
  15.         this.app.setAttribute("online",all); 
  16.     }  
  17.     public void attributeRemoved(HttpSessionBindingEvent se){ 
  18.         Set all = (Set) this.app.getAttribute("online"); 
  19.         all.remove(se.getSession().getAttribute("userid")); 
  20.         this.app.setAttribute("online",all); 
  21.     }   
  22.     public void attributeReplaced(HttpSessionBindingEvent se){}  
  23.     public void sessionCreated(HttpSessionEvent se){}  
  24.     public void sessionDestroyed(HttpSessionEvent se){ 
  25.         Set all = (Set) this.app.getAttribute("online"); 
  26.         all.remove(se.getSession().getAttribute("userid")); 
  27.         this.app.setAttribute("online",all); 
  28.     }  
  29.   
  30.   
  31. /* web.xml 配置
  32.     <listener>
  33.         <listener-class>
  34.         org.listenerdemo.OnlineUserList
  35.         </listener-class>
  36.     </listener>
  37.     </servlet-mapping>
  38.     <session-config>
  39.         <session-timeout>1</session-timeout>
  40.     </session-config>
  41. */ 


登陆:
Java代码 复制代码 收藏代码
  1. <%@ page contentType="text/html;charset=GBK"%> 
  2. <%@ page import="java.util.*"%> 
  3. <html> 
  4. <head> 
  5.     <title>www.baidu.com</title> 
  6. </head> 
  7. <body> 
  8.     <form action="login.jsp" method="post"
  9.     用户ID: <input type="text" name="userid"
  10.     <input type="submit" value="登陆"
  11.     </form> 
  12.     <% 
  13.     request.setCharacterEncoding("GBK"); 
  14.     %> 
  15.     <% 
  16.         String userid = request.getParameter("userid"); 
  17.         if(!(userid==null ||"".equals(userid))){ 
  18.             session.setAttribute("userid",userid); 
  19.             response.sendRedirect("list.jsp"); 
  20.         } 
  21.     %> 
  22. </body> 
  23. </html> 


在线成员列表:
Java代码 复制代码 收藏代码
  1. <%@ page contentType="text/html;charset=GBK"%> 
  2. <%@ page import="java.util.*"%> 
  3. <html> 
  4. <head> 
  5.     <title>www.baidu.com</title> 
  6.      
  7. </head> 
  8. <body> 
  9. <% 
  10.     request.setCharacterEncoding("GBK"); 
  11. %> 
  12.     <% 
  13.         Set all = (Set)this.getServletContext().getAttribute("online"); 
  14.         Iterator iter = all.iterator(); 
  15.         while(iter.hasNext()){ 
  16.     %> 
  17.         <h3><%=iter.next()%></h3> 
  18.     <% 
  19.         } 
  20.     %> 
  21. </body> 
  22. </html> 


本页面用监听器实现了在线人员的列表显示,登陆后,session-timeou 为1分钟

在监听器中,实现了ServletContextListener,HttpSessionAttributeListener,HttpSessionListener 的个别方法,以实现人员的登陆与注销.

只在了解监听器(学习中.........)