SERVLET3.0 Learning Summary (iv)--using annotation callout listener (Listener)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.