您的位置:首页 > 理论基础 > 计算机网络

HttpSessionAttributeListener

2016-07-15 17:49 811 查看

HttpSessionAttributeListener

监听已登录用户

当进行session操作时,如下:就会调用本监听

session.setAttribute("username","tom");

session.removeAttribute("username");

 

 

 

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import com.stephen.utility.DateTool;

@WebListener
public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener {

@Override
public void attributeAdded(HttpSessionBindingEvent event) {
System.out.println(DateTool.date2String()+"->[MyHttpSessionAttributeListener]"+event.getName()+"="+event.getValue()+" attributeAdded");
}

@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
System.out.println(DateTool.date2String()+"->[MyHttpSessionAttributeListener]"+event.getName()+"="+event.getValue()+" attributeRemoved");
}

@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
System.out.println(DateTool.date2String()+"->[MyHttpSessionAttributeListener]"+event.getName()+"="+event.getValue()+" attributeReplaced");
}

}

 

 

 

package com.nbrc.lddw.util;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.nbrc.lddw.interceptor.AuthorizeInterceptor;
import com.nbrc.lddw.model.OnlineInfo;
import com.nbrc.lddw.model.User;
import com.nbrc.lddw.service.OnlineUserService;
/**
*
* @author fox
* @date 2009-02-09
* @description 已登录用户的监听
*/
public class OnlineUserListener implements HttpSessionAttributeListener {
private static Log log = LogFactory.getLog(OnlineUserListener.class);

public void attributeAdded(HttpSessionBindingEvent hse) {
log.info("value bound! make session info ...");
HttpSession session = hse.getSession();
ApplicationContext context =
WebApplicationContextUtils.getRequiredWebApplicationContext(session.getServletContext());
OnlineUserService svr = (OnlineUserService)context.getBean("onlineService");
User u = null;
if(session.getAttribute(AuthorizeInterceptor.USR_KEY)!=null)
u = (User) session.getAttribute(AuthorizeInterceptor.USR_KEY);
if(u!=null && svr.findByUserId(u.getId())==null){
OnlineInfo info = new OnlineInfo();
info.setSessionId(session.getId());
info.setUserId(u.getId());
svr.save(info);
}else{
log.error("can't get user in session");
}
}

public void attributeRemoved(HttpSessionBindingEvent hse) {
HttpSession session = hse.getSession();
ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(session.getServletContext());
OnlineUserService svr = (OnlineUserService)context.getBean("onlineService");
if(svr.findById(session.getId())!=null){
svr.removeById(session.getId());
}
}

public void attributeReplaced(HttpSessionBindingEvent se) {
// TODO Auto-generated method stub

}

}

 

 

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐