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?
views:
36answers:
2
+6
A:
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
Aaron Saunders
2010-09-09 20:30:31
That's what I needed. Thanks for the quick and concise help!
NBJack
2010-09-09 20:37:55
can you mark the answer as accepted? it will assist in getting others to respond when you ask
Aaron Saunders
2010-09-09 20:41:48
+1
A:
One thing you can do portably is to implement a servlet that initializes everything you need in its init() method (and maybe call System.exit() if it fails, i do not know if you have permission to do this in Tomcat). Then you would load it using <load-on-startup>
in your web.xml
to specify initialization order.
gpeche
2010-09-09 20:30:47