随着tomcat一起启动一个线程
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>