关于 session 的 HttpSessionActivationListener 监听器

Jsp/servlet 标准不要求一个web容器支持分布式应用,但是他一定要支持HttpSessionActivationListener借口,以使代码可以支持分布式环境。一般免费的web容器都不支持分布式,weblogic websphere是支持的。为了负载均衡或者fail-over,web容器可以迁移一个session到其他的jvm.session的passivation是指非活动的session被写入持久设备(比如硬盘)。activate自然就是相反的过程。在分布式环境中切换的属性必须实现serializable接口。

一般情况下他和HttpSessionBindingListener一起使用。
比如一个属性类,
public class attributeClass implements HttpSessionBindingListener,HttpSessionActivationListener{
    //HttpSessionActivationListener
    public   void   sessionDidActivate(HttpSessionEvent   se) 
    {         logout("sessionDidActivate("+se.getSession().getId()+")");//激活
    } 
    public   void   sessionWillPassivate(HttpSessionEvent   se)
    {//被传送到别的jvm或 写到硬盘
    logout("sessionWillPassivate("+se.getSession().getId()+")");
    }
    //HttpSessionBindingListener
    public   void   valueBound(HttpSessionBindingEvent   event)
    { //被设置到session中(setAttribute)
        logout("valueBound("+event.getSession().getId()+event.getValue()+")");
    } 
    public   void   valueUnbound(HttpSessionBindingEvent   event)
    { //从session中解除(removeAttribute)
    logout("valueUnbound("+event.getSession().getId()+event.getValue()+")");
    }
}

这样你就可以将它加到session中
public class AAAServlet extends HttpServlet {
 protected void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
   HttpSession session = request.getSession();
   session.setAttribute("attribute",attributeClass);
 }
}

猜你喜欢

转载自cb-13.iteye.com/blog/1208140
今日推荐