转自: http://stephen830.iteye.com/blog/338291
在系统运行中,有时候我们想了解下当前使用系统的用户,有多少人在使用系统?具体是哪些人在使用系统?
这时候,就可以通过session监听器(javax.servlet.http.HttpSessionAttributeListener 接口)的功能来实现。当用户登录系统的时候,通过特定的session属性记录下用户的登录情况。
整个过程可以分为以下4步:
第1步 准备session监听器处理类。
(如下面的SessionListener.java)
-
-
-
-
- package com.stephen.filter;
-
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.http.HttpSessionAttributeListener;
- import javax.servlet.http.HttpSessionBindingEvent;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class SessionListener implements HttpSessionAttributeListener {
-
-
-
- public final static String LISTENER_NAME = "_login";
-
-
-
-
- private static List sessions = new ArrayList();
-
-
-
-
-
-
-
- public void attributeAdded(HttpSessionBindingEvent sbe) {
- if (LISTENER_NAME.equals(sbe.getName())) {
- sessions.add(sbe.getValue());
- }
- }
-
-
-
-
-
-
-
- public void attributeRemoved(HttpSessionBindingEvent sbe) {
- if (LISTENER_NAME.equals(sbe.getName())) {
- sessions.remove(sbe.getValue());
- }
- }
-
-
-
-
-
-
-
- public void attributeReplaced(HttpSessionBindingEvent sbe) {
- }
-
-
-
-
-
-
- public static List getSessions() {
- return sessions;
- }
- }
-
-
-
-
- package com.stephen.filter;
-
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.http.HttpSessionAttributeListener;
- import javax.servlet.http.HttpSessionBindingEvent;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class SessionListener implements HttpSessionAttributeListener {
-
-
-
- public final static String LISTENER_NAME = "_login";
-
-
-
-
- private static List sessions = new ArrayList();
-
-
-
-
-
-
-
- public void attributeAdded(HttpSessionBindingEvent sbe) {
- if (LISTENER_NAME.equals(sbe.getName())) {
- sessions.add(sbe.getValue());
- }
- }
-
-
-
-
-
-
-
- public void attributeRemoved(HttpSessionBindingEvent sbe) {
- if (LISTENER_NAME.equals(sbe.getName())) {
- sessions.remove(sbe.getValue());
- }
- }
-
-
-
-
-
-
-
- public void attributeReplaced(HttpSessionBindingEvent sbe) {
- }
-
-
-
-
-
-
- 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 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>
<?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步 当用户登录时监听用户。
当用户成功登录后执行下面的代码。
-
- session.setAttribute(com.stephen.filter.SessionListener.LISTENER_NAME,new OnlineSession(request.getRemoteAddr(),userName,new Date().toString()));
-
- 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具体的内容如下:
-
-
-
-
- package com.stephen.filter;
-
-
-
-
-
-
-
- public final class OnlineSession {
-
-
-
-
-
- private String ip = null;
-
-
-
- private String loginId = null;
-
-
-
- private String onlineTime = null;
-
-
-
-
-
-
-
- public OnlineSession(String ip,String loginId,String onlineTime){
- this.ip=ip;
- this.loginId=loginId;
- this.onlineTime=onlineTime;
- }
-
-
-
-
- public String getIp() {
- return ip;
- }
-
-
-
- public void setIp(String ip) {
- this.ip = ip;
- }
-
-
-
- public String getLoginId() {
- return loginId;
- }
-
-
-
- public void setLoginId(String loginId) {
- this.loginId = loginId;
- }
-
-
-
- public String getOnlineTime() {
- return onlineTime;
- }
-
-
-
- public void setOnlineTime(String onlineTime) {
- this.onlineTime = onlineTime;
- }
- }
-
-
-
-
- package com.stephen.filter;
-
-
-
-
-
-
-
- public final class OnlineSession {
-
-
-
-
-
- private String ip = null;
-
-
-
- private String loginId = null;
-
-
-
- private String onlineTime = null;
-
-
-
-
-
-
-
- public OnlineSession(String ip,String loginId,String onlineTime){
- this.ip=ip;
- this.loginId=loginId;
- this.onlineTime=onlineTime;
- }
-
-
-
-
- public String getIp() {
- return ip;
- }
-
-
-
- public void setIp(String ip) {
- this.ip = ip;
- }
-
-
-
- public String getLoginId() {
- return loginId;
- }
-
-
-
- public void setLoginId(String loginId) {
- this.loginId = loginId;
- }
-
-
-
- public String getOnlineTime() {
- return onlineTime;
- }
-
-
-
- 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;
}
}
第4步 显示在线用户的情况。
可以直接通过 SessionListener.getSessions()方法来取得所有在线的用户。
- <%@ page contentType="text/html;charset=UTF-8"%>
- <%@ page import="java.util.List"%>
- <%@ page import="java.util.Iterator"%>
- <%@ page import="com.stephen.filter.SessionListener"%>
- <%@ page import="com.stephen.filter.OnlineSession"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html><head><title>Online session Query</title>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- </head><body bgcolor="#e5ecf9" topmargin="5px" leftmargin="5px" rightmargin="5px">
- <%
- List sessions = SessionListener.getSessions();
- String pageErrorInfo = null;
- try{
- %>
- <h5>Online Employee</h5>
-
- <table width="100%" align="center" cellspacing="1" cellpadding="4" border="0"></str
分享到:
评论