教育行業(yè)A股IPO第一股(股票代碼 003032)

全國咨詢/投訴熱線:400-618-4000

Servlet對象的生命周期詳細(xì)介紹

更新時(shí)間:2021年10月28日10時(shí)44分 來源:傳智教育 瀏覽次數(shù):

好口碑IT培訓(xùn)

監(jiān)聽域?qū)ο蟮纳芷冢?/strong>在Web應(yīng)用程序的運(yùn)行期間,Web容器會(huì)創(chuàng)建和銷毀三個(gè)比較重要的對象ServletContext、HttpSession和ServletRequest,這些對象被稱為域?qū)ο?,為了監(jiān)聽這些域?qū)ο蟮纳芷?,Servlet API中專門提供三個(gè)接口ServletContextListener、HttpSessionListener、ServletRequestListener,它們分別用于監(jiān)聽ServletContext對象的生命周期、監(jiān)聽 HttpSession對象的生命周期、監(jiān)聽 ServletRequest對象的生命周期,接下來將針對這三個(gè)接口進(jìn)行講解。


ServletContextListener接口

ServletContext對象是Web應(yīng)用程序中一個(gè)非常重要的對象,為了監(jiān)聽該對象的創(chuàng)建與銷毀過程,Servlet API中提供了一個(gè)ServletContextListener接口,當(dāng)在Web應(yīng)用程序中注冊一個(gè)或多個(gè)實(shí)現(xiàn)了ServletContextListener 接口的事件監(jiān)聽器時(shí),Web容器在創(chuàng)建或銷毀每個(gè)ServletContext對象時(shí)就會(huì)產(chǎn)生一個(gè)與其對應(yīng)的事件對象,然后依次調(diào)用每個(gè)ServletContext事件監(jiān)聽器中的處理方法,并將ServletContext事件對象傳遞給這些方法,來完成事件的處理工作。

ServletContextListener,接口中共定義了兩個(gè)事件處理方法,具體如下。


1.contextInitialized()方法

contextInitialized()方法的完整語法定義如下。

public void contextInitialized(servletContextEvent sce)

當(dāng)ServletContext對象被創(chuàng)建時(shí),Web容器會(huì)調(diào)用contextInitialized()方法。contextInitialized()方法接收一個(gè)ServletContextEvent類型的參數(shù),contextInitialized()方法內(nèi)部可以通過這個(gè)參數(shù)來獲取創(chuàng)建的ServletContext對象。


2. contextDestroyed()方法

contextDestroyed()方法的完整語法定義如下。

public void contextDestroyed(servletContextEvent sce)

當(dāng)ServletContext對象即將被銷毀時(shí),Web容器會(huì)調(diào)用contextDestroyed()方法,并將servletContextEvent對象傳遞給這個(gè)方法。


HttpSessionListener接口

HttpSession用于完成會(huì)話操作,為了監(jiān)聽HttpSession對象的創(chuàng)建和銷毀過程,ServletAPI中提供了一個(gè)HttpSessionListener接口,當(dāng)Web應(yīng)用程序注冊了一個(gè)或者多個(gè)實(shí)現(xiàn)了HttpSessionListener接口的事件監(jiān)聽器時(shí),Web容器

HttpSession對象時(shí)就會(huì)產(chǎn)生一個(gè)HttpSessionEvent事件對象,然后依次調(diào)用每個(gè)HttpSession事件監(jiān)聽器中的相應(yīng)方法﹐并將HttpSessionEVent事件對象傳遞給這些方法。

HttpSessionListener接口中共定義了兩個(gè)事件處理方法,分別是SessionCreated()和sessionDestroy()方法,接下來針對這兩個(gè)方法進(jìn)行講解。


1.sessionCreated()方法

sessionCreated(方法的完整語法定義如下。

public void sessionCreated(HttpsessionEvent se)

每當(dāng)一個(gè)HttpSession對象被創(chuàng)建時(shí),Web容器都會(huì)調(diào)用sessionCreated()方法。sessionCreated()方法接收一個(gè)HttpSessionEvent類型多參數(shù),sessionCreate()方法內(nèi)部都可以通過這個(gè)參數(shù)來獲取當(dāng)前被創(chuàng)建的HttpSession對象。


2.sessionDestroyed()方法

sessionDestroyed()方法的完整語法定義如下。

public void sessionDestroyed(HttpSessionEvent se)

每當(dāng)一個(gè)HttpSession對象即將被銷毀時(shí), Web容器都會(huì)調(diào)用sessionDestroyed()方法,并將HttpSessionEvent事件對象傳遞給這個(gè)方法。


ServletRequestListener接口

ServletRequest對象用于獲取客戶端發(fā)送的請求數(shù)據(jù),為了監(jiān)聽ServletReauest對象的創(chuàng)建和銷毀過程,Servlet API提供了ServletReauestListener接口,當(dāng)Web應(yīng)用程序中注冊了一個(gè)或多個(gè)實(shí)現(xiàn)了ServletRequestListener接口的事件監(jiān)聽器時(shí),Web容器在創(chuàng)建或銷毀每個(gè)ServletRequest對象時(shí)都會(huì)產(chǎn)生一個(gè)ServletRequestEvent事件對象,然后依次調(diào)用每個(gè)ServletRequest事件監(jiān)聽器中的相應(yīng)處理方法。

ServletRequestListener接口中定義了兩個(gè)事件外理方法,分別是requestInitialized()方法和requestDestroyed()方法,接下來針對這兩個(gè)方法進(jìn)行講解。


1. requestInitialized()方法

requestInitialized()方法的完整語法定義如下。

public void requestInitialized(ServletRequestEvent sre)

每當(dāng)一個(gè)ServletRequest對象創(chuàng)建時(shí),Web容器都會(huì)調(diào)用requestInitialized()方法。requestInitialized()方法接收一個(gè)ServletRequestEvent類型的參數(shù),requestInitialized()方法內(nèi)部可以通過這個(gè)參數(shù)來獲取當(dāng)前創(chuàng)建的ServletRequest對象。


2. requestDestroyed()方法

requestDestroyed()方法的完整語法定義如下。

public void requestDestroyed(ServletRequestEvent sre)

每當(dāng)一個(gè)ServletRequest對象銷毀時(shí),Web容器都會(huì)調(diào)用reauestDestroyed()方法,并將ServletRequestEvent對象傳遞給這個(gè)方法。



0 分享到:
和我們在線交談!