session监听器 统计在线人数 1

★★ 本篇为原创,需要引用转载的朋友请注明:《 http://stephen830.javaeye.com/blog/338291 》 谢谢支持! ★★★

在系统运行中,有时候我们想了解下当前使用系统的用户,有多少人在使用系统?具体是哪些人在使用系统?

这时候,就可以通过session监听器(javax.servlet.http.HttpSessionAttributeListener 接口)的功能来实现。当用户登录系统的时候,通过特定的session属性记录下用户的登录情况。

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

第1步 准备session监听器处理类。
(如下面的SessionListener.java)
Java代码 <embed height="15" width="14" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" flashvars="clipboard=%2F*%0A%20*%20%40(%23)SessionListener.java%091.00%092009%2F02%2F20%0A%20*%20CopyRight(C)%20stephen(zhoujianqiang%20AT%20gmail%20DOT%20com)%202009-2014%2C%20All%20rights%20reserved.%0A%20*%2F%0Apackage%20com.stephen.filter%3B%0A%0Aimport%20java.util.ArrayList%3B%0Aimport%20java.util.List%3B%0Aimport%20javax.servlet.http.HttpSessionAttributeListener%3B%0Aimport%20javax.servlet.http.HttpSessionBindingEvent%3B%0A%0A%2F**%0A%20*%20session%E7%9B%91%E5%90%AC%E5%99%A8.%20%3Cbr%3E%0A%20*%20%E5%9C%A8WEB%E5%AE%B9%E5%99%A8%E7%9A%84web.xml%E4%B8%AD%E6%B7%BB%E5%8A%A0%E6%9C%AC%E7%9B%91%E5%90%AC%E5%99%A8%E7%9A%84%E8%B0%83%E7%94%A8%2C%E5%85%B7%E4%BD%93%E6%A0%BC%E5%BC%8F%E5%A6%82%E4%B8%8B%EF%BC%9A(%E5%85%B6%E4%B8%AD%E7%9A%84%22%5B%22%2C%22%5D%22%E5%88%86%E5%88%AB%E7%94%A8%22%20%3C%22%2C%22%3E%22%E6%9B%BF%E6%8D%A2)%20%3Cbr%3E%0A%20*%20%0A%20*%20%3Cpre%3E%0A%20*%20%0A%20*%20%20%20%20%5Bweb-app%5D%0A%20*%20%20%20%20%5Bfilter%5D%0A%20*%20%20%20%20...%0A%20*%20%20%20%20%5B%2Ffilter%5D%0A%20*%20%20%20%20%5Bfilter-mapping%5D%0A%20*%20%20%20%20...%0A%20*%20%20%20%20%5B%2Ffilter-mapping%5D%0A%20*%20%20%20%20...%0A%20*%20%20%20%20%5Blistener%5D%5Blistener-class%5Dcom.stephen.filter.SessionListener%5B%2Flistener-class%5D%5B%2Flistener%5D%0A%20*%20%20%20%20...%0A%20*%20%20%20%20%5Bservlet%5D%0A%20*%20%20%20%20...%0A%20*%20%20%20%20%5B%2Fservlet%5D%0A%20*%20%20%20%20...%0A%20*%20%20%20%20%5B%2Fweb-app%5D%0A%20*%20%20%0A%20*%20%3C%2Fpre%3E%0A%20*%20%0A%20*%20%E6%B3%A8%E6%84%8F%E5%9C%A8web.xml%E4%B8%AD%E9%85%8D%E7%BD%AE%E7%9A%84%E4%BD%8D%E7%BD%AE.%20%3Cbr%3E%0A%20*%20%0A%20*%20%40author%20stephen%0A%20*%20%40version%201.00%0A%20*%20%40see%20javax.servlet.http.HttpSessionAttributeListener%0A%20*%2F%0Apublic%20class%20SessionListener%20implements%20HttpSessionAttributeListener%20%7B%0A%09%2F**%0A%09%20*%20%E5%AE%9A%E4%B9%89%E7%9B%91%E5%90%AC%E7%9A%84session%E5%B1%9E%E6%80%A7%E5%90%8D.%0A%09%20*%2F%0A%09public%20final%20static%20String%20LISTENER_NAME%20%3D%20%22_login%22%3B%0A%09%0A%09%2F**%0A%09%20*%20%E5%AE%9A%E4%B9%89%E5%AD%98%E5%82%A8%E5%AE%A2%E6%88%B7%E7%99%BB%E5%BD%95session%E7%9A%84%E9%9B%86%E5%90%88.%0A%09%20*%2F%0A%09private%20static%20List%20sessions%20%3D%20new%20ArrayList()%3B%0A%0A%09%2F**%0A%09%20*%20%E5%8A%A0%E5%85%A5session%E6%97%B6%E7%9A%84%E7%9B%91%E5%90%AC%E6%96%B9%E6%B3%95.%0A%09%20*%20%0A%09%20*%20%40param%20HttpSessionBindingEvent%0A%09%20*%20%20%20%20%20%20%20%20%20%20%20%20session%E4%BA%8B%E4%BB%B6%0A%09%20*%2F%0A%09public%20void%20attributeAdded(HttpSessionBindingEvent%20sbe)%20%7B%0A%09%09if%20(LISTENER_NAME.equals(sbe.getName()))%20%7B%0A%09%09%09sessions.add(sbe.getValue())%3B%0A%09%09%7D%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20session%E5%A4%B1%E6%95%88%E6%97%B6%E7%9A%84%E7%9B%91%E5%90%AC%E6%96%B9%E6%B3%95.%0A%09%20*%20%0A%09%20*%20%40param%20HttpSessionBindingEvent%0A%09%20*%20%20%20%20%20%20%20%20%20%20%20%20session%E4%BA%8B%E4%BB%B6%0A%09%20*%2F%0A%09public%20void%20attributeRemoved(HttpSessionBindingEvent%20sbe)%20%7B%0A%09%09if%20(LISTENER_NAME.equals(sbe.getName()))%20%7B%0A%09%09%09sessions.remove(sbe.getValue())%3B%0A%09%09%7D%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20session%E8%A6%86%E7%9B%96%E6%97%B6%E7%9A%84%E7%9B%91%E5%90%AC%E6%96%B9%E6%B3%95.%0A%09%20*%20%0A%09%20*%20%40param%20HttpSessionBindingEvent%0A%09%20*%20%20%20%20%20%20%20%20%20%20%20%20session%E4%BA%8B%E4%BB%B6%0A%09%20*%2F%0A%09public%20void%20attributeReplaced(HttpSessionBindingEvent%20sbe)%20%7B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E8%BF%94%E5%9B%9E%E5%AE%A2%E6%88%B7%E7%99%BB%E5%BD%95session%E7%9A%84%E9%9B%86%E5%90%88.%0A%09%20*%20%0A%09%20*%20%40return%0A%09%20*%2F%0A%09public%20static%20List%20getSessions()%20%7B%0A%09%09return%20sessions%3B%0A%09%7D%0A%7D%0A%0A" src="http://stephen830.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_mediaid="lk_juiceapp_mediaPopup_1235637043374" lk_media="yes">
  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 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()));  
//记入session监听器 session.setAttribute(com.stephen.filter.SessionListener.LISTENER_NAME,new OnlineSession(request.getRemoteAddr(),userName,new Date().toString()));
注意在上面的代码中使用了新的OnlineSession类,OnlineSession类封装了登录用户的信息(如用户ip,用户名,登录时间).

OnlineSession具体的内容如下:
Java代码 <embed height="15" width="14" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" flashvars="clipboard=%2F*%0A%20*%20%40(%23)OnlineSession.java%091.00%092009%2F02%2F20%0A%20*%20CopyRight(C)%20stephen(zhoujianqiang%20AT%20gmail%20DOT%20com)%202009-2014%2C%20All%20rights%20reserved.%0A%20*%2F%0Apackage%20com.stephen.filter%3B%0A%0A%2F**%0A%20*%20%E5%AE%A2%E6%88%B7session%E4%BF%A1%E6%81%AF.%0A%20*%20%0A%20*%20%40author%20%20stephen%0A%20*%20%40version%201.0.0%0A%20*%2F%0Apublic%20final%20class%20OnlineSession%20%7B%0A%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20%2F**%0A%20%20%20%20%20*%20%E5%AE%A2%E6%88%B7%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84ip.%0A%20%20%20%20%20*%2F%0A%20%20%20%20private%20String%20ip%20%3D%20null%3B%0A%20%20%20%20%2F**%0A%20%20%20%20%20*%20%E5%AE%A2%E6%88%B7%E7%99%BB%E5%BD%95%E5%90%8D.%0A%20%20%20%20%20*%2F%0A%20%20%20%20private%20String%20loginId%20%3D%20null%3B%0A%20%20%20%20%2F**%0A%20%20%20%20%20*%20%E5%AE%A2%E6%88%B7%E7%99%BB%E5%BD%95%E7%B3%BB%E7%BB%9F%E6%97%B6%E9%97%B4.%0A%20%20%20%20%20*%2F%0A%20%20%20%20private%20String%20onlineTime%20%3D%20null%3B%0A%20%20%20%20%0A%20%20%20%20%2F**%0A%20%20%20%20%20*%20%E6%9E%84%E9%80%A0%E5%99%A8.%0A%20%20%20%20%20*%20%40param%20ip%0A%20%20%20%20%20*%20%40param%20loginId%0A%20%20%20%20%20*%20%40param%20onlineTime%0A%20%20%20%20%20*%2F%0A%20%20%20%20public%20OnlineSession(String%20ip%2CString%20loginId%2CString%20onlineTime)%7B%0A%20%20%20%20%09this.ip%3Dip%3B%0A%20%20%20%20%09this.loginId%3DloginId%3B%0A%20%20%20%20%09this.onlineTime%3DonlineTime%3B%0A%20%20%20%20%7D%0A%20%20%20%20%0A%09%2F**%0A%09%20*%20%40return%20Returns%20the%20ip.%0A%09%20*%2F%0A%09public%20String%20getIp()%20%7B%0A%09%09return%20ip%3B%0A%09%7D%0A%09%2F**%0A%09%20*%20%40param%20ip%20The%20ip%20to%20set.%0A%09%20*%2F%0A%09public%20void%20setIp(String%20ip)%20%7B%0A%09%09this.ip%20%3D%20ip%3B%0A%09%7D%0A%09%2F**%0A%09%20*%20%40return%20Returns%20the%20loginId.%0A%09%20*%2F%0A%09public%20String%20getLoginId()%20%7B%0A%09%09return%20loginId%3B%0A%09%7D%0A%09%2F**%0A%09%20*%20%40param%20loginId%20The%20loginId%20to%20set.%0A%09%20*%2F%0A%09public%20void%20setLoginId(String%20loginId)%20%7B%0A%09%09this.loginId%20%3D%20loginId%3B%0A%09%7D%0A%09%2F**%0A%09%20*%20%40return%20Returns%20the%20onlineTime.%0A%09%20*%2F%0A%09public%20String%20getOnlineTime()%20%7B%0A%09%09return%20onlineTime%3B%0A%09%7D%0A%09%2F**%0A%09%20*%20%40param%20onlineTime%20The%20onlineTime%20to%20set.%0A%09%20*%2F%0A%09public%20void%20setOnlineTime(String%20onlineTime)%20%7B%0A%09%09this.onlineTime%20%3D%20onlineTime%3B%0A%09%7D%0A%7D%0A%0A" src="http://stephen830.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_mediaid="lk_juiceapp_mediaPopup_1235637043403" lk_media="yes">
  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; } }

你可能感兴趣的:(session)