SERVLET3.0 Learning Summary (iv)--using annotation callout listener (Listener)
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:
1 package Me.gacl.web.listener; 2 3 import javax.servlet.ServletContextEvent; 4 import javax.servlet.ServletContextListener; 5 import Javax.servlet.annotation.WebListener; 6 7/** 8 * Using @weblistener annotations will implement the Myservletcontextlistener labeled Servletcontextlistener interface as Listener 9 */10 @ WebListener11 public class Myservletcontextlistener implements Servletcontextlistener { @Override14 public void contextdestroyed (Servletcontextevent sce) { System.out.println ("Servletcontex destroyed"); }17 18 @Override19 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
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <web-app version= "3.0" 3 xmlns= "http://java.sun.com/xml/ Ns/javaee " 4 xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance " 5 xsi:schemalocation=" http ://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> 7 < Display-name></display-name> 8 <welcome-file-list> 9 <welcome-file>index.jsp </welcome-file>10 </welcome-file-list>11 </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.
SERVLET3.0 Learning Summary (iv)--using annotation callout listener (Listener)