ServletContextAttributeListener 監(jiān)聽對ServletContext屬性的操作,比如增加、刪除、修改屬性。
ServletContextListener監(jiān)聽ServletContext。
當創(chuàng)建ServletContext時,激發(fā) contextInitialized(ServletContextEvent sce)方法;
當銷毀ServletContext時,激發(fā)contextDestroyed(ServletContextEvent sce)方法。
ServletContextListener 接口
contextInitialized 初始化方法
contextDestroyed 銷毀方法
ServletRequestListener, ServletRequestAttributeListener 接口
Servlet 2.4版在事件監(jiān)聽器中加入了ServletRequest監(jiān)聽器,包括:ServletRequestListener, ServletRequestAttributeListener ,用來管理和控制與ServletRequest動作有關(guān)的事件。
事件類型 | 接口 | 方法 |
request初始化 | javax.servlet.ServletRequestListener | requestInitialized() |
request銷毀 | javax.servlet.ServletRequestListener | requestDestroyed() |
增加屬性 | javax.servlet.ServletRequestAttributeListener | attributeAdded() |
刪除屬性 | javax.servlet.ServletRequestAttributeListener | attributeRemoved() |
屬性被替換 | javax.servlet.ServletRequestAttributeListener | attributeReplaced() |
HttpSessionListener 接口
Http會話(Seesion)與請求(Request)與ServletContext用法相當。需指出,Request監(jiān)聽器在Tomcat 4.1不能調(diào)試,故升級到Tomcat 5.0才可以,所以可以肯定RequestListener是符合Servlet2.4新規(guī)范的,需用tomcat5.0以上版本。
利用HttpSessionListener接口可針對HTTP會話建立一個“監(jiān)聽器類”。只要Web應用程序內(nèi)新增了一個HTTP會話,Servlet 容器就會將該事件(HttpSessionEvent)轉(zhuǎn)交給適當?shù)摹氨O(jiān)聽器類”進行處理(必須事先配置web.xml)。
下表是HttpSessionListener接口內(nèi)定義的兩個方法,只要是實現(xiàn)該接口的“監(jiān)聽器類”,就必須包含這兩種方法。
方法名稱 | 調(diào)用時機 |
sessionCreated(HttpSessionEvent se) | 在Web應用程序內(nèi)建立一個新的HTTP會話時, Servlet容器將會調(diào)用此方法 |
sessionDestoryed(HttpSessionEvent se) | 在Web應用程序內(nèi)移除某個HTTP會話時,Servlet容器將會調(diào)用此 方法 |
HttpSessionActivationListener 接口
當Web應用程序的會話必須跨越其他服務器時,實現(xiàn)HttpSessionActivationListener接口的“監(jiān)聽器類”將會收到會話轉(zhuǎn)移的事 件。
下表是HttpSessionActivationListener接口內(nèi)定義的兩種方法。
方法名稱 | 調(diào)用時機 |
sessionWillPassivate(HttpSessionEvent se) | 當HTTP會話必須轉(zhuǎn)移到其他服務器之前,Servlet容器將會調(diào)用此方法 |
sessionDidActivate(HttpSessionEvent se) | 當HTTP會話轉(zhuǎn)移到其他服務器以后,Servlet容器將會調(diào)用此方法 |
舉例來說,會話S必須從服務器A轉(zhuǎn)移到服務器B,此時Servlet容器會在S轉(zhuǎn)移前產(chǎn)生一個會話“被動(passive)”事件,該事件由 HttpSessionActivationListener接口的sessionWillPassivate()方法予以回應。當S轉(zhuǎn)移到服務器B以 后,Servlet容器會再產(chǎn)生一個會話“啟動”(activate)事件,該事件由HttpSessionActivationListener接口的 sessionDidActivate()方法予以回應。
HttpSessionAttributeListener 接口
HttpSessionAttributeListener接口與ServletContextAttributeListener非常類似,前者是針對 HTTP會話所設計的“監(jiān)聽器接口”,后者則是針對Servlet運行環(huán)境(context)所設計的“監(jiān)聽器接口”,該接口定義的方法見下表。
方法名稱 | 調(diào)用時機 |
attributeAdded(HttpSessionBindingEvent scab) | 在HttpSession對象內(nèi)加入新的屬性時會調(diào)用此方法 |
attributeRemoved(ServletContextAttributeEvent scab) | 在HttpSession對象內(nèi)刪除某個屬性時會調(diào)用此方法 |
attributeReplaced(ServletContextAttributeEvent scab) | 在HttpSession對象內(nèi)置換某個屬性時會調(diào)用此方法 |
當HTTP會話(HttpSession對象)內(nèi)新增、置換或刪除某個屬性時將會產(chǎn)生一個事件(HttpSessionBindingEvent),只要 是實現(xiàn)HttpSessionAttributeListener接口的“監(jiān)聽器類”就可以回應該事件。當然了,你必須將這個“監(jiān)聽器類”定義在 web.xml文件內(nèi)。
HttpSessionBindingListener 接口
HttpSessionBindingListener接口在觀念上與HttpSessionAttributeListener接口有點類似,但是它與 本章探討的“監(jiān)聽器類”并沒有直接關(guān)系。
因為Servlet 2.3規(guī)范以前尚未制定Web應用程序的“監(jiān)聽器”機制,如果想知道HTTP會話內(nèi)何時加入或移除某個對象,必須采用下列方式:
(1)準備綁定至HTTP會話的對象必須實現(xiàn) HttpSessionBindingListener接口- - 監(jiān)聽器對象。
(2)在該對象內(nèi)改寫HttpSessionBindingListener接口 所定義的兩種方法(參考下表)。
方法名稱 | 調(diào)用時 機 |
valueBound(HttpSessionBindingEvent event) | 當監(jiān)聽器對象綁定至HTTP會話時,Servlet容器將會調(diào)用此方法 |
valueUnbound(HttpSessionBindingEvent event) | 當監(jiān)聽器對象從HTTP會話內(nèi)修改、移除或會話銷毀時,Servlet容器將會調(diào)用此方法 |