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?
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?
Servlets are initialised by servlet container in one of two ways:
or
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.
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; }