Java in-depth-web container listener detailed Servletcontextlistener

Source: Internet
Author: User

The Web Container listener Servletcontextlistener is primarily used to listen to the container when it is started and destroyed, and you can use this listener to do something.

The Servletcontextlistener is started before spring starts.


We implement a simple listener that needs to inherit the interface Servletcontextlistener:

  1. * An example of a test listener

  2. * @author zhuli

  3.  *   2014 7 26   

  4.  */   

  5. public class   testcontextlister  implements   servletcontextlistener {  

  6. @Override   

  7.      public void   Contextinitialized (SERVLETCONTEXTEVENT SCE)  {  

  8. System.out.println ("============================== container loading");

  9. }

  10.       @Override    

  11.      public void   Contextdestroyed (SERVLETCONTEXTEVENT SCE)  {  

  12. System.out.println ("============================== container Destruction");

  13. }

  14. }

servletcontextlistener implements two interfaces, one is when the container starts, and the other is when the container is destroyed:

  1. public interface   servletcontextlistener  extends   eventlistener {  

  2. /**

  3. * * Notification that Web application initialization

  4. * * process is starting.

  5. * * All servletcontextlisteners is notified of the context

  6. * * Initialization before any filter or servlet in the Web

  7. * * application is initialized.

  8.      */   

  9. Public   void contextinitialized (servletcontextevent SCE);

  10. /**

  11. * * Notification, the servlet context is on to being shut down.

  12. * * All servlets and filters has been destroy () Ed before any

  13. * * Servletcontextlisteners is notified of the context

  14. * * destruction.

  15.      */   

  16. Public   void contextdestroyed (servletcontextevent SCE);

  17. }


Configuration in Web. xml:

  1. < Listener >   

  2. <Listener-class>Com.xxx.controller.web.TestContextLister</Listener-class>  

  3. </ Listener >   


When the container starts, it is seen in the log that the container starts:

  1. ============================== Container Loading

  2. the- -- -  ,: Wu:01.302: info:/:initializing Spring frameworkservlet' Apiservlet '  



Java in-depth-web container listener detailed Servletcontextlistener

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.