The Servletcontextlistener interface is used to automatically load functions when Tomcat starts, as follows:
First, the class that needs to be loaded must implement the Servletcontextlistener interface.
Two, there are two methods in this interface must be implemented:
1. contextinitialized (Servletcontextevent SCE) This method loads the content when the server is started.
2, contextdestroyed (Servletcontextevent SCE) This method is the content that is loaded when the server shuts down.
Third, such as:
publicclass ReadContext extends HttpServlet implements ServletContextListener{ publicvoid contextInitialized(ServletContextEvent sce){ System.out.println("this is contextInitialized"); } publicvoidcontextDestroyed(ServletContextEvent sce){ System.out.println("this is contextDestroyed"); }}
Iv. Web. XML Configuration Listener Tags
<listener> <listener-class>ReadContext</listener-class></listener>
V. If contextdestroyed does not perform much because Tomcat does not shut down properly or does not implement the Servletcontextlistener interface. The correct way to turn Tomcat off in MyEclipse is to click the right button to close
Servletcontextlistener Interface Usage