1. Create Dynamic Web Project, Dynamic Web module version Select 3.0
2. Add metadata-complete= "false" to the auto-generated Web. XML configuration
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Javaee:web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 3 xmlns= "Http://Java.sun.com/xml/ns/javaee"4 Xmlns:javaee= "Http://java.sun.com/xml/ns/javaee" 5 Xmlns:web= "Http://java.sun.com/xml/ns/javaee" 6 xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"7 Metadata-complete= "false"version= "3.0">8<Javaee:display-name></Javaee:display-name>9<javaee:welcome-file-list>Ten <Javaee:welcome-file>Index.html</Javaee:welcome-file> One</javaee:welcome-file-list> A </Javaee:web-app>
3. Create a listener class to add annotations to the head of the Listener class @WebListener
Package Com.xhkj.listener;
Import Javax.servlet.servletcontextevent;import Javax.servlet.servletcontextlistener;import Javax.servlet.annotation.WebListener; @WebListenerpublic class Myservletcontextlistener implements Servletcontextlistener {@Override public void contextdestroyed (Servletcontextevent sce) { System.out.println ("=========================== Myservletcontextlistener destruction "); } @Override public void contextinitialized (Servletcontextevent sce) { System.out.println ("================== =========myservletcontextlistener initialization "); System.out.println (Sce.getservletcontext (). Getserverinfo ());} }
4. Start the Tomcat service. Printing results are as follows
5 precautions, each time you modify the configuration or Java code, you want to recompile, otherwise it will not work
@WebListener Annotation for monitoring