Deploying a Java Application as a Servlet

I have a Java application that so far has been running as a standalone Java application (i.e. an executable jar). Now I need to deploy it to Tomcat as a servlet. However, it does not need to process HTTP requests, but it needs to be launched using tomcat.

What are the steps required to transform a project so that it can be deployed to Tomcat? I use maven as a build tool and Java 1.5.

+3
source share
2 answers

I understand that you want to run this application when the server starts. The best way would be to implement ServletContextListenerand run the application in a method contextInitialized(). For instance.

public class Config implements ServletContextListener {

    private YourApp yourApp;

    public void contextInitialized(ServletContextEvent event) {
        yourApp = new YourApp();
        yourApp.start();
    }

    public void contextDestroyed(ServletContextEvent event) {
        yourApp.shutdown();
    }

}

web.xml :

<listener>
    <listener-class>com.example.Config</listener-class>
</listener>

. HttpServlet, HTTP- .

, , . , Runnable ExecutorService.

+5

, , /- (, Tomcat/Jetty), . , :

  • AbstractHttpServlet , , init(). .
  • a web.xml, , load-on-startup 1 (, , )
  • .war

2 , init() / HTTP- ( ).

- javaservicewrapper Windows .

+2

Source: https://habr.com/ru/post/1733256/


All Articles