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

I want to initialize a global instance of a class before my Tomcat server completes startup and begins

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?

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?

Share Improve this question edited Oct 23, 2015 at 6:50 jruizaranguren 13.6k7 gold badges58 silver badges74 bronze badges asked Sep 9, 2010 at 20:25 NBJackNBJack 3154 silver badges13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 19

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1739194552a4109939.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信