標籤:des style blog http color io os ar 使用
使用Listener只需要兩個步驟:
- 定義Listener實作類別。
- 通過Annotation或在web.xml檔案中配置Listener
實現Listener類
監聽不同Web事件的監聽器不相同,常用的Web事件監聽器介面有如下幾個:
- ServletContextListener:用於監聽Web應用的啟動和關閉。
- ServletContextAttributeListener:用於監聽ServletContext範圍(application)內屬性的改變。
- ServletRequestListener:用於監聽使用者請求。
- ServletRequestAttributeListener:用於監聽ServletRequest範圍(request)內屬性的改變。
- HttpSessionListener:用於監聽使用者session的開始和結束。
- HttpSessionAttributeListener:用於監聽HttpSesssion範圍(session)內屬性的改變。
以ServletContextListener為例,要實現這個介面,需要實現兩個方法:
- contextInitialized(ServletCOntextEvent sce):啟動web應用時,系統調用Listener的該方法。
- contextDestroyed(ServletContextEvent sce):關閉web應用時,系統調用Listener的該方法。
ServletContextListener的作用有點類似於load-on-startup Servlet,都可用於在Web應用啟動時,回調方法來啟動某些背景程式,這些背景程式負責為系統運行提供支援。
例:建立一個Listener,用於在啟動時擷取資料庫連接,設定到application中,關閉應用時,關閉串連。
@WebListenerpublic class GetConnListener implements ServletContextListener{ //應用啟動是,該方法被調用 public void contextInitialized(ServletContextEvent sce){ try{ //取得應用的ServletContext執行個體 ServletContext application = sce.getServletContext(); //從配置參數中擷取資料庫資訊 String driver = application.getInitParameter("driver"); String url = application.getInitParameter("url"); String user = application.getInitParameter("user"); String pass = application.getInitParameter("pass"); //註冊驅動 Class.forName(driver); //擷取資料庫連接 Connection conn = DriverManager.getConnection(url,user,pass); //將資料庫連接設定成application範圍內的屬性 application.setAttribute("conn",conn); }catch(Excetpion e){ e.printStackTrace(); } } //應用關閉時,該方法被調用 public void contextDestroyed(ServletContextEvent sce){ //取得該應用的ServletContext執行個體 ServletContext application = sce.getServletContext(); Connection conn = (Connection)application.getAttribute("conn"); //關閉資料庫連接 if(conn!=null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } } }}
配置Listener
配置Listener只要向Web應用註冊Listener實作類別即可,無需配置參數之類的東西。為Web應用配置Listener有兩種方式:
- 使用@WebListener修飾Listener實作類別。
- 在web.xml文檔中使用<listener.../>元素進行配置。
使用@WebListener時通常無需指定任何屬性,只要使用該Annotation修飾Listener實作類別即可向Web應用註冊該監聽器。
在web.xml中使用<listener.../>元素進行配置時只要配置其實作類別即可,如:
<listener> <!-- 指定Listener的實作類別 --> <listener-class>luxl.GetGonnListener</listener-class></listener>
使用ServletContextAttributeListener
ServletContextAttributeListener用於監聽ServletContext(application)範圍內屬性的變化,實現該介面的監聽器需要實現三個方法:
- attributeAdded(ServletCOntextAttributeEvent event);當程式把一個屬性存入application範圍時出發該方法。
- attributeRemoved(ServletContextAttributeEvent event);當程式把一個屬性從application範圍刪除時出發該方法。
- attributeReplaced(ServletContextAttributeEvent event);當程式替換application範圍內的屬性時將出發該方法。
使用ServletRequestListener和ServletRequestAttributeListener
ServletRequestListener用於監聽使用者請求的到達,實現該介面的監聽器需要實現如下兩個方法:
- requestInitialized(ServletRequestEvent sre);使用者請求到底、被初始化是出發該方法。
- requestDestroyed(ServletRequestEvent sre); 使用者請求結束、被銷毀時觸發該方法。
ServletRequestAttributeListener則用於監聽ServletRequest(request)範圍內屬性的變化,實現該介面的監聽器需要實現attributeAdded、attributeRemoved、attributeReplaced三個方法。
應用程式完全可以採用一個監聽器來監聽多個事件,只要讓該監聽器實作類別同時實現多個監聽器介面即可,不如同時實現ServletRequestListener、ServletRequesetAttributeListener。
使用HttpSessionListener和HttpSessionAttributeListener與使用ServletRequestListener和ServletRequestAttributeListener非常相似。
HttpSessionListener用於監聽使用者session的建立和銷毀,實現該介面的監聽器需要實現:
- sessionCreate(HttpSessionEvent se);使用者與伺服器的會話開始、建立時觸發該方法。
- sessionDestroyed(HttpSessionEvent se);使用者與伺服器的會話斷開、銷毀時觸發該方法。
Web中Listener的建立