-
-
- package com.stephen.filter;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.http.HttpSessionAttributeListener;
- import javax.servlet.http.HttpSessionBindingEvent;
-
- public class SessionListenerimplements HttpSessionAttributeListener {
-
- public finalstatic 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监听器.
* 在WEB容器的web.xml中添加本监听器的调用,具体格式如下:(其中的"[","]"分别用" <",">"替换)
*
*
*
* [web-app]
* [filter]
* ...
* [/filter]
* [filter-mapping]
* ...
* [/filter-mapping]
* ...
* [listener][listener-class]com.stephen.filter.SessionListener[/listener-class][/listener]
* ...
* [servlet]
* ...
* [/servlet]
* ...
* [/web-app]
*
*
*
* 注意在web.xml中配置的位置.
*
* @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配置)
- xmlversion="1.0"?>
- >
- <web-app>
- <listener><listener-class>com.stephen.filter.SessionListenerlistener-class>listener>
- <welcome-file-list>
- <welcome-file>index.htmlwelcome-file>
- welcome-file-list>
- web-app>
com.stephen.filter.SessionListener
index.html
上面的
com.stephen.filter.SessionListener 就是配置session监听器的。
在你的web.xml配置文件中添加上这一行就可以了(要注意添加的位置)。
第3步 当用户登录时监听用户。
当用户成功登录后执行下面的代码。
- 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 finalclass 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()方法来取得所有在线的用户。
- <%@ pagecontentType="text/html;charset=UTF-8"%>
- <%@ pageimport="java.util.List"%>
- <%@ pageimport="java.util.Iterator"%>
- <%@ pageimport="com.stephen.filter.SessionListener"%>
- <%@ pageimport="com.stephen.filter.OnlineSession"%>
- >
- <html><head><title>Online session Querytitle>
- <metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
- head><bodybgcolor="#e5ecf9"topmargin="5px"leftmargin="5px"rightmargin="5px">
- <%
- List sessions =SessionListener.getSessions();
- String pageErrorInfo =null;
- try{
- %>
- <h5>Online Employeeh5>
- <tablewidth="100%"align="center"cellspacing="1"cellpadding="4"border="0">
- <trid="listTitle">
- <td>No.td>
- <td>登录Idtd>
- <td>登录时间td>
- <td>iptd>
- tr>
- <%
- StringBuffer datas =new StringBuffer();
- if(sessions!=null){
- OnlineSession onlineSession =null;
- Iterator it =sessions.iterator();
- int count=0;
- while(it.hasNext()){
- count++;
- onlineSession=(OnlineSession)it.next();
- datas.append("<tr><td>");
- datas.append(count);
- datas.append("td><td>");
- datas.append(onlineSession.getLoginId());
- datas.append("td><td>");
- datas.append(onlineSession.getOnlineTime());
- datas.append("td><td>");
- datas.append(onlineSession.getIp());
- datas.append("td>tr>");
- }
- }
- out.print(datas.toString());
- %>
- table>
- <%
- }catch(Exception e){
- pageErrorInfo = e.toString();
- }finally{
- if(pageErrorInfo!=null){
- %>
- <fontcolor="red">Error:<%=pageErrorInfo%>font>
- <%
- }
- %>
- body>html>
- <%
- }
- %>
<%@ 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"%>
Online session Query
<%
List sessions = SessionListener.getSessions();
String pageErrorInfo = null;
try{
%>
Online Employee
No. |
登录Id |
登录时间 |
ip |
<%
StringBuffer datas = new StringBuffer();
if(sessions!=null){
OnlineSession onlineSession = null;
Iterator it = sessions.iterator();
int count=0;
while(it.hasNext()){
count++;
onlineSession=(OnlineSession)it.next();
datas.append("");
datas.append(count);
datas.append(" | ");
datas.append(onlineSession.getLoginId());
datas.append(" | ");
datas.append(onlineSession.getOnlineTime());
datas.append(" | ");
datas.append(onlineSession.getIp());
datas.append(" |
");
}
}
out.print(datas.toString());
%>
<%
}catch(Exception e){
pageErrorInfo = e.toString();
}finally{
if(pageErrorInfo!=null){
%>
Error:<%=pageErrorInfo%>
<%
}
%>
<%
}
%>
最后的说明:
如果你的系统中有注销(或者退出登录)类似的功能(功能中调用了 session.invalidate() 方法),那么用户的信息就会自动从上面的列表中删除。如果没有使用注销功能,则只能等到session超时候才能从列表中自动删除。