views:

32

answers:

2

My app has a bit of expensive setup to do when it first starts up. It appears that as soon as I copy the WAR file in webapps, the log file says "Deploying web application archive Navaid.jar", but it doesn't actually run anything until I hit the URL. I'd rather than have the first person to hit the url endure the wait time for this start up. Is there an "onDeploy" method I could use or something to do that processing, or should I just have ant copy the file, wait a few seconds, and then wget the url?

A: 

You can specify a couple of Servlets to load on startup in your web.xml file. They can then call various parts of your app to make sure they are primed.

Aaron Digulla
How does one do that?
Paul Tomblin
+1  A: 

Implement a class with the ServletContextListener interface, then declare it as a listener in your web.xml:

<listener>
  <listener-class> ...your class here... </listener-class>
</listener>

Your class will get called on startup (& on shutdown).

Keith Randall
Awesome, that works. Now I just have to figure out how to get the other servlets to block responding until this is done, but that's another question.
Paul Tomblin
No servlet will be invoked by the container until the call to your ServletContextListener is completed. See http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContextListener.html
Keith Randall