Struts2中實現(xiàn)web應(yīng)用的初始化實例詳解
Struts2中實現(xiàn)web應(yīng)用的初始化實例詳解
在JavsSE中,main方法為應(yīng)用提供了入口,而在Android中,我們可以使用Application對于整個應(yīng)用的生命周期進行管理,那么在基于Struts2的JavaEE應(yīng)用中,如何實現(xiàn)類似的功能呢。
其中一種比較好的方式,是通過實現(xiàn)ServletContextListener接口進行堅挺,重寫contextInitialized方法,實現(xiàn)自己需要進行的初始化操作,之后在web.xml中添加相應(yīng)的listner,tomcat在啟動服務(wù)時會調(diào)用相應(yīng)方法。
lintener 代碼:
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 ... ");
//系統(tǒng)的初始化工作
//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>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
JSP 開發(fā)之Servlet解決網(wǎng)頁緩存問題
這篇文章主要介紹了JSP 開發(fā)之Servlet解決網(wǎng)頁緩存問題的相關(guān)資料,原理在不需要緩存的頁面中需要實現(xiàn)不緩存頁面,需要的朋友可以參考下2017-08-08
eclipse配置tomcat開發(fā)Dynamic Web Project環(huán)境圖解
這篇文章主要介紹了基于Eclipse進行Dynamic Web Project項目開發(fā)環(huán)境的整合,大家參考使用吧2013-12-12

