user191776
user191776

Reputation:

Convert Java Servlet to Standalone Java Application

I have a legacy Java servlet that is currently running in a Tomcat container. I want to run it outside the container, as a standalone Java application. The primary intention in doing so is because the new role in which this application will be deployed, involves only computation, and no servicing of requests.

How should I go about modifying the servlet code? Will pulling out the servlet's init() code into the main() method of a new class help?

Upvotes: 1

Views: 1967

Answers (5)

Extract the functionality you want from the servlet to other classes, so that the servlet is only the web interface to the functionality.

Those other classes should ideally not use anything in javax.servlet.*.

Then create a separate class with a main method that uses those other classes, in an appropriate way.

Upvotes: 2

Gertjan Assies
Gertjan Assies

Reputation: 1900

You need to figure out a couple of things first, the functionality in the servlet will probably react to some of the url's parameters. first figure out what the computational part is and what parameters it expects, once you understand that, figuring out what to put in your standalone application will be trivial.

another approach is to embed Jetty in your main application and let that run your servlet,
this will leave your servlet code untouched reducing the risk of introducing bugs

Upvotes: 1

Alonso Dominguez
Alonso Dominguez

Reputation: 7858

I would transform the servlet class into a main class, as you mentioned. The main method of the new class will create an instance of main class, execute init method to initialise if (potentially using the arguments received from the command line). Then invoke the service method inside a try/catch/finally block and invoke the destroy method of the servlet inside the finally block. Of course, your new service method will be invoked without parameters and will not contain any references to the servlet api.

Upvotes: 1

stefan bachert
stefan bachert

Reputation: 9608

You have to do that carefully. It may happen that some filters are doing necessary work not seen within the servlet

Assuming your case is rather simple and a GET or POST just triggers the code, it should be easy to convert into a classical java application.

Just call the former init part and the code from doGet or doPost respectly.

Upvotes: 1

wemu
wemu

Reputation: 8160

yes.

The servlet implements the interface to interact with the Tomcat servlet container. If there are no settings read from the servlet context (path names, configuration paramters) you can extract the logic into a main class.

You will need to make sure you do not rely on the request/response scheme in your services. Usually if only one process runs code is much easier to write than in concurrent scenarios. But one cannot be sure there will be no such effect (for example caches that are now request based will not be emptied in standalone).

also remove any servlet api dependencies in your code. It will not work and is no longer required.

good luck!

Upvotes: 1

Related Questions