摘要:的生命周期是由它部署的容器控制的。管理生命周期事件你可以通過定義生命周期事件發(fā)生方法的監(jiān)聽器來監(jiān)聽的生命周期并做出反應(yīng)。生命周期事件列表使用注解可以獲取特定程序的各種事件。處理異常執(zhí)行時(shí),可能發(fā)生任意數(shù)量的異常。
Servlet的生命周期是由它部署的容器控制的。當(dāng)一個請求映射到一個Servlet,Servlet容器執(zhí)行下面的步驟。
1 如果不存在這個Servlet的實(shí)例,容器執(zhí)行:
a. 加載Servlet類
b. 創(chuàng)建Servlet類實(shí)例
c. 通過調(diào)用init方法初始化Servlet(initialization is covered in Creating and Initializing a Servlet)
2 容器調(diào)用Servlet的service方法,傳入requset和response參數(shù)。Service方法會在 Writing Service Methods章節(jié)探討。
如果它需要移除這個Servlet,則容器通過調(diào)用Servlet的destroy方法來完成。更多信息需要查看Finalizing a Servlet章節(jié)。
你可以通過定義生命周期事件發(fā)生方法的監(jiān)聽器來監(jiān)聽Servlet的生命周期并做出反應(yīng)。使用這些監(jiān)聽對象,你必須定義并且具體描述這些監(jiān)聽類。
定義監(jiān)聽類你定義的監(jiān)聽類是必須是監(jiān)聽接口的一個實(shí)現(xiàn)。下面的列表定義了可以監(jiān)聽的事件和對應(yīng)的必須實(shí)現(xiàn)的規(guī)范的接口。當(dāng)監(jiān)聽方法被調(diào)用,它將傳入一個適合該事件的容器的信息的事件。例如,一個HttpSessionListener接口的方法會被傳入一個HttpSessionEvent,HttpSessionEvent包含了一個HttpSession。
Servlet 生命周期事件列表
event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent
event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent
event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent
event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent
event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent
event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent
event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent
event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent
event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent
event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent
使用@WebListener注解可以獲取特定web程序的各種事件。使用@WebListener注解的類必須實(shí)現(xiàn)下列的接口之一。
javax.servlet.ServletContextListener javax.servlet.ServletContextAttributeListener javax.servlet.ServletRequestListener javax.servlet.ServletRequestAttributeListener javax.servlet..http.HttpSessionListener javax.servlet..http.HttpSessionAttributeListener處理Servlet異常
Servlet執(zhí)行時(shí),可能發(fā)生任意數(shù)量的異常。當(dāng)一個異常發(fā)生時(shí),web容器自動生成包含下述信息的默認(rèn)頁面:
A Servlet Exception Has Occurred
但你也可以定義特定異常的特定頁面讓容器返回。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/73864.html
摘要:實(shí)際開發(fā)中最常見的和方法配置文件配置文件如下引用百度定義 Servlet全解析筆記 Servlet(Server Applet)是Java Servlet的簡稱,稱為小服務(wù)程序或服務(wù)連接器,用Java編寫的服務(wù)器端程序,具有獨(dú)立于平臺和協(xié)議的特性,主要功能在于交互式地瀏覽和生成數(shù)據(jù),生成動態(tài)Web內(nèi)容,Servlet的常見類型有,繼承自Httpservlet的普通Servlet類、Se...
摘要:協(xié)議版本請求頭客戶端向服務(wù)器端表示,我能支持什么類型的數(shù)據(jù)。在地址欄上輸入項(xiàng)目名稱向報(bào)告,我這個應(yīng)用里面有這個,名字叫做具體的路徑是注冊的映射。的配置,通過這個對象,可以獲取在配置的時(shí)候一些信息先說,在寫怎么用,最后說有什么用。 1.Http協(xié)議 什么是協(xié)議 雙方在交互、通訊的時(shí)候, 遵守的一種規(guī)范、規(guī)則。 http協(xié)議 針對網(wǎng)絡(luò)上的客戶端 與 服務(wù)器端在執(zhí)行http請求的時(shí)候...
摘要:使用技術(shù)開發(fā)應(yīng)用程序深入了解的機(jī)制對應(yīng)用的開發(fā)將有重要的推動作用而想深入了解的機(jī)制就不得不了解包包中包含了個接口個類和個異常類它們分別是接口和類和異常類和的生命周期在的接口中定義了一個的生命周期方法分別是和演示了生命周期方法的簡單在中如何獲 使用 Java 技術(shù)開發(fā) WEB 應(yīng)用程序 , 深入了解 Servlet 的機(jī)制對應(yīng)用的開發(fā)將有重要的推動作用 . 而想深入了解 Servlet ...
閱讀 1228·2023-04-25 20:56
閱讀 2286·2023-04-25 14:42
閱讀 1036·2023-04-25 14:06
閱讀 2876·2021-10-14 09:42
閱讀 2153·2021-09-22 16:03
閱讀 1001·2021-09-13 10:30
閱讀 1356·2019-08-29 15:41
閱讀 1812·2019-08-29 12:55