如何在 Tomcat 8 中设置上下文路径

How to set context path in Tomcat 8

我有一个名为 FooBar.war 的 war 文件,现在我想设置上下文路径“/”指向此 Web 应用程序。这意味着用户可以通过 URL: localhost:8080/ 访问 webapp 为此,我在 <catalina_home>/conf/Catalina/localhost/ 中添加了一个名为 ROOT.xml 的文件,内容为

<Context docBase="/opt/mywebapps/FooBar" path="" reloadable="true" />

我也把FooBar.war放到了文件夹/opt/mywebapps/ 然后设置这个文件夹的所有权限:

chmod -R 777 /opt/mywebapps/

启动Tomcat服务器后,服务器无法部署war文件:

1-Apr-2019 11:33:36.211 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
 org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:629)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1839)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.webresources.StandardRoot@6023aae1]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
    at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4932)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5067)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 10 more
Caused by: java.lang.IllegalArgumentException: The main resource set specified [/opt/mywebapps/FooBar] is not valid
    at org.apache.catalina.webresources.StandardRoot.createMainResourceSet(StandardRoot.java:748)
    at org.apache.catalina.webresources.StandardRoot.startInternal(StandardRoot.java:706)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 13 more

如果您有任何解决办法,请帮助我。

<Context docBase="/opt/mywebapps/FooBar" path="" reloadable="true" />

And I also put FooBar.war into folder /opt/mywebapps/

请注意,您现在有 /opt/mywebapps/FooBar.war,而您正在引用 /opt/mywebapps/FooBar。 (注意“.war”的区别)

因为您不在 Tomcat 自己的 webapps 文件夹中,其中(使用默认配置)WAR 文件将自动解压缩,Tomcat 只是表明位置您配置的那个根本不存在。