package com.ane56.common.util; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; /** * Created by liyang on 2016/11/28. */ public class SessionListener implements HttpSessionAttributeListener { /** * 定義監聽的session屬性名. */ public final static String LISTENER_NAME = "_login"; /** * 定義存儲客戶登陸session的集合. */ private static List sessions = new ArrayList(); /** * 加入session時的監聽方法. * * @param sbe * session事件 */ public void attributeAdded(HttpSessionBindingEvent sbe) { if (LISTENER_NAME.equals(sbe.getName())) { sessions.add(sbe.getValue()); } } /** * session失效時的監聽方法. * * @param sbe * session事件 */ public void attributeRemoved(HttpSessionBindingEvent sbe) { if (LISTENER_NAME.equals(sbe.getName())) { sessions.remove(sbe.getValue()); } } /** * session覆蓋時的監聽方法. * * @param sbe * session事件 */ public void attributeReplaced(HttpSessionBindingEvent sbe) { } /** * 返回客戶登陸session的集合. * * @return */ public static List getSessions() { return sessions; } }
中的_login就是設定的特殊session屬性,固然你能夠改爲別的名字。 javascript
<!-- 經過listener來統計在線人數--> <listener> <listener-class>com.common.util.SessionListener</listener-class> </listener >
//記入session監聽器 session.setAttribute(com.stephen.filter.SessionListener.LISTENER_NAME, new OnlineSession(request.getRemoteAddr(),userName,new Date().toString()));
注意在上面的代碼中使用了新的OnlineSession類,OnlineSession類封裝了登陸用戶的信息(如用戶ip,用戶名,登陸時間等). html
/** * Created by liyang on 2016/11/28. */ public final class OnlineSession { /** * 客戶計算機的ip. */ private String ip = null; /** * 客戶登陸名. */ private String loginId = null; /** * 客戶登陸系統時間. */ private String onlineTime = null; private String timeCompare = null; ...
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/"; %> <html> <head> <title>user manage</title> <%@ include file="../common/common.jsp"%> <script type="application/javascript"> function date2str(x, y) { var z = { y: x.getFullYear(), M: x.getMonth() + 1, d: x.getDate(), h: x.getHours(), m: x.getMinutes(), s: x.getSeconds() }; return y.replace(/(y+|M+|d+|h+|m+|s+)/g, function(v) { return ((v.length > 1 ? "0" : "") + eval('z.' + v.slice(-1))).slice(-(v.length > 2 ? v.length : 2)) }); } function fmt_time(value){ var unixTimestamp = new Date(value); return date2str(unixTimestamp, "yyyy-MM-d h:m:s") } </script> </head> <body> <table id="listener" class="easyui-datagrid" style="width:100%;height:100%" data-options="fit:true,rownumbers:true,singleSelect:true,url:'../user/sessionListener',method:'POST',pagination:true,pageSize:20"> <thead> <tr> <th data-options="field:'loginId',width:100,sortable:true,align:'center'" >工號</th> <th data-options="field:'ip',width:80,sortable:true,align:'center'" >登陸IP</th> <th data-options="field:'onlineTime',width:240,sortable:true,align:'center',formatter:fmt_time" >登陸時間</th> <th data-options="field:'timeCompare',width:240,sortable:true,align:'center'" >登陸時長</th> </tr> </thead> </table> </body> </html>
第5步 在線用戶登出,清除session記錄。java
@RequestMapping("logout") public void logout(HttpSession session) { //清楚session相關記錄 session.removeAttribute(com.ane56.common.util.SessionListener.LISTENER_NAME); }