ServletContextListener接口用法
阿新 • • 發佈:2018-10-10
pan views number servlet xtend void 不執行 tag web.xml配置
ServletContextListener接口用於tomcat啟動時自動加載函數,方法如下:
一、需加載的類必須實現ServletContextListener接口。
二、該接口中有兩個方法必須實現:
1、contextInitialized(ServletContextEvent sce)該方法為服務器起動時加載內容。
2、contextDestroyed(ServletContextEvent sce)該方法為服務器關閉時加載的內容。
三、如:
public class ReadContext extends HttpServlet implements ServletContextListener{
public void contextInitialized(ServletContextEvent sce){
System.out.println("this is contextInitialized");
}
public void contextDestroyed(ServletContextEvent sce){
System.out.println("this is contextDestroyed" );
}
}
四、web.xml配置listener標簽
<listener>
<listener-class>ReadContext</listener-class>
</listener>
五、如果contextDestroyed不執行多是因為tomcat沒有正常關閉或是沒有實現ServletContextListener接口。在MyEclipse關閉tomcat的正確方法是:點擊右鍵點關閉
ServletContextListener接口用法