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

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

实例:
OnlineListener.java:
view plaincopy to clipboardprint?

package mgc.listener.test;   

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class OnlineListener implements ServletContextListener,ServletContextAttributeListener,HttpSessionListener,HttpSessionAttributeListener {
    
    private ServletContext application = null ;
    private HttpSession session = null ;
    
    public void contextInitialized(ServletContextEvent sce) {
        
        //初始化一个application对象
        this.application=sce.getServletContext() ;
        //设置一个列表属性,用于保存在线用户名
        this.application.setAttribute("online", new ArrayList()) ;
    }
    
    public void contextDestroyed(ServletContextEvent sce) {
        
    }
    
    public void attributeAdded(ServletContextAttributeEvent scab) {
        
    }
    
    public void attributeRemoved(ServletContextAttributeEvent scab) {
        
    }
    
    public void attributeReplaced(ServletContextAttributeEvent scab) {
        
    }
    
    public void sessionCreated(HttpSessionEvent se) {
        
    }
    
    public void sessionDestroyed(HttpSessionEvent se) {
    
        //取得用户名列表
        List online=(List)this.application.getAttribute("online") ;
        //取得当前用户名
        String username=(String)se.getSession().getAttribute("username") ;
        //将此用户名从列表中删除
        online.remove(username) ;
        //将删除后的列表重新设置到application属性中
        this.application.setAttribute("online", online) ;
    }
    
    public void attributeAdded(HttpSessionBindingEvent se) {
        
        //取得用户名列表
        List online=(List)this.application.getAttribute("online") ;
        //将当前用户名添加到列表中
        online.add(se.getValue()) ;
        //将添加后的列表重新设置到application属性中
        this.application.setAttribute("online", online) ;
    }
    
    public void attributeRemoved(HttpSessionBindingEvent se) {
        
    }
    
    public void attributeReplaced(HttpSessionBindingEvent se) {
        
    }
}


web.xml:
view plaincopy to clipboardprint?
<listener>  
      <listener-class>mgc.listener.test.OnlineListener</listener-class>  
  </listener>  


sessionlistener.jsp:
view plaincopy to clipboardprint?
<%@ page contentType="text/html;charset=GB2312" %>   
<%@ page import="java.util.*" %>   
<html>   
  <head>   
    <title>sessionlistener</title>   
  </head>   
     
  <body>   
      <form action="onlinelistener.jsp" method="post" >   
          用户名:<input type="text" name="username"/>   
          <input type="submit" value="登录"/>   
          <a href="logout.jsp">注销</a>   
      </form>   
<%   
    String username=request.getParameter("username") ;   
    if(username != null) {   
       
        session.setAttribute("username",username) ;       
    }       
%>   
    <p>   
    <h3>在线用户:</h3>   
    <hr>   
<%   
    List online = (List)getServletContext().getAttribute("online") ;   
    Iterator iter = online.iterator();   
    while(iter.hasNext()) {   
       
%>   
    <li><%=iter.next() %></li>   
<%   
    }   
%>   
  </body>   
</html>  



logout.jsp:
view plaincopy to clipboardprint?
<%@ page contentType="text/html;charset=GB2312" %>  
<html>  
  <head>  
    <title>logout</title>  
  </head>  
     
  <body>  
<%   
    session.invalidate() ;   
    response.setHeader("refresh","3;URL=onlinelistener.jsp") ;   
%>  
    <h3>注销成功!</h3>  
    3秒后自动返回登录页面<br>  
    如果没有跳转,请点<a href="onlinelistener.jsp">这里</a>  
  </body>  
</html>  

<%@ page contentType="text/html;charset=GB2312" %>
<html>
  <head>
    <title>logout</title>
  </head>
  
  <body>
<%
    session.invalidate() ;
    response.setHeader("refresh","3;URL=onlinelistener.jsp") ;
%>
    <h3>注销成功!</h3>
    3秒后自动返回登录页面<br>
    如果没有跳转,请点<a href="onlinelistener.jsp">这里</a>
  </body>
</html>


    相关文章:



发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。