简体   繁体   English

如何从Servlet中关闭Servlet容器?

[英]How to shutdown a servlet container from within a servlet?

Is there a portable way to request a Servlet container to shutdown gracefully , from within a servlet? 是否有一种可移植的方法来请求Servlet容器从Servlet内部正常关闭?

By portable I mean a technique that will work on all standard compliant containers (Tomcat, Jetty, Winstone, etc). 所谓可移植的,是指一种可在所有符合标准的容器(Tomcat,Jetty,Winstone等)上使用的技术。

Note that this is the opposite of the Servlet.destroy() method, which gets called when the container is taking the servlet down. 请注意,这与Servlet.destroy()方法相反,该方法在容器关闭Servlet时调用。

There's no way defined in the Java EE Servlet Spec that I know of, which there would need to be for it to be portable. 据我所知,在Java EE Servlet规范中没有定义任何方法,因此必须具有可移植性。

The link it to the servlet API specs, so if there's such a way, it'll be documented there somewhere. 将其链接到servlet API规范,因此,如果有这种方法,它将在某处记录。

I also agree that it'd be a really bad idea for one servlet to be able to shut down the container! 我也同意,对于一个servlet来说,能够关闭容器是一个非常糟糕的主意!

System.exit(); System.exit()的;

If you are running with no SecurityManager. 如果您没有SecurityManager运行。

EDIT: Is this graceful? 编辑:这是优美吗? This depends on containers. 这取决于容器。

On Tomcat, if you call it with 0 在Tomcat上,如果用0调用

  System.exit(0);

It's as graceful as shutdown.sh or Catalina.stop() because the shutdown hook simply calls stop(). 它与shutdown.sh或Catalina.stop()一样优美,因为shutdown钩子仅调用stop()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM