Vishal
Vishal

Reputation: 2752

Startup Servlet for WAR File

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. :)

Upvotes: 3

Views: 2265

Answers (2)

Jesse
Jesse

Reputation: 3829

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.

Upvotes: 9

chedine
chedine

Reputation: 2374

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

Upvotes: 0

Related Questions