开发者

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

开发者 https://www.devze.com 2023-01-15 06:55 出处:网络
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 s

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 p开发者_如何学JAVAointless for the server to be running otherwise). What's the best way I can accomplish this?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消