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()
        {
            //.....
        }
    }
}
/* web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<listener>
<listnenr-class>test.MyCode</listener-class>
</listener>
</web-app>
*/

猜你喜欢

转载自z-408.iteye.com/blog/1844433