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:
-
* An example of a test listener
-
* @author zhuli
-
* 2014 7 26
-
*/
-
public class testcontextlister implements servletcontextlistener {
-
-
@Override
-
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 Web application initialization
-
* * process is starting.
-
* * All servletcontextlisteners is notified of the context
-
* * Initialization before any filter or servlet in the Web
-
* * application is initialized.
-
*/
-
-
Public void contextinitialized (servletcontextevent SCE);
-
-
/**
-
* * Notification, the servlet context is on to being shut down.
-
* * All servlets and filters has been destroy () Ed before any
-
* * Servletcontextlisteners is notified of the context
-
* * destruction.
-
*/
-
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
-
the- -- - ,: Wu:01.302: info:/:initializing Spring frameworkservlet' Apiservlet '
-
Java in-depth-web container listener detailed Servletcontextlistener