package test; import javax.servlet.*; public class MyCode implements ServletContextListener { //當Tomcat啟動時會執行contextInitialized() public void contextInitialized(ServletContextEvent e) { new MyThread().start(); } public void contextDestroyed(ServletContextEvent e) { } class MyThread extends Thread { public void run() { System.out.println("測試開始"); } } }
web.xml 添加下面的就可以了
<listener> <listnenr-class> test.MyCode </listener-class> </listener>