Listeners for HttpSession

最近在项目中恰好需要在 session time out 的时候, 取出某些attribute的value, 做一些善后工作, 要用到 HttpSession Listener, 在网上查到些许资料, 为了方便故,整理一下,写在这里,算是为自己做个汇总, 多有引用,参考帖子的地址附在最后,方便查阅

HttpSession 的 Listener 大致有一下几种:

  1.  HttpSessionListener  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. 这个 interface 包括两个方法, sessionCreated(HttpSessionEvent se), sessionDestroyed(HttpSessionEvent se), 从这两个方法的名字就可以看出, 这个 listener 监听的是 session 的创建和销毁, 因为并不确定在调用sessionDestroyed 方法时, session 里的 attribute 是否被 remove, (手头不便, 待验证), 所以并不确定该 listener 是否会满足我的需求, hold.
  2.  HttpSessionAttributeListener This listener interface can be implemented in order to get notifications of changes to the attribute lists of sessions within this web application. 这个 listener 里共有三个方法 attributeAdded(HttpSessionBindingEvent se), attributeRemoved(HttpSessionBindingEvent se)  和  attributeReplaced(HttpSessionBindingEvent se) . 同样,顾名思义,这个 listener 监听的是 session 中 attribute 的变化 ( 在timeout 时会调用 ), 这个 listener 符合我的需求,但是会在每次 attribute 改变的时候调用, 所以不确定在效能上产生多大的影响, hold.
  3. HttpSessionBindingListener  Causes an object to be notified when it is bound to or unbound from a session. The object is notified by an HttpSessionBindingEvent object. This may be as a result of a servlet programmer explicitly unbinding an attribute from a session, due to a session being invalidated, or due to a session timing out.  这个 listener 包括两个方法 valueBound(HttpSessionBindingEvent event) 和 valueUnbound(HttpSessionBindingEvent event) . 这个 listener 的使用大致是, 倘若我们已经创建了一个实现了该接口的类的实例,当这个实例被 bound 到session 或者被 unbound 的时候, valueBound 和 valueUnbound 就会被调用,  看起来不错,比较接近我的需求, 因为我所需要使用的 attribute 仅仅会在session 开始和结束时, 才会发生bind 和 unbind 操作, 暂 hold.
  4. HttpSessionActivationListener  Objects that are bound to a session may listen to container events notifying them that sessions will be passivated and that session will be activated. A container that migrates session between VMs or persists sessions is required to notify all attributes bound to sessions implementing HttpSessionActivationListener. 这个 listener 包括两个方法 sessionDidActivate(HttpSessionEvent se) 和 sessionWillPassivate(HttpSessionEvent se), 当对象被bound的某个 session 已经被 activated 或者即将被 passivated 时被调用.  说实话虽然字面看懂了,但真心不知道什么时候用,唉,求指教啊!

从上面的说明,我们也可以了解到这些listener的使用方法,前两种 listener 定义好实现接口的类后,需要配置web.xml

<listener> <listener-class>com.abc.MyListener</listener-class> </listener> .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

后面两种实现了接口,将实现接口的类的object 直接设入 session 的 attribute 里即可. 

具体一些listener的例子,可以直接参看下面的链接, mkyong 的示例

真正需要实现的时候,因为我们采用的是springframework,所以还需要参考 mkyong 的另外一篇范例 “在session listener中如何做依赖注入"

 

感谢所有发问解答提供了方便的前辈,多谢,哈哈!

参考资料如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章