天天看點

servlet 八個Listener接口,六個Event類别 (轉)

Servlet事件監聽器

在Servlet技術中已經定義了一些事件,并且我們可以針對這些事件來編寫相關的事件監聽器,進而對事件作出相應處理。Servlet事件主要有3類:Servlet上下文事件、會話事件與請求事件。下面具體講解這3類事件的監聽器實作。

1.對Servlet上下文進行監聽

可以監聽ServletContext對象的建立和删除以及屬性的添加、删除和修改等操作。該監聽器需要使用到如下兩個接口類:

● ServletContextAttributeListener:監聽對ServletContext屬性的操作,如增加、删除、修改操作。

● ServletContextListener:監聽ServletContext,當建立ServletContext時,激發contextInitialized (ServletContextEvent sce)方法;當銷毀ServletContext時,激發contextDestroyed(ServletContext- Event sce)方法。

2.監聽Http會話

可以監聽Http會話活動情況、Http會話中屬性設定情況,也可以監聽Http會話的active、paasivate情況等。該監聽器需要使用到如下多個接口類:

● HttpSessionListener:監聽HttpSession的操作。當建立一個Session時,激發session Created (SessionEvent se)方法;當銷毀一個Session時,激發sessionDestroyed (HttpSessionEvent se)    方法。

● HttpSessionActivationListener:用于監聽Http會話active、passivate情況。

● HttpSessionAttributeListener:監聽HttpSession中屬性的操作。當在Session增加一個屬性時,激發attributeAdded(HttpSessionBindingEvent se) 方法;當在Session删除一個屬性時,激發attributeRemoved(HttpSessionBindingEvent se)方法;在Session屬性被重新設定時,激發attributeReplaced(HttpSessionBindingEvent se) 方法。

3.對用戶端請求進行監聽

繼續閱讀