監聽tomcat服務器啟動/關閉很簡單(2步):
1. 建立一個類實現ServletContextListener接口,重寫其中的方法(contextDestroyed和contextInitialized)。
package action; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class SystemListener implements ServletContextListener{ @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub } }
2. 在web.xml中注冊該監聽器。如下:
<listener> <description>監聽tomcat</description> <display-name>test</display-name> <listener-class>listener.SystemListener</listener-class> </listener>
監聽tomcat啟動和關閉就這么簡單地完成了。可以把一些tomcat啟動時就需要初始化的屬性和需要完成的操作放到這步來完成。
下面是完整的項目代碼:
1. 目錄結構
2. 監聽類
package listener; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Properties; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import pub.SystemProperty; /** * 系統上下文啟動/關閉監聽 * 把需要初始化的數據放在contextInitialized方法里 * * @author Sky * @date 2016年8月10日 上午9:32:09 listener */ public class SystemListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent arg0) { System.out.println("tomcat關閉了..."); } @Override public void contextInitialized(ServletContextEvent arg0) { System.out.println("tomcat啟動了..."); /** * 讀取系統運行基本參數 * 讀取靜態參數 */ InputStream input = null; try { input = SystemListener.class.getClassLoader().getResourceAsStream( "sqlProperty.properties"); Properties p = new Properties(); p.load(input); String sqlPort = p.getProperty("port", "3306"); SystemProperty.port = (sqlPort == null || "".equals(sqlPort)) ? 3306 : Integer.parseInt(sqlPort); SystemProperty.password = p.getProperty("password"); System.out.println("系統初始化成功"); } catch (Throwable e) { e.printStackTrace(); System.out.println("系統配置文件SystemProperty.properties不存在或讀取錯誤"); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 讀取時間配置 * 讀取其他配置文件,並把配置文件的數據加入靜態容器中,以便通過類名取出使用 */ input = null; try { input = SystemListener.class.getClassLoader().getResourceAsStream( "timeProperty.properties"); Properties p = new Properties(); p.load(input); //列舉 Enumeration<?> propNames = p.propertyNames(); while (propNames.hasMoreElements()) { String key = (String) propNames.nextElement(); String value = p.getProperty(key, ""); SystemProperty.propList.put(key, value); //放入靜態容器中 } System.out.println("時間配置初始化成功"); } catch (Throwable e) { e.printStackTrace(); System.out.println("時間配置文件timeProperty.properties不存在或讀取錯誤"); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
3. struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="abc" namespace="/" extends="struts-default"> <action name="test" class="action.TestAction"> <result name="input">/index.jsp</result> <result name="success">/success.jsp</result> <result name="fail">/fail.jsp</result> </action> </package> </struts>
3. web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <listener> <description>監聽tomcat</description> <display-name>test</display-name> <listener-class>listener.SystemListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
4. 靜態屬性、容器類(方便使用類名直接調用)
package pub; import java.util.HashMap; import java.util.Map; /** * 靜態存儲系統運行所需的基本參數,在啟動的時候被初始化 * * @author Sky * @date 2016年8月10日 上午10:19:12 * pub */ public class SystemProperty { /* * 設置為靜態,直接使用類名調用 */ public static int port; //端口號 public static String password; //密碼 //靜態容器,用來存儲后面擴展添加的配置(添加其他的配置文件,使得本類來調用) public static Map<String, String> propList = new HashMap<String, String>(); }
5. properties配置文件
sqlProperty.properties
#端口號
port=1234
#密碼
password=6666
timeProperty.properties
#默認時間2秒
normalTime=2000
#一天
oneDay=86400000
6. 測試(使用)類
package action; import pub.SystemProperty; import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport { private static final long serialVersionUID = 1L; @Override public String execute() throws Exception { // 因為是靜態變量,所以直接從類名.變量的方式獲取 int port = SystemProperty.port; String password = SystemProperty.password; System.out.println("port=" + port); System.out.println("password=" + password); //從靜態容器中取 String normalTime = SystemProperty.propList.get("normalTime"); String oneDay = SystemProperty.propList.get("oneDay"); System.out.println("normalTime=" + normalTime); System.out.println("oneDay=" + oneDay); return SUCCESS; } }
7. 輸出結果如下:
如有問題,請聯系QQ:409169399,答案:Sky。