tomcat auto start servlet [duplicate]

you could use the servlet context listener. More specifically you could start your thread in the contextInitialized method:

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
         // start the thread
    }

    public void contextDestroyed(ServletContextEvent sce) {
         // stop the thread
    }
}

then add:

<listener>
    <description>ServletContextListener</description>
    <listener-class>MyListener</listener-class>
</listener>

in you web.xml

Leave a Comment