Detailed descriptions of web application initialization instances in Struts2, struts2web

Source: Internet
Author: User

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!

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.