问题描述
我有一个servlet,它在servlet的init()
方法中的服务器启动<load-on-startup>1</load-on-startup>
上加载,我正在从属性文件中检查某些参数.如果该参数不存在,我想停止整个上下文的初始化.
I have a servlet which is loaded on server startup <load-on-startup>1</load-on-startup>
in the init()
method of the servlet I am checking for some parameter from a properties file. If the Parameter is not there, I want to stop the Entire Context from initializing.
public void init(){
Readproperty pr = new ReadProperty();
if(!pr.parameterExists()){
//Edit:
throw new UnavailableException("Testing Stopping Context")
}
}
做到这一点的最佳方法是什么?我现在确实想将此代码移到我的上下文侦听器类中,因此我正在寻找从"init()"方法执行此操作的最佳方法.
What is the best way to do this? I do now want to Move this Code to my Context Listener Class so I am looking for the best way to do it from the `init()' method.
推荐答案
您无法阻止Web应用程序从servlet启动.
You can't stop a web application from starting from a servlet.
如果要停止启动Web应用程序,请将测试移至ServletContextListener
. contextInitialized()
方法中的异常将阻止Web应用程序启动.
If you want to stop the web application from starting move your test(s) to a ServletContextListener
. An exception in the contextInitialized()
method will stop the web application from starting.
如果您正在使用最新的Tomcat 7.0.x或8.0.x版本,则可以在Context或Host上使用特定于Tomcat的选项failCtxIfServletStartFails
,如果启动servlet上的任何加载失败,它将导致Web应用程序失败但这是非标准的. ServletContextListener
是更好的选择.
If you are using a recent Tomcat 7.0.x or 8.0.x release you can use a Tomcat specific option failCtxIfServletStartFails
on the Context or Host that will cause the web application to fail if any of the load on startup servlets fail but this is non-standard. The ServletContextListener
is the better option.
这篇关于阻止Servlet上下文在init方法中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!