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中如何做依賴注入"

 

感謝所有發問解答提供了方便的前輩,多謝,哈哈!

參考資料如下:

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