Load class / action on tomcat startup / deploy

I wrote this post, because few day ago I need to automaticaly load action when my application is deployed on tomcat (see my previous post).
In many case you should load some features on startup of your application on tomcat, like load configuration, connect some stuff, …

Tomcat can do this with a servlet loaded on deployment of your app.

First of all, you need edit the web.xml file present on the WEB-INF directory, to add the following line :


The load-on-startup instruction is used to determine the order of the servlet is loaded. A servlet with a load-on-startup higher will be loaded after all lesser number.


After that we need to create our class wich will load all stuff we need. This one will seem like that :

package yourPackage;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class YourClass extends HttpServlet {

public void init() throws ServletException {
 //Code to load all your needed stuff
public void destroy() {
 //Code in order to destroy all previously loaded stuff when the application is undeployed/exited

With this code your stuff will be loaded on startup with the init method, and detroy on exit with the destroy method

Share and Enjoy:
  • Print
  • Facebook
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • Add to favorites
  • MSN Reporter
  • MySpace

Tags: , , , ,

Leave a Reply