Java thorough-the listener specific explanation of the Web container Servletcontextlistener

Source: Internet
Author: User

The listener for the Web container Servletcontextlistener is primarily used to monitor container startup and when the destruction needs to do something, listeners will be able to use this.

Servletcontextlistener starts in spring and then starts again.


To implement a simple listener, you need to inherit the interface Servletcontextlistener:

* A Test Listener sample * @author Zhuli * @date 2014-7-26 */public class Testcontextlister implements Servletcontextlistener {    @O Verride public    void contextinitialized (Servletcontextevent sce) {        System.out.println ("==================== ========== container Loading ");    }    @Override public    void contextdestroyed (Servletcontextevent sce) {        System.out.println ("==================== ========== container Destruction ");    }}

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

Public interface Servletcontextlistener extends EventListener {/** * Notification that the Web application initialization * * process is starting. * * All servletcontextlisteners is notified of the context * * Initialization before any filter or servlets in the web * * applic ation is initialized.  *    /public void contextinitialized (Servletcontextevent sce);/** * * Notification that servlet context was about to be shut down. * * All Servlets and filters has been destroy () Ed before any * * Servletcontextlisteners is notified of context * * Destruc tion. *    /public void contextdestroyed (Servletcontextevent sce);}

Configuration in Web. xml:

<listener><listener-class>com.xxx.controller.web.testcontextlister</listener-class></ Listener>

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

============================== container loading 2014-07-26 08:54:01.302:info:/:initializing Spring frameworkservlet ' ApiServlet '



Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Java thorough-the listener specific explanation of the Web container 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.