5.監聽器(Listener)
1.監聽器簡介:
監聽器主要用來監聽物件的建立,屬性的變化,是一個實現特定介面的普通Java類。
Listener介面與事件對應表:
與 ServletContext 有關 |
ServletContextListener |
ServletContextEvent |
ServletContextAttributeListener |
ServletContextAttributeEvent |
|
與 HttpSession 有關 |
HttpSessionListener |
HttpSessionEvent |
HttpSessionActivationListener |
||
HttpSessionAttributeListener |
HttpSessionBindingEvent |
|
HttpSessionBindingListener |
||
與 ServletRequest 有關 |
ServletRequestListener |
ServletRequestEvent |
ServletRequestAttributeListener |
ServletRequestAttributeEvent |
編寫監聽器的步驟:
編寫實現類->在web.xml中進行部署->編寫測試頁面
2.與ServletContext相關監聽器
單個Web站點的資源都共享一個javax.servlet.ServletContext類的實體。通過該物件可以存取應用程式的全域性物件以及初始化階段的變數全域性物件即為Application範圍物件,其生命週期從容器啟動至容器關閉。初始階段的變數是指在web.xml中,由<context-param>元素設定的變數,該變數的範圍是Application範圍
ServletContextListener介面:
實現了該介面的程式,當JavaWeb應用程式啟動時,會自動開始監聽工作
首先呼叫contextInitialized()方法接收對應的ServletContextEvent事件
當應用從容器中移除時,會自動呼叫contextDestroyed()方法
以上兩個方法都會接收到ServletContextEvent事件物件,該物件可以呼叫getServletContext()方法取得ServletContext物件(全域性物件)
ServletContextAttributeListener介面:
實現該介面的程式,能夠監聽ServletContext屬性的變化,例如:當往ServletContext中新增資料時,該程式會被呼叫。
方法 |
說明 |
attributeAdded(ServletContextAttributeEvent e) |
若有物件加入Application範圍時,通知 正在收聽的物件 |
attributeReplaced(ServletContextAttributeEvent e) |
若在Application的範圍,有物件取代另 一個物件,通知正在收聽的物件 |
attributeRemoved(ServletContextAttributeEvent e) |
若有物件從Application範圍移出時,通 知正在收聽的物件 |
ServletContextAttributeEvent的主要方法:getName(),getValue();attributeReplaced()方法中,getName()與getValue()是取之前的值。
3.與HttpSession相關監聽器:
HttpSessionListener:
HttpSessionListener監聽Session物件的建立與銷燬,當有Session物件產生或銷燬時,會自動呼叫sessionCreated()或sessionDestroyed()兩個方法。
HttpSessionEvent事件:
HttpSessionListener介面與HttpSessionActivationListener介面都使用HttpSessionEvent事件物件
HttpSessionEvent類主要的方法:
getSession()
HttpSessionActivationListener介面:
該介面主要用於:同一個Session轉移到不同JVM的情形(如:負載均衡,這些JVM可以在同一臺機器或分散在網路中的多臺機器)
當Session被儲存起來,並且等待轉移至另一個JVM,這段時間稱為失效狀態(Passivate),若Session中的屬性物件實現HttpSessionActivationListener介面時,Container會自動呼叫sessionWillPassivate()方法通知該物件的Session已變成失效狀態
當Session被轉移至其他JVM之後,它又成為有效狀態(Activate),此時Container會自動呼叫sessionDidActivate()方法通知該物件的Session已變成有效狀態。
方法 |
說明 |
HttpSessionListener介面 |
|
sessionCreated(HttpSessionEvent e) |
通知正在收聽的物件,Session已經被加 載及初始化 |
sessionDestroyed(HttpSessionEvent e) |
通知正在收聽的物件, Session已經被 載出 |
HttpSessionActivationListener介面 |
|
sessionDidActivate(HttpSessionEvent e) |
通知正在收聽的物件,它的Session已經 被變為有效狀態 |
sessionWillPassivate(HttpSessionEvent e) |
通知正在收聽的物件,它的Session已經 被變為無效狀態 |
HttpSessionAttributeListener:
HttpSessionAttributeListener會監聽Session屬性的變化,功能與ServletContextAttributeListener介面類似,包含三個方法
attributeAdded()
attributeReplaced()
attributeRemove()
HttpSessionBindingEvent事件
HttpSessionBindingEvent事件主要有三個方法
getName()
getSession()
getValue()
HttpSessionBindingListener:
實現HttpSessionBindingListener介面的物件加入Session範圍或從Session範圍中移除時,容器會分別自動呼叫下面兩個方法:
valueBound(HttpSessionBindingEvent e)
valueUnbound(HttpSessionBindingEvent e)
HttpSessionBindingListener介面是唯一不需要在web.xml中設定的Listener
自定義實現HttpSessionBindingListener介面的類
例項化監聽器類的物件
將該物件新增到Session中
HttpSessionAttributeListener
HttpSessionAttributeListener使用的事件與HttpSessionBindingListener使用的事件相同: HttpSessionBindingEvent
HttpSessionAttributeListener與HttpSessionBindingListener的不同在於:
前者監聽Web站點所有Session範圍的變化
後者只監聽Session範圍內實現了HttpSessionBindingListener介面的物件移入移出
方法 |
說明 |
HttpSessionBindingListener介面 |
|
valueBound(HttpSessionBindingEvent e) |
當實現 HttpSessionBindingListener的對 象加入session時,會呼叫該方法 |
valueUnbound(HttpSessionBindingEvent e) |
當實現 HttpSessionBindingListener的對 象在session中銷燬時,呼叫該方 法 |
HttpSessionAttributeListener介面 |
|
attributeAdded(HttpSessionBindingEvent e) |
若有物件加入Session範圍時,通 知正在收聽的物件 |
attributeReplaced(HttpSessionBindingEvent e) |
若在Session的範圍,有物件取代 另一個物件,通知正在收聽的對 象 |
attributeRemoved(HttpSessionBindingEvent e) |
若有物件從Session範圍移出時, 4通知正在收聽的物件 |
4.與ServletRequest相關監聽器:
ServletRequestListener介面:
當有請求產生或銷燬,會自動呼叫該介面實現的requestInitialized()或requestDestroyed()方法
該介面使用ServletRequestEvent事件
方法 |
說明 |
requestInitialized(ServletRequestEvent e) |
通知正在收聽的物件, ServletRequest已經被載入及初始 化 |
requestDestroyed(ServletRequestEvent e) |
通知正在收聽的物件, ServletRequest已經被載出 |
ServletRequestEvent的主要方法:
getServletContext()
getServletRequest()
ServletResquestAttributeListener
該介面監聽Request範圍的變化,有三個主要方法:
attributeAdded()
attributeReplaced()
attributeRemoved()
使用ServletRequestAttributeEvent事件。
ServletRequestAttributeEvent主要方法
getName()
getValue()
監聽器的應用:
ServletContext範圍的監聽器可以進行一些初始化的動作,如:當Web應用啟動的時候進行全域性配置
Session範圍的監聽器對一個會話過程(與客戶端關聯)中所產生的事件進行響應,可以對客戶端資訊的變化進行跟蹤
Request範圍的監聽器可以監聽使用者的每次請求
實現了該介面的程式,當JavaWeb應用程式啟動時,會自動開始監聽工作首先呼叫contextInitialized()方法接收對應的ServletContextEvent事件當應用從容器中移除時,會自動呼叫contextDestroyed()方法以上兩個方法都會接收到ServletContextEvent事件物件,該物件可以呼叫getServletContext()方法取得ServletContext物件(全域性物件)