Web中Listener的建立

來源:互聯網
上載者:User

標籤: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的建立

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.