Home  >  Article  >  Web Front-end  >  Detailed explanation of initialization examples of web applications in Struts2

Detailed explanation of initialization examples of web applications in Struts2

韦小宝
韦小宝Original
2018-01-04 10:18:211649browse

This article mainly introduces the relevant information on the detailed explanation of the initialization example of the web application in Struts2. Friends who are interested in the web application can refer to the implementation of in Struts2. webDetailed explanation of the initialization instance of the application

Detailed explanation of the initialization instance of the web application in Struts2

In JavsSE, the main method provides the entrance for the application, and in In Android, we can use Application to manage the entire application life cycle, so how to implement similar functions in JavaEE applications based on Struts2.

One of the better ways is to implement the ServletContextListener interface, rewrite the contextInitialized method, implement the initialization operations you need, and then add the corresponding listner and tomcat in web.xml The corresponding method will be called 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 ... ");   
    //系统的初始化工作   
    //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, I hope it can help everyone, thank you for your support of this site!

Related recommendations:

Analysis of technical trends that web developers must know in 2018

Redis in 11 web application scenarios Give full play to

Tell you how to ensure the security of Web applications

The above is the detailed content of Detailed explanation of initialization examples of web applications in Struts2. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn