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 | 若在Application的範圍,有對象取代另 一個對象,通知正在收聽的對象 |
attributeRemoved(ServletContextAttributeEvent | 若有對象從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 | 通知正在收聽的對象,Session已經被加 載及初始化 |
sessionDestroyed(HttpSessionEvent e) | 通知正在收聽的對象, Session已經被 載出 |
HttpSessionActivationListener接口 | |
sessionDidActivate(HttpSessionEvent | 通知正在收聽的對象,它的Session已經 被變為有效狀态 |
sessionWillPassivate(HttpSessionEvent e) | 被變為無效狀态 |
HttpSessionAttributeListener:
HttpSessionAttributeListener會監聽Session屬性的變化,功能與ServletContextAttributeListener接口類似,包含三個方法
attributeAdded()
attributeReplaced()
attributeRemove()
HttpSessionBindingEvent事件
HttpSessionBindingEvent事件主要有三個方法
getName()
getValue()
HttpSessionBindingListener:
實作HttpSessionBindingListener接口的對象加入Session範圍或從Session範圍中移除時,容器會分别自動調用下面兩個方法:
valueBound(HttpSessionBindingEvent e)
valueUnbound(HttpSessionBindingEvent e)
HttpSessionBindingListener接口是唯一不需要在web.xml中設定的Listener
自定義實作HttpSessionBindingListener接口的類
執行個體化監聽器類的對象
将該對象添加到Session中
HttpSessionAttributeListener使用的事件與HttpSessionBindingListener使用的事件相同: HttpSessionBindingEvent
HttpSessionAttributeListener與HttpSessionBindingListener的不同在于:
前者監聽Web站點所有Session範圍的變化
後者隻監聽Session範圍内實作了HttpSessionBindingListener接口的對象移入移出
HttpSessionBindingListener接口 | |
valueBound(HttpSessionBindingEvent | 當實作 HttpSessionBindingListener的對 象加入session時,會調用該方法 |
valueUnbound(HttpSessionBindingEvent | 象在session中銷毀時,調用該方 法 |
HttpSessionAttributeListener接口 | |
attributeAdded(HttpSessionBindingEvent e) | 若有對象加入Session範圍時,通 |
attributeReplaced(HttpSessionBindingEvent e) | 若在Session的範圍,有對象取代 另一個對象,通知正在收聽的對 象 |
attributeRemoved(HttpSessionBindingEvent e) | 若有對象從Session範圍移出時, 4通知正在收聽的對象 |
4.與ServletRequest相關監聽器:
ServletRequestListener接口:
當有請求産生或銷毀,會自動調用該接口實作的requestInitialized()或requestDestroyed()方法
該接口使用ServletRequestEvent事件
requestInitialized(ServletRequestEvent | ServletRequest已經被加載及初始 化 |
requestDestroyed(ServletRequestEvent e) | ServletRequest已經被載出 |
ServletRequestEvent的主要方法:
getServletContext()
getServletRequest()
ServletResquestAttributeListener
該接口監聽Request範圍的變化,有三個主要方法:
attributeRemoved()
使用ServletRequestAttributeEvent事件。
ServletRequestAttributeEvent主要方法
getName()
監聽器的應用:
ServletContext範圍的監聽器可以進行一些初始化的動作,如:當Web應用啟動的時候進行全局配置
Session範圍的監聽器對一個會話過程(與用戶端關聯)中所産生的事件進行響應,可以對用戶端資訊的變化進行跟蹤
Request範圍的監聽器可以監聽使用者的每次請求
實作了該接口的程式,當JavaWeb應用程式啟動時,會自動開始監聽工作首先調用contextInitialized()方法接收對應的ServletContextEvent事件當應用從容器中移除時,會自動調用contextDestroyed()方法以上兩個方法都會接收到ServletContextEvent事件對象,該對象可以調用getServletContext()方法取得ServletContext對象(全局對象)