Detailed descriptions of web application initialization instances in Struts2, struts2web
Detailed descriptions of web application initialization instances in Struts2
In JavsSE, the main method provides an entry for the Application. In Android, we can use the Application to manage the entire Application lifecycle. In this case, in Java EE applications based on Struts2, how can we implement similar functions.
One of the better methods is to rewrite the contextInitialized method by implementing the ServletContextListener interface to implement the initialization operations you need, and then perform the following operations on the web. add the corresponding listner in xml. tomcat will call the corresponding method when starting the service.
Lintener code:
Package listener; import javax. servlet. servletContextEvent; import javax. servlet. servletContextListener; public class InitListener implements ServletContextListener {public void contextDestroyed (ServletContextEvent sce) {System. out. println ("web exit... ");} public void contextInitialized (ServletContextEvent sce) {System. out. println ("web init... "); // initialization of the system // TODO }}
Web. xml
<?xml version="1.0" encoding="UTF-8"?> <web-app> <listener> <listener-class>fangwei.listener.InitListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!