★★ 本篇为原创,需要引用转载的朋友请注明:《 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">
- /*
- * @(#)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;
- }
- }
/* * @(#)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代码
- <?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>
<?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代码
- //记入session监听器
- 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">
- /*
- * @(#)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;
- }
- }
/* * @(#)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; } }