Sebastian S.
Sebastian S.

Reputation: 513

Java Tomcat initialize a servlet from a thread

I have a Tomcat 7 application that starts a Thread at startup and after doing some things I want to call "something" to initialize/start a servlet thats deployed on the Tomcat.

Any Ideas?

Upvotes: 0

Views: 244

Answers (2)

Sebastian S.
Sebastian S.

Reputation: 513

Here is the solution:

 private boolean startup() throws ClientProtocolException, IOException {
   logger.entry();

   HttpPost request = new HttpPost("http://.../StartupServlet");

  RequestConfig requestConfig = RequestConfig.custom()
           .setSocketTimeout(30 * 1000)
           .setConnectionRequestTimeout(30 * 1000)
           .setConnectTimeout(30 * 1000)
           .build();
  request.setConfig(requestConfig);

  CloseableHttpClient client = HttpClients.createDefault();
  CloseableHttpResponse response = client.execute(request);

  if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) {
     return false;
  }

  logger.exit();
  return true;
}

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

Servlets are initialised by servlet container in one of two ways:

  1. On startup, if servlet is marked as loadOnStartup then it will be intialized when the war file is deployed

or

  1. On the first request

If your servlet is not marked to be loaded on startup then simply send an Http request to your servlet from your Thread. You can use HttpURLConnection or any similar API to do that.

Upvotes: 1

Related Questions