public interface HttpSessionListener
extends java.util.EventListener
Implementations of this interface are notified of changes to the list of active sessions in a web application. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application
void sessionCreated(HttpSessionEvent se)
Notification that a session was created.
void sessionDestroyed(HttpSessionEvent se)
Notification that a session is about to be invalidated.
HttpSessionAttributeListener监听
HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。
实际例子:
用户提出要求,需要知道哪些用户已经登录,但是在实际环境中,程序被部署到3台web上,前端用f5的负载均衡,那玩意没有玩过,幸好数据库是单一的,所以就简单建立了一个MySQL的内存表(Heap Table),来已经登录的用户信息。
Java代码
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
}
}
extends java.util.EventListener
Implementations of this interface are notified of changes to the list of active sessions in a web application. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application
void sessionCreated(HttpSessionEvent se)
Notification that a session was created.
void sessionDestroyed(HttpSessionEvent se)
Notification that a session is about to be invalidated.
HttpSessionAttributeListener监听
HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。
实际例子:
用户提出要求,需要知道哪些用户已经登录,但是在实际环境中,程序被部署到3台web上,前端用f5的负载均衡,那玩意没有玩过,幸好数据库是单一的,所以就简单建立了一个MySQL的内存表(Heap Table),来已经登录的用户信息。
Java代码
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
}
}