0

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?

2
  • 1
    Why do you want to 'initialize' a servlet by yourself? Is there any particular reason? And what is it that you want to do when you initialize a servlet? Commented Feb 19, 2015 at 9:56
  • If you are using a listener to listen to application startup, server will definitely wait for it to complete before it initializes servlets. So, you may not need to do anything. docs.oracle.com/javaee/6/api/javax/servlet/… Commented Feb 19, 2015 at 9:59

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes it did, see my solution below. Thanks!
@SebastianS. Did it serve your purpose?
0

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; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.