When starting the Web App backend service, it is sometimes necessary to pre-configure some global variables such as the database or the configuration file to read the information
At this point, you can use Servletcontextlistener to load the Setup basic configuration when you start the service
The implementation is as follows:
(1) Create a class named Servletlistener (placed in the Com.settings package),
Some of the things that need to be configured to start a service can be handled in the contextinitialized () method, as follows:
Packagecom.settings;Importjavax.servlet.ServletContextEvent;ImportJavax.servlet.ServletContextListener; Public classServletlistenerImplementsservletcontextlistener{@Override Public voidcontextdestroyed (servletcontextevent arg0) {//TODO auto-generated Method Stub} @Override Public voidcontextinitialized (servletcontextevent arg0) {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Load Configuration"); }}
(2) Configure the Web. xml file First
<listener> <description>server listener</description> <listener-class> com.settings.servletlistener</listener-class> </listener>
(3) Start Tomcat, you can see the output "load Configuration" in the console
(@[email protected]) Y, Finish ~
Java Web--Servletcontextlistener