Servlet3.0 provides @weblistener annotations to define a class that implements a specific listener interface as a listener , so that when we use the listener in a web app, we no longer need to configure the listener's descriptive information in the. xml file.
Let's create a listener and experience using the @weblistener annotation callout listener as follows:
The code for the listener is as follows:
Package Me.gacl.web.listener;import Javax.servlet.servletcontextevent;import Javax.servlet.ServletContextListener ; Import javax.servlet.annotation.weblistener;/** * Using @weblistener annotations will implement the Myservletcontextlistener annotation of the Servletcontextlistener interface as Listener */@WebListenerpublic class Myservletcontextlistener implements Servletcontextlistener { @Override public void contextdestroyed ( Servletcontextevent SCE) { System.out.println ("Servletcontex destroyed"); } @Override public void contextinitialized (Servletcontextevent sce) { System.out.println ("Servletcontex initialization") ; System.out.println (Sce.getservletcontext (). Getserverinfo ());} }
The listener is initialized when the Web app starts, as shown in:
With @weblistener annotations, our web. XML does not have to be configured
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "3.0"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <Display-name></Display-name> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list></Web-app>
The advent of the SERVLET3.0 specification allows us to develop servlet, filter, and listener programs that implement zero configuration in Web. Xml.
Transferred from: http://www.cnblogs.com/xdp-gacl/p/4226851.html
Servlet uses annotation callout listener (Listener)