Start / stop a web application from itself?

前端 未结 3 1819
故里飘歌
故里飘歌 2020-12-16 23:13

I\'ve made a web application using Java, Struts and running over Apache Server and Tomcat. It would be very useful to be able to restart the application from the web. I don\

3条回答
  •  自闭症患者
    2020-12-16 23:51

    I took a quick look at the source code for the Tomcat Manager. It looks like there's a class that's part of the Tomcat source called "Container Servlet". From the javadocs:

    A ContainerServlet is a servlet that has access to Catalina internal functionality, and is loaded from the Catalina class loader instead of the web application class loader.

    A ContainerServlet automatically gets passed a wrapper that can be used to get the Context and Deployer -- and the Deployer has helpful methods such as start(String contextPath) and stop(String contextPath) that will do what you want.

    So, what I think you would need to do is write your own servlet that inherits from ContainerServlet, and configure Tomcat to load your servlet using the Catalina class loader (look at how the Manager is configured to see how). Note that this is probably not going to be an option for you in a hosted environment.

    Then your servlet could have, say, a button you press to reload the application. I recommend putting password-protection of some kind in front of that. :)

提交回复
热议问题