关于 session 的 HttpSessionActivationListener 监听器

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

一般情况下他和HttpSessionBindingListener一起使用。 
比如一个属性类, 
Java代码   收藏代码
  1. public class attributeClass implements HttpSessionBindingListener,HttpSessionActivationListener{  
  2.     //HttpSessionActivationListener  
  3.     public   void   sessionDidActivate(HttpSessionEvent   se)   
  4.     {         logout("sessionDidActivate("+se.getSession().getId()+")");//激活  
  5.     }   
  6.     public   void   sessionWillPassivate(HttpSessionEvent   se)  
  7.     {//被传送到别的jvm或 写到硬盘  
  8.     logout("sessionWillPassivate("+se.getSession().getId()+")");  
  9.     }  
  10.     //HttpSessionBindingListener  
  11.     public   void   valueBound(HttpSessionBindingEvent   event)  
  12.     { //被设置到session中(setAttribute)  
  13.         logout("valueBound("+event.getSession().getId()+event.getValue()+")");  
  14.     }   
  15.     public   void   valueUnbound(HttpSessionBindingEvent   event)  
  16.     { //从session中解除(removeAttribute)  
  17.     logout("valueUnbound("+event.getSession().getId()+event.getValue()+")");  
  18.     }  
  19. }  

这样你就可以将它加到session中 
Java代码   收藏代码
  1. public class AAAServlet extends HttpServlet {  
  2.  protected void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {  
  3.    HttpSession session = request.getSession();  
  4.    session.setAttribute("attribute",attributeClass);  
  5.  }  
  6. }  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值