session监听器(可以统计网站在线人数)

转自: http://stephen830.iteye.com/blog/338291
在系统运行中,有时候我们想了解下当前使用系统的用户,有多少人在使用系统?具体是哪些人在使用系统?
这时候,就可以通过session监听器(javax.servlet.http.HttpSessionAttributeListener 接口)的功能来实现。当用户登录系统的时候,通过特定的session属性记录下用户的登录情况。

整个过程可以分为以下4步:

第1步 准备session监听器处理类。
(如下面的SessionListener.java)

Java代码 复制代码
  1. /*  
  2.  * @(#)SessionListener.java 1.00    2009/02/20  
  3.  * CopyRight(C) stephen(zhoujianqiang AT gmail DOT com) 2009-2014, All rights reserved.  
  4.  */  
  5. package com.stephen.filter;   
  6.   
  7. import java.util.ArrayList;   
  8. import java.util.List;   
  9. import javax.servlet.http.HttpSessionAttributeListener;   
  10. import javax.servlet.http.HttpSessionBindingEvent;   
  11.   
  12. /**  
  13.  * session监听器. <br>  
  14.  * 在WEB容器的web.xml中添加本监听器的调用,具体格式如下:(其中的"[","]"分别用" <",">"替换) <br>  
  15.  *   
  16.  * <pre>  
  17.  *   
  18.  *    [web-app]  
  19.  *    [filter]  
  20.  *    ...  
  21.  *    [/filter]  
  22.  *    [filter-mapping]  
  23.  *    ...  
  24.  *    [/filter-mapping]  
  25.  *    ...  
  26.  *    [listener][listener-class]com.stephen.filter.SessionListener[/listener-class][/listener]  
  27.  *    ...  
  28.  *    [servlet]  
  29.  *    ...  
  30.  *    [/servlet]  
  31.  *    ...  
  32.  *    [/web-app]  
  33.  *    
  34.  * </pre>  
  35.  *   
  36.  * 注意在web.xml中配置的位置. <br>  
  37.  *   
  38.  * @author stephen  
  39.  * @version 1.00  
  40.  * @see javax.servlet.http.HttpSessionAttributeListener  
  41.  */  
  42. public class SessionListener implements HttpSessionAttributeListener {   
  43.     /**  
  44.      * 定义监听的session属性名.  
  45.      */  
  46.     public final static String LISTENER_NAME = "_login";   
  47.        
  48.     /**  
  49.      * 定义存储客户登录session的集合.  
  50.      */  
  51.     private static List sessions = new ArrayList();   
  52.   
  53.     /**  
  54.      * 加入session时的监听方法.  
  55.      *   
  56.      * @param HttpSessionBindingEvent  
  57.      *            session事件  
  58.      */  
  59.     public void attributeAdded(HttpSessionBindingEvent sbe) {   
  60.         if (LISTENER_NAME.equals(sbe.getName())) {   
  61.             sessions.add(sbe.getValue());   
  62.         }   
  63.     }   
  64.   
  65.     /**  
  66.      * session失效时的监听方法.  
  67.      *   
  68.      * @param HttpSessionBindingEvent  
  69.      *            session事件  
  70.      */  
  71.     public void attributeRemoved(HttpSessionBindingEvent sbe) {   
  72.         if (LISTENER_NAME.equals(sbe.getName())) {   
  73.             sessions.remove(sbe.getValue());   
  74.         }   
  75.     }   
  76.   
  77.     /**  
  78.      * session覆盖时的监听方法.  
  79.      *   
  80.      * @param HttpSessionBindingEvent  
  81.      *            session事件  
  82.      */  
  83.     public void attributeReplaced(HttpSessionBindingEvent sbe) {   
  84.     }   
  85.   
  86.     /**  
  87.      * 返回客户登录session的集合.  
  88.      *   
  89.      * @return  
  90.      */  
  91.     public static List getSessions() {   
  92.         return sessions;   
  93.     }   
  94. }  
Java代码 复制代码
  1. /*  
  2.  * @(#)SessionListener.java 1.00    2009/02/20  
  3.  * CopyRight(C) stephen(zhoujianqiang AT gmail DOT com) 2009-2014, All rights reserved.  
  4.  */  
  5. package com.stephen.filter;   
  6.   
  7. import java.util.ArrayList;   
  8. import java.util.List;   
  9. import javax.servlet.http.HttpSessionAttributeListener;   
  10. import javax.servlet.http.HttpSessionBindingEvent;   
  11.   
  12. /**  
  13.  * session监听器. <br>  
  14.  * 在WEB容器的web.xml中添加本监听器的调用,具体格式如下:(其中的"[","]"分别用" <",">"替换) <br>  
  15.  *   
  16.  * <pre>  
  17.  *   
  18.  *    [web-app]  
  19.  *    [filter]  
  20.  *    ...  
  21.  *    [/filter]  
  22.  *    [filter-mapping]  
  23.  *    ...  
  24.  *    [/filter-mapping]  
  25.  *    ...  
  26.  *    [listener][listener-class]com.stephen.filter.SessionListener[/listener-class][/listener]  
  27.  *    ...  
  28.  *    [servlet]  
  29.  *    ...  
  30.  *    [/servlet]  
  31.  *    ...  
  32.  *    [/web-app]  
  33.  *    
  34.  * </pre>  
  35.  *   
  36.  * 注意在web.xml中配置的位置. <br>  
  37.  *   
  38.  * @author stephen  
  39.  * @version 1.00  
  40.  * @see javax.servlet.http.HttpSessionAttributeListener  
  41.  */  
  42. public class SessionListener implements HttpSessionAttributeListener {   
  43.     /**  
  44.      * 定义监听的session属性名.  
  45.      */  
  46.     public final static String LISTENER_NAME = "_login";   
  47.        
  48.     /**  
  49.      * 定义存储客户登录session的集合.  
  50.      */  
  51.     private static List sessions = new ArrayList();   
  52.   
  53.     /**  
  54.      * 加入session时的监听方法.  
  55.      *   
  56.      * @param HttpSessionBindingEvent  
  57.      *            session事件  
  58.      */  
  59.     public void attributeAdded(HttpSessionBindingEvent sbe) {   
  60.         if (LISTENER_NAME.equals(sbe.getName())) {   
  61.             sessions.add(sbe.getValue());   
  62.         }   
  63.     }   
  64.   
  65.     /**  
  66.      * session失效时的监听方法.  
  67.      *   
  68.      * @param HttpSessionBindingEvent  
  69.      *            session事件  
  70.      */  
  71.     public void attributeRemoved(HttpSessionBindingEvent sbe) {   
  72.         if (LISTENER_NAME.equals(sbe.getName())) {   
  73.             sessions.remove(sbe.getValue());   
  74.         }   
  75.     }   
  76.   
  77.     /**  
  78.      * session覆盖时的监听方法.  
  79.      *   
  80.      * @param HttpSessionBindingEvent  
  81.      *            session事件  
  82.      */  
  83.     public void attributeReplaced(HttpSessionBindingEvent sbe) {   
  84.     }   
  85.   
  86.     /**  
  87.      * 返回客户登录session的集合.  
  88.      *   
  89.      * @return  
  90.      */  
  91.     public static List getSessions() {   
  92.         return sessions;   
  93.     }   
  94. }  
/*
 * @(#)SessionListener.java	1.00	2009/02/20
 * CopyRight(C) stephen(zhoujianqiang AT gmail DOT com) 2009-2014, All rights reserved.
 */
package com.stephen.filter;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * session监听器. <br>
 * 在WEB容器的web.xml中添加本监听器的调用,具体格式如下:(其中的"[","]"分别用" <",">"替换) <br>
 * 
 * <pre>
 * 
 *    [web-app]
 *    [filter]
 *    ...
 *    [/filter]
 *    [filter-mapping]
 *    ...
 *    [/filter-mapping]
 *    ...
 *    [listener][listener-class]com.stephen.filter.SessionListener[/listener-class][/listener]
 *    ...
 *    [servlet]
 *    ...
 *    [/servlet]
 *    ...
 *    [/web-app]
 *  
 * </pre>
 * 
 * 注意在web.xml中配置的位置. <br>
 * 
 * @author stephen
 * @version 1.00
 * @see javax.servlet.http.HttpSessionAttributeListener
 */
public class SessionListener implements HttpSessionAttributeListener {
	/**
	 * 定义监听的session属性名.
	 */
	public final static String LISTENER_NAME = "_login";
	
	/**
	 * 定义存储客户登录session的集合.
	 */
	private static List sessions = new ArrayList();

	/**
	 * 加入session时的监听方法.
	 * 
	 * @param HttpSessionBindingEvent
	 *            session事件
	 */
	public void attributeAdded(HttpSessionBindingEvent sbe) {
		if (LISTENER_NAME.equals(sbe.getName())) {
			sessions.add(sbe.getValue());
		}
	}

	/**
	 * session失效时的监听方法.
	 * 
	 * @param HttpSessionBindingEvent
	 *            session事件
	 */
	public void attributeRemoved(HttpSessionBindingEvent sbe) {
		if (LISTENER_NAME.equals(sbe.getName())) {
			sessions.remove(sbe.getValue());
		}
	}

	/**
	 * session覆盖时的监听方法.
	 * 
	 * @param HttpSessionBindingEvent
	 *            session事件
	 */
	public void attributeReplaced(HttpSessionBindingEvent sbe) {
	}

	/**
	 * 返回客户登录session的集合.
	 * 
	 * @return
	 */
	public static List getSessions() {
		return sessions;
	}
}


注意其中的_login就是设定的特殊session属性,当然你可以改成别的名字。

第2步 将session监听器配置到web.xml中.(参考如下的web.xml配置)

Xml代码 复制代码
  1. <?xml version="1.0" ?>  
  2. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">  
  3. <web-app>  
  4.            
  5.     <listener><listener-class>com.stephen.filter.SessionListener</listener-class></listener>  
  6.   
  7.     <welcome-file-list>  
  8.         <welcome-file>index.html</welcome-file>  
  9.     </welcome-file-list>  
  10.   
  11. </web-app>  
Xml代码 复制代码
  1. <?xml version="1.0" ?>  
  2. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">  
  3. <web-app>  
  4.            
  5.     <listener><listener-class>com.stephen.filter.SessionListener</listener-class></listener>  
  6.   
  7.     <welcome-file-list>  
  8.         <welcome-file>index.html</welcome-file>  
  9.     </welcome-file-list>  
  10.   
  11. </web-app>  
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
		
	<listener><listener-class>com.stephen.filter.SessionListener</listener-class></listener>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>

</web-app>



上面的 <listener><listener-class>com.stephen.filter.SessionListener</listener-class></listener> 就是配置session监听器的。

在你的web.xml配置文件中添加上这一行就可以了(要注意添加的位置)。

第3步 当用户登录时监听用户。

当用户成功登录后执行下面的代码。

Java代码 复制代码
  1. //记入session监听器   
  2. session.setAttribute(com.stephen.filter.SessionListener.LISTENER_NAME,new OnlineSession(request.getRemoteAddr(),userName,new Date().toString()));  
Java代码 复制代码
  1. //记入session监听器   
  2. session.setAttribute(com.stephen.filter.SessionListener.LISTENER_NAME,new OnlineSession(request.getRemoteAddr(),userName,new Date().toString()));  
//记入session监听器
session.setAttribute(com.stephen.filter.SessionListener.LISTENER_NAME,new OnlineSession(request.getRemoteAddr(),userName,new Date().toString()));


注意在上面的代码中使用了新的OnlineSession类,OnlineSession类封装了登录用户的信息(如用户ip,用户名,登录时间).

OnlineSession具体的内容如下:

Java代码 复制代码
  1. /*  
  2.  * @(#)OnlineSession.java   1.00    2009/02/20  
  3.  * CopyRight(C) stephen(zhoujianqiang AT gmail DOT com) 2009-2014, All rights reserved.  
  4.  */  
  5. package com.stephen.filter;   
  6.   
  7. /**  
  8.  * 客户session信息.  
  9.  *   
  10.  * @author  stephen  
  11.  * @version 1.0.0  
  12.  */  
  13. public final class OnlineSession {   
  14.        
  15.        
  16.     /**  
  17.      * 客户计算机的ip.  
  18.      */  
  19.     private String ip = null;   
  20.     /**  
  21.      * 客户登录名.  
  22.      */  
  23.     private String loginId = null;   
  24.     /**  
  25.      * 客户登录系统时间.  
  26.      */  
  27.     private String onlineTime = null;   
  28.        
  29.     /**  
  30.      * 构造器.  
  31.      * @param ip  
  32.      * @param loginId  
  33.      * @param onlineTime  
  34.      */  
  35.     public OnlineSession(String ip,String loginId,String onlineTime){   
  36.         this.ip=ip;   
  37.         this.loginId=loginId;   
  38.         this.onlineTime=onlineTime;   
  39.     }   
  40.        
  41.     /**  
  42.      * @return Returns the ip.  
  43.      */  
  44.     public String getIp() {   
  45.         return ip;   
  46.     }   
  47.     /**  
  48.      * @param ip The ip to set.  
  49.      */  
  50.     public void setIp(String ip) {   
  51.         this.ip = ip;   
  52.     }   
  53.     /**  
  54.      * @return Returns the loginId.  
  55.      */  
  56.     public String getLoginId() {   
  57.         return loginId;   
  58.     }   
  59.     /**  
  60.      * @param loginId The loginId to set.  
  61.      */  
  62.     public void setLoginId(String loginId) {   
  63.         this.loginId = loginId;   
  64.     }   
  65.     /**  
  66.      * @return Returns the onlineTime.  
  67.      */  
  68.     public String getOnlineTime() {   
  69.         return onlineTime;   
  70.     }   
  71.     /**  
  72.      * @param onlineTime The onlineTime to set.  
  73.      */  
  74.     public void setOnlineTime(String onlineTime) {   
  75.         this.onlineTime = onlineTime;   
  76.     }   
  77. }  
Java代码 复制代码
  1. /*  
  2.  * @(#)OnlineSession.java   1.00    2009/02/20  
  3.  * CopyRight(C) stephen(zhoujianqiang AT gmail DOT com) 2009-2014, All rights reserved.  
  4.  */  
  5. package com.stephen.filter;   
  6.   
  7. /**  
  8.  * 客户session信息.  
  9.  *   
  10.  * @author  stephen  
  11.  * @version 1.0.0  
  12.  */  
  13. public final class OnlineSession {   
  14.        
  15.        
  16.     /**  
  17.      * 客户计算机的ip.  
  18.      */  
  19.     private String ip = null;   
  20.     /**  
  21.      * 客户登录名.  
  22.      */  
  23.     private String loginId = null;   
  24.     /**  
  25.      * 客户登录系统时间.  
  26.      */  
  27.     private String onlineTime = null;   
  28.        
  29.     /**  
  30.      * 构造器.  
  31.      * @param ip  
  32.      * @param loginId  
  33.      * @param onlineTime  
  34.      */  
  35.     public OnlineSession(String ip,String loginId,String onlineTime){   
  36.         this.ip=ip;   
  37.         this.loginId=loginId;   
  38.         this.onlineTime=onlineTime;   
  39.     }   
  40.        
  41.     /**  
  42.      * @return Returns the ip.  
  43.      */  
  44.     public String getIp() {   
  45.         return ip;   
  46.     }   
  47.     /**  
  48.      * @param ip The ip to set.  
  49.      */  
  50.     public void setIp(String ip) {   
  51.         this.ip = ip;   
  52.     }   
  53.     /**  
  54.      * @return Returns the loginId.  
  55.      */  
  56.     public String getLoginId() {   
  57.         return loginId;   
  58.     }   
  59.     /**  
  60.      * @param loginId The loginId to set.  
  61.      */  
  62.     public void setLoginId(String loginId) {   
  63.         this.loginId = loginId;   
  64.     }   
  65.     /**  
  66.      * @return Returns the onlineTime.  
  67.      */  
  68.     public String getOnlineTime() {   
  69.         return onlineTime;   
  70.     }   
  71.     /**  
  72.      * @param onlineTime The onlineTime to set.  
  73.      */  
  74.     public void setOnlineTime(String onlineTime) {   
  75.         this.onlineTime = onlineTime;   
  76.     }   
  77. }  
/*
 * @(#)OnlineSession.java	1.00	2009/02/20
 * CopyRight(C) stephen(zhoujianqiang AT gmail DOT com) 2009-2014, All rights reserved.
 */
package com.stephen.filter;

/**
 * 客户session信息.
 * 
 * @author  stephen
 * @version 1.0.0
 */
public final class OnlineSession {
    
    
    /**
     * 客户计算机的ip.
     */
    private String ip = null;
    /**
     * 客户登录名.
     */
    private String loginId = null;
    /**
     * 客户登录系统时间.
     */
    private String onlineTime = null;
    
    /**
     * 构造器.
     * @param ip
     * @param loginId
     * @param onlineTime
     */
    public OnlineSession(String ip,String loginId,String onlineTime){
    	this.ip=ip;
    	this.loginId=loginId;
    	this.onlineTime=onlineTime;
    }
    
	/**
	 * @return Returns the ip.
	 */
	public String getIp() {
		return ip;
	}
	/**
	 * @param ip The ip to set.
	 */
	public void setIp(String ip) {
		this.ip = ip;
	}
	/**
	 * @return Returns the loginId.
	 */
	public String getLoginId() {
		return loginId;
	}
	/**
	 * @param loginId The loginId to set.
	 */
	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}
	/**
	 * @return Returns the onlineTime.
	 */
	public String getOnlineTime() {
		return onlineTime;
	}
	/**
	 * @param onlineTime The onlineTime to set.
	 */
	public void setOnlineTime(String onlineTime) {
		this.onlineTime = onlineTime;
	}
}



第4步 显示在线用户的情况。

可以直接通过 SessionListener.getSessions()方法来取得所有在线的用户。

Html代码 复制代码
  1. <%@ page contentType="text/html;charset=UTF-8"%>  
  2. <%@ page import="java.util.List"%>  
  3. <%@ page import="java.util.Iterator"%>  
  4. <%@ page import="com.stephen.filter.SessionListener"%>  
  5. <%@ page import="com.stephen.filter.OnlineSession"%>  
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  7. <html><head><title>Online session Query</title>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  9. </head><body bgcolor="#e5ecf9" topmargin="5px" leftmargin="5px" rightmargin="5px">  
  10. <%   
  11.     List sessions = SessionListener.getSessions();   
  12.     String pageErrorInfo = null;   
  13.     try{   
  14. %>  
  15.         <h5>Online Employee</h5>  
  16.            
  17.         <table width="100%" align="center" cellspacing="1" cellpadding="4" border="0">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值