If you do not want to intercept HTTP requests, just do not extend the HttpServlet . It makes no sense. If you really want to run it as a "background job" when you start webapp and stop it when you finish webapp, then just run ServletContextListener accordingly.
public class Config implements ServletContextListener { private YourApp yourApp; public void contextInitialized(ServletContextEvent event) { yourApp = new YourApp(); yourApp.start(); } public void contextDestroyed(ServletContextEvent event) { yourApp.stop(); } }
which you can register in web.xml as follows:
<listener> <listener-class>com.example.Config</listener-class> </listener>
If YourApp does not actually run the task in a separate thread, you need to wrap it in Runnable and execute it using the ExecutorService . For instance.
public class Config implements ServletContextListener { private ExecutorService executor; public void contextInitialized(ServletContextEvent event) { executor = Executors.newSingleThreadExecutor(); executor.submit(new YourApp());
If, in the end, your webapp has nothing , then I ask a question about its launch in the servletcontainer. Instead, simply run it as a stand-alone Java application using the main() method.
source share