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>
*/
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>
*/
然后通过servlet启动这个线程.
在servlet的init()方法中启动你要启动的线程.
不知道合不合适你.
启动Servlet的方法你应该也知道吧.
搞得那么麻烦。
用servlet就够了
不需要用什么listener之类的东西了
又不是搞监控。