Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a means of executing code during the startup of Apache Tomcat 6.x to perform initialization routines, etc.?

I want to initialize a global instance of a class before my Tomcat server completes startup and begins to offer my servlets. If this service somehow fails initialization, I'd like the entire startup sequence to fail as well (or as close to this as possible; it would be pointless for the server to be running otherwise). What's the best way I can accomplish this?

like image 275
NBJack Avatar asked Sep 09 '10 20:09

NBJack


1 Answers

Each web application has a ServletContext associated with it. The ServletContext object is created when the application is started and is destroyed when the application is shut down. A ServletContext has a global scope and is similar to a global variable in an application.

http://www.javabeat.net/tips/178-servletcontextlistener-example.html

complete explanation here

http://onjava.com/pub/a/onjava/2001/04/12/listeners.html

like image 126
Aaron Saunders Avatar answered Oct 05 '22 10:10

Aaron Saunders