停止从ServletContextListener方法contextInitialized启动Web应用程序 [英] Stop launch of web app from `ServletContextListener` method `contextInitialized`

查看:273
本文介绍了停止从ServletContextListener方法contextInitialized启动Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在其中实现了 ServletContextListener 通过 contextInitialized 方法,我进行了一些设置工作,并验证了预期的资源是否可用.

I have implemented a ServletContextListener in a Java Servlet web app via the @WebListener annotation. In my contextInitialized method I do some set-up work and verify that expected resources are available.

如果我在该contextInitialized方法中确定出了问题,如何停止执行servlet的Web应用程序继续运行?如果环境不适合(例如,没有可用的数据库),则这些servlet不应执行.

If I determine in that contextInitialized method that something is wrong, how do I stop the web app from continuing onwards with executing servlets? Those servlets should not execute if the environment is not suitable (such as no database available).

如何为基于servlet的Web应用程序正常处理错误的环境?

How to gracefully handle a faulty environment for a servlets-based web app?

推荐答案

不,看来ServletContextListener界面不是 设计的目的,是为了防止网络启动应用程序.

No, it appears that the ServletContextListener interface was not designed with the intent to be able to prevent the launch of a web app.

正如此答案所述,Servlet规范指出ServletContextListener 可能以某种方式禁用遇到异常时访问Web应用程序.该单词may表示可选的,不是必需的.规范也没有确切定义停止访问Web应用程序的含义.

As this Answer states, the Servlet spec says a ServletContextListener may somehow disable access to the web app when an Exception is encountered. That word may means optional, not required. Nor does the spec define exactly what stopping access to the web app means.

显然,在各种Web容器中实现的行为差异很大.有些不执行任何操作,有些则记录并继续运行,有些则阻止了Web应用程序的部署.

Apparently the implemented behavior in various web containers varies widely. Some do nothing, some log it and move on, some prevent the web app from being deployed.

我对Tomcat 8.0.33的经验……将throw new RuntimeException ( "bogus stop servlet " );放在contextInitialized方法中会阻止应用程序的部署. IDE报表中的部署过程中控制台报告失败-上下文路径中已部署的应用程序/但上下文无法启动".不幸的是,该控制台和任何日志都没有捕获实际异常的报告.因此,如果您从一个或多个侦听器中抛出多个异常,调试将不会很明显.

My experience with Tomcat 8.0.33… Putting throw new RuntimeException ( "bogus stop servlet " ); in the contextInitialized method prevents the app from being deployed. The console during deployment in the IDE report reports "FAIL - Deployed application at context path / but context failed to start". Unfortunately neither that console nor none of the logs capture the report of the actual Exception. So if you throw more than one Exception from one or more listeners, debugging will not be obvious.

如Stack Overflow中其他地方所述,最可靠的解决方案可能是将ServletContextListener标记为成功或失败,并在servlet会话中存储一个标志变量.然后让您的Servlet代码检索并检查该标志.然后,您的servlet代码将确定适当的操作过程.您的Web应用程序将被部署,但是您自己的servlet可以选择不执行任何操作并发送回一些HTTP错误代码.

As mentioned elsewhere in Stack Overflow, the most reliable solution is probably to have your ServletContextListener mark success or failure with a flag variable stored in the servlet session. Then have your servlet code retrieve and examine that flag. Your servlet code would then determine the appropriate course of action. Your web app would be deployed, but your own servlet(s) could choose to do nothing and send back some HTTP error code.

类似的问题:

  • Abort java webapp on startup
  • How can I make a ServletContextListener stop the Java EE application?
  • Prefered way to handle Java exceptions in ServletContextListener
  • Prevent Java EE application start on WebSphere on exception
  • ServletContextListener execution order
  • Stop Servlet context from initializing in init method

旁注:添加或编辑ServletContextListener时,您可能需要对项目进行清理并生成"操作.您的IDE的热插拔或开发中部署功能可能无法在新的或更改的侦听器上使用.跟踪您的代码或进行一些日志记录以进行验证.

Side note: When adding or editing your ServletContextListener you may need to do a 'clean-and-build' operation on your project. Your IDE’s hot-swap or deploy-while-developing feature may not pickup on a new or changed listener. Trace your code or do some logging to verify.

这篇关于停止从ServletContextListener方法contextInitialized启动Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆