Listener类:
/**
* 定义监听的session属性名.
*/
public final static String LISTENER_NAME = "_loginSession";
/**
* 定义存储客户登录session的集合.
*/
@SuppressWarnings("unchecked")
private static List sessions = new ArrayList();
* 加入session时的监听方法.
*
* @param HttpSessionBindingEvent
* session事件
*/
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
//System.out.println("被添加的名字是:"+se.getName());
//System.out.println("被添加的值是:"+se.getValue());
sessions.add(se.getValue());
}
}
* session失效时的监听方法.
*
* @param HttpSessionBindingEvent
* session事件
*/
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
//System.out.println("被销毁的名字是:"+se.getName());
//System.out.println("被销毁的值是:"+se.getValue());
if (LISTENER_NAME.equals(se.getName())) {
sessions.remove(se.getValue());
}
}
* session覆盖时的监听方法.
*
* @param HttpSessionBindingEvent
* session事件
*/
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
}
* 返回客户登录session的集合.
*
* @return
*/
@SuppressWarnings("unchecked")
public static List getSessions() {
return sessions;
}
}
* 客户计算机的ip.
*/
private String ip = null;
/**
* 登录用户ID
*/
private String keyId=null;
/**
* 客户登录名. 客服工号
*/
/**
* 客户登录系统时间.
*/
private String onlineTime = null;
/**
* 构造器.
* @param ip
* @param loginId
* @param onlineTime
*/
public OnLineSession(String ip,String keyId,String loginId,String onlineTime){
this.ip=ip;
this.keyId=keyId;
this.loginId=loginId;
this.onlineTime=onlineTime;
}
/**
* @return Returns the ip.
*/
public String getIp() {
return ip;
}
/**
* @param ip The ip to set.
*/
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;
}
return keyId;
}
this.keyId = keyId;
}
}
<listener-class>包路径.UserOnLineListener</listener-class>
<load-on-startup>1</load-on-startup>
<description>session监听器用户在线离线</description>
</listener>
/**
* request.getRemoteAddr() 客户端的ip
*
*/
new OnLineSession(request.getRemoteAddr(),u.getUserId().toString(),u.getUserAccount(),new Date().toString()));
HttpServletRequest request = ServletActionContext.getRequest();
List<OnLineSession> sessionList=new ArrayList<OnLineSession>();
if(sessions!=null){
OnLineSession onlineSession = null; //封装登录用户的对象
Iterator it = sessions.iterator();
while(it.hasNext()){
onlineSession=(OnLineSession)it.next();
sessionList.add(onlineSession);//登录用户的工号
//System.out.println("用户登录的工号的序号:"+onlineSession.getKeyId());
}
}
<!-- 页面级变量起控制作用 start 显示用户是否上下线问题 -->
<s:set name="onlineValue" value="0" />
<c:forEach items="${requestScope.sessionList}" var="online">
<c:if test="${online.keyId eq userId }">
<s:set name="onlineValue" value="1" />
</c:if>
</c:forEach>
<!-- 页面级变量起控制作用 stop -->
<s:if test="#onlineValue!=1"> 离线</s:if>
window.onbeforeunload=function()
{
var warnning = '<fmt:message key="systemMessage.exitWarning" />';
var beforeExit='<fmt:message key="systemMessage.beforeExitWarning" />';
if(event.clientY<0 && event.clientX>document.body.clientWidth-20 || event.clientY<0 && event.clientX<20 ||
event.altKey || event.ctrlKey || event.clientY>document.body.clientHeight){
//alert(beforeExit);
//return warnning;
window.location ='${basePath}companyManage/user!loginOut.action';//进入后台清除session值的操作
}
}