Deploy Application at Tomcat Root – 在Tomcat根部部署应用程序

最后修改: 2017年 11月 15日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick article, we’ll discuss deploying a web application at the root of a Tomcat.

在这篇快速文章中,我们将讨论在Tomcat的根部部署一个Web应用程序。

2. Tomcat Deployment Basics and Terminology

2.Tomcat部署的基础知识和术语

First, the basics of deploying an application to Tomcat can be found in this guide: How to Deploy a WAR File to Tomcat.

首先,将应用程序部署到Tomcat的基础知识可以在本指南中找到。如何将WAR文件部署到Tomcat

Simply put, web applications are placed under $CATALINA_HOME\webapps, where $CATALINA_HOME is the Tomcat’s installation directory.

简单地说,Web应用程序被放在$CATALINA_HOME/webapps下,其中$CATALINA_HOME是Tomcat的安装目录。

The context path refers to the location relative to the server’s address which represents the name of the web application.

上下文路径指的是相对于服务器地址的位置,它代表了网络应用的名称。

By default, Tomcat derives it from the name of the deployed war-file. So if we deploy a file ExampleApp.war, it will be available at http://localhost:8080/ExampleApp. I. e. the context path is /ExampleApp.

默认情况下,Tomcat从部署的战争文件的名称中导出。因此,如果我们部署一个文件ExampleApp.war,它将在http://localhost:8080/ExampleApp。也就是说,上下文路径是/ExampleApp

If we now need to have that app available at http://localhost:8080/ instead, we have a few options, which we’ll discuss in the following sections.

如果我们现在需要让该应用程序在http://localhost:8080/上可用,我们有几个选择,我们将在下面几节中讨论。

For a more detailed explanation of the context concept of Tomcat, have a look at the official Tomcat documentation.

关于Tomcat的上下文概念的更详细解释,请看官方的Tomcat文档

3. Deploying the App as ROOT.war

3.将应用程序部署为ROOT.war

The first option is very straightforward: we just have to delete the default /ROOT/ folder in $CATALINA_HOME\webapps, rename our ExampleApp.war to ROOT.war, and deploy it.

第一种方案非常简单明了。我们只需删除/ROOT/ $CATALINA_HOME/webapps中的默认文件夹,将我们的ExampleApp.war重命名为ROOT.war,并将其部署。

Our app will now be available at http://localhost:8080/.

我们的应用程序现在可以在http://localhost:8080/

4. Specifying the Context Path in the server.xml

4.在server.xml中指定上下文路径

The second option is to set the context path of the application in the server.xml (which is located at $CATALINA_HOME\conf).

第二个选择是在server.xml(位于$CATALINA_HOME/conf)中设置应用程序的上下文路径。

We must insert the following inside the <Host> tag for that:

为此,我们必须在<Host>标签内插入以下内容。

<Context path="" docBase="ExampleApp"></Context>

Note: defining the context path manually has the side effect that the application is deployed twice by default: at http://localhost:8080/ExampleApp/ as well as at http://localhost:8080/.

注意:手动定义上下文路径有一个副作用,即应用程序默认部署两次:在http://localhost:8080/ExampleApp/以及http://localhost:8080/

To prevent this, we have to set autoDeploy=”false” and deployOnStartup=”false” in the <Host> tag:

为了防止这种情况,我们必须在<Host>标签中设置autoDeploy=”false”/em>和deployOnStartup=”false”/em>。

<Host name="localhost" appBase="webapps" unpackWARs="true"
  autoDeploy="false" deployOnStartup="false">
    <Context path="" docBase="ExampleApp"></Context>

    <!-- Further settings for localhost -->
</Host>

Important: this option is not recommended anymore, since Tomcat 5: it makes context configurations more invasive, since the server.xml file cannot be reloaded without restarting Tomcat.

重要的是:自Tomcat 5以来,不再推荐使用该选项:它使上下文配置更具侵入性,因为如果不重新启动Tomcat,server.xml文件就不能被重新加载。

5. Specifying the Context Path in an App-Specific XML File

5.在一个特定应用的XML文件中指定上下文路径

To avoid this problem with the server.xml, we’ve got the third option: we’ll set the context path in an application-specific XML file.

为了避免server.xml的这个问题,我们有第三个选择:我们将在一个特定于应用程序的XML文件中设置上下文路径。

Therefore, we have to create a ROOT.xml at $CATALINA_HOME\conf\Catalina\localhost with the following content:

因此,我们必须在$CATALINA_HOME\conf\Catalina\localhost创建一个ROOT.xml,内容如下。

<Context docBase="../deploy/ExampleApp.war"/>

Two points are worth nothing here.

这里有两点是不值一提的。

First, we don’t have to specify the path explicitly as in the previous option – Tomcat derives that from the name of our ROOT.xml.

首先,我们不必像前一个选项那样明确指定路径–Tomcat从我们的ROOT.xml的名称中得出。

And second – since we’re defining our context in a different file than the server.xml, our docBase has to be outside of $CATALINA_HOME\webApps.

其次,由于我们在一个不同于server.xml的文件中定义我们的上下文,我们的docBase必须在$CATALINA_HOME\webApps之外。

6. Conclusion

6.结论

In this tutorial, we discussed different options of how to deploy a web application at the root of a Tomcat.

在本教程中,我们讨论了如何在Tomcat的根部部署一个Web应用程序的不同选项。