tags:

views:

49

answers:

2

Hi,

I am deploying a .WAR file on Tomcat v5.5. I want to run a thread on the startup of the war file. So I am thinking of using Startup Servlet for this. But I don't have much knowledge about servlets/startup servlets. Can you guys guide me a bit on how to do it? Any descriptive links on this issues will also be much appreciated. :)

+5  A: 

You can use a javax.servlet.ServletContextListener, and configure it in web.xml, like:

<listener>
    <listener-class>com.mycompany.Listener</listener-class>
</listener>

SerletContextListeners are the correct place to initialize stuff that need the lifetime of your application. It allows you to initialize things when the application is started, and destroy stuff when the application (or app server) is shut down.

Jesse
A: 

Alternatively, you can add

<load-on-startup>1</load-on-startup> 

to your servlet, so that it gets loaded when the app comes up. You could do your init stuff in the init method. You might also have to avoid configuring url patterns for this servlet.

Also have a look at this example

chedine