How to Deploy a WAR File to Tomcat – 如何将WAR文件部署到Tomcat上

最后修改: 2016年 9月 1日

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

1. Overview

1.概述

Apache Tomcat is one of the most popular web servers in the Java community. It ships as a servlet container capable of serving Web Archives with the WAR extension.

Apache Tomcat是Java社区中最受欢迎的Web服务器之一。它作为一个servlet容器,能够为带有WAR扩展的Web档案服务。

It provides a management dashboard from which we can deploy a new web application, or undeploy an existing one without having to restart the container. This is especially useful in production environments.

它提供了一个管理仪表板,我们可以从这里部署一个新的Web应用程序,或撤销一个现有的应用程序,而不必重新启动容器。这在生产环境中特别有用。

In this tutorial, we’ll do a quick overview of Tomcat, and then cover various approaches to deploying a WAR file.

在本教程中,我们将对Tomcat做一个快速概述,然后介绍部署WAR文件的各种方法。

2. Tomcat Structure

2.Tomcat的结构

Before we begin, we should familiarize ourselves with some terminology and environment variables.

在我们开始之前,我们应该熟悉一些术语和环境变量。

2.1. Environment Variables

2.1.环境变量

If we’ve worked with Tomcat before, these will be familiar to us.

如果我们以前与Tomcat合作过,这些都是我们熟悉的。

This variable points to the directory where our server is installed:

这个变量指向我们的服务器所安装的目录。

$CATALINA_HOME

$CATALINA_HOME

This variable points to the directory of a particular instance of Tomcat (we may have multiple instances installed):

这个变量指向Tomcat的一个特定实例的目录(我们可能安装了多个实例)。

$CATALINA_BASE

$CATALINA_BASE

If this variable isn’t set explicitly, then it’ll be assigned the same value as $CATALINA_HOME.

如果这个变量没有明确设置,那么它将被赋予与$CATALINA_HOME相同的值。

Web applications are deployed under the $CATALINA_HOME\webapps directory.

网络应用被部署在$CATALINA_HOME\webapps目录下。

2.2. Terminology

2.2.术语

Document root – Refers to the top-level directory of a web application where all the application resources are located, like JSP files, HTML pages, Java classes, and images.

文档根–指的是Web应用程序的顶级目录,所有的应用程序资源都位于此,如JSP文件、HTML页面、Java类和图像。

Context path – Refers to the location that’s relative to the server’s address and represents the name of the web application.

上下文路径 – 指的是与服务器地址相对的位置,代表网络应用程序的名称。

For example, if our web application is put under the $CATALINA_HOME\webapps\myapp directory, it’ll be accessed by the URL http://localhost/myapp, and its context path will be /myapp.

例如,如果我们的网络应用被放在$CATALINA_HOME\webapps\myapp目录下,它将被URL http://localhost/myapp访问,其上下文路径将是/myapp

WAR – Short for Web Archive. It’s the extension of a file that packages a web application directory hierarchy in ZIP format. Java web applications are usually packaged as WAR files for deployment. These files can be created on the command line or with an IDE, like Eclipse.

WAR –网络档案的简称。它是一个文件的扩展名,以ZIP格式打包网络应用程序目录层次结构。Java网络应用程序通常被打包成WAR文件进行部署。这些文件可以在命令行上创建,也可以用IDE创建,如Eclipse。

After deploying the WAR file, Tomcat unpacks it and stores all the project files from the webapps directory in a new directory named after the project.

在部署WAR文件后,Tomcat将其解压并将webapps目录下的所有项目文件存储在一个以项目命名的新目录中。

3. Tomcat Setup

3.Tomcat的设置

The Tomcat Apache web server is free software that can be downloaded from their website. It’s a requirement that the user’s machine has a JDK available, and that the JAVA_HOME environment variable is set correctly.

Tomcat Apache Web 服务器是免费软件,可以从其网站下载。这是一个要求,即用户的机器有JDK可用,并且JAVA_HOME环境变量被正确设置。

3.1. Start Tomcat

3.1.启动Tomcat

We can start the Tomcat server by simply running the startup script located at $CATALINA_HOME\bin\startup. There’s a .bat and .sh in every installation.

我们可以通过简单运行位于$CATALINA_HOME/binstartupstartup脚本来启动Tomcat服务器。在每个安装中都有一个.bat.sh

We’ll choose the appropriate option depending on whether we’re using a Windows or Unix based operating system.

我们将根据我们使用的是Windows还是基于Unix的操作系统,选择适当的选项。

3.2. Configure Roles

3.2.配置角色

During the deployment phase, we’ll have some options, one of which is to use Tomcat’s management dashboard. To access this dashboard, we must have an admin user configured with the appropriate roles.

在部署阶段,我们会有一些选择,其中之一是使用Tomcat的管理仪表板。要访问这个仪表板,我们必须有一个配置了适当角色的管理员用户。

To have access to the dashboard, the admin user needs the manager-gui role. Later, we’ll need to deploy a WAR file using Maven, and for this we’ll also need the manager-script role too.

要访问仪表板,管理员用户需要manager-gui角色。稍后,我们需要用Maven部署一个WAR文件,为此我们也需要manager-script角色。

Let’s make these changes in $CATALINA_HOME\conf\tomcat-users:

让我们在$CATALINA_HOME\conf\tomcat-users中做这些修改。

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="admin" password="password" roles="manager-gui, manager-script"/>

More details about the different Tomcat roles can be found by following this official link.

关于不同的Tomcat角色的更多细节,可以通过这个官方链接找到。

3.3. Set Directory Permissions

3.3.设置目录权限

Finally, we’ll ensure that there’s read/write permission on the Tomcat installation directory.

最后,我们要确保Tomcat安装目录有读/写权限。

3.4. Test Installation

3.4.测试安装

To test that Tomcat is set up properly, we’ll run the startup script (startup.bat/startup.sh). If no errors are displayed on the console, we can double-check by visiting http://localhost:8080.

为了测试Tomcat是否设置正确,我们将运行启动脚本(startup.bat/startup.sh)。如果控制台没有显示错误,我们可以通过访问http://localhost:8080来仔细检查。

If we see the Tomcat landing page, then we have installed the server correctly.

如果我们看到Tomcat登陆页面,那么我们已经正确安装了服务器。

3.5. Resolve Port Conflict

3.5.解决端口冲突

By default, Tomcat is set to listen to connections on port 8080. If there’s another application that’s already bound to this port, the startup console will let us know.

默认情况下,Tomcat被设置为监听端口8080上的连接。如果有另一个应用程序已经绑定到这个端口,启动控制台会让我们知道。

To change the port, we can edit the server configuration file, server.xml, located at $CATALINA_HOME\conf\server.xml. By default, the connector configuration is:

要改变端口,我们可以编辑服务器配置文件,server.xml,位于$CATALINA_HOME/conf\server.xml.默认情况下,连接器配置为。

<Connector port="8080" protocol="HTTP/1.1" 
  connectionTimeout="20000" redirectPort="8443" />

For instance, if we want to change our port to 8081, then we’ll have to change the connector’s port attribute:

例如,如果我们想把端口改为8081,那么我们就必须改变连接器的端口属性。

<Connector port="8081" protocol="HTTP/1.1" 
  connectionTimeout="20000" redirectPort="8443" />

Sometimes, the port we have chosen is not open by default. In that case, we would need to open the port with the appropriate commands in the Unix kernel, or create the appropriate firewall rules in Windows. We won’t go into further details here as it’s beyond the scope of this article.

有时,我们选择的端口在默认情况下是不开放的。在这种情况下,我们需要在Unix内核中用适当的命令打开端口,或者在Windows中创建适当的防火墙规则。我们不会在这里进一步讨论细节,因为这已经超出了本文的范围。

4. Deploy From Maven

4.从Maven进行部署

If we want to use Maven for deploying our web archives, we must configure Tomcat as a server in Maven’s settings.xml file.

如果我们想用Maven部署网络档案,就必须在Maven的settings.xml文件中将Tomcat配置为服务器。

There are two locations where the settings.xml file may be found:

有两个地方可以找到settings.xml文件。

  • The Maven install: ${maven.home}/conf/settings.xml
  • A user’s install: ${user.home}/.m2/settings.xml

Once we have found it, we’ll add Tomcat:

一旦我们找到它,我们将添加Tomcat。

<server>
    <id>TomcatServer</id>
    <username>admin</username>
    <password>password</password>
</server>

Now we’ll need to create a basic web application from Maven to test the deployment. Let’s navigate to where we would like to create the application.

现在我们需要从Maven中创建一个基本的Web应用来测试部署。让我们导航到我们想创建应用程序的地方。

We’ll run this command on the console to create a new Java web application:

我们将在控制台运行这个命令来创建一个新的Java Web应用程序。

mvn archetype:generate -DgroupId=com.baeldung -DartifactId=tomcat-war-deployment 
  -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

This will create a complete web application in the directory tomcat-war-deployment, which will print hello world! if we deploy it now and access it via the browser.

这将在tomcat-war-deployment,目录下创建一个完整的Web应用程序,它将打印hello world!如果我们现在部署它并通过浏览器访问它。

But before we do that, we need to make one change to enable Maven deployment. Let’s head over to the pom.xml and add this plugin:

但在这之前,我们需要做一个改动,以启用Maven部署。让我们前往 pom.xml,添加这个插件。

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <url>http://localhost:8080/manager/text</url>
        <server>TomcatServer</server>
        <path>/myapp</path>
    </configuration>
</plugin>

Note that we’re using the Tomcat 7 plugin because it works for both versions 7 and 8 without any special changes.

请注意,我们使用的是Tomcat 7插件,因为它对7和8版本都适用,不需要做任何特别的改动。

The configuration url is the url to which we’re sending our deployment, and Tomcat will know what to do with it. The server element is the name of the server instance that Maven recognizes. Finally, the path element defines the context path of our deployment.

配置url是我们要发送部署的URL,Tomcat会知道如何处理它。server元素是Maven识别的服务器实例的名称。最后,path元素定义了部署的context path

This means that if our deployment succeeds, we’ll access the web application by hitting http://localhost:8080/myapp.

这意味着,如果我们的部署成功,我们将通过点击http://localhost:8080/myapp访问网络应用。

Now we can run the following commands from Maven.

现在我们可以从Maven运行以下命令。

To deploy the web app:

要部署网络应用程序。

mvn tomcat7:deploy

Then to undeploy it:

然后,要取消部署。

mvn tomcat7:undeploy

Finally, to redeploy it after making changes:

最后,在进行修改后重新部署。

mvn tomcat7:redeploy

5. Deploy With Cargo Plugin

5.使用货物插件进行部署

Cargo is a versatile library that allows us to manipulate the various types of application containers in a standard way.

Cargo是一个多功能的库,它允许我们以标准方式操纵各种类型的应用程序容器。

5.1. Cargo Deployment Setup

5.1.货物部署设置

In this section, we’ll learn how to use Cargo’s Maven plugin to deploy a WAR to Tomcat. In this case, we’ll deploy it to a version 7 instance.

在本节中,我们将学习如何使用Cargo的Maven插件,将WAR部署到Tomcat。在本例中,我们将把它部署到一个版本7的实例上。

To get a firm understanding of the whole process, we’ll start from scratch by creating a new Java web application from the command line:

为了坚定地了解整个过程,我们将从头开始,从命令行中创建一个新的Java Web应用程序。

mvn archetype:generate -DgroupId=com.baeldung -DartifactId=cargo-deploy 
  -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

This will create a complete Java web application in the cargo-deploy directory. If we build, deploy, and load this application as is, it’ll print Hello World! in the browser.

这将在cargo-deploy目录下创建一个完整的Java Web应用程序。如果我们构建、部署并按原样加载这个应用程序,它将在浏览器中打印Hello World!

Unlike the Tomcat7 Maven plugin, the Cargo Maven plugin requires that this file is present.

与Tomcat7 Maven插件不同,Cargo Maven插件要求存在该文件。

Since our web application doesn’t contain any servlets, our web.xml file will be very basic. We’ll navigate to the WEB-INF folder of our newly created project, and create a web.xml file with the following content:

由于我们的Web应用程序不包含任何Servlet,我们的web.xml文件将是非常基本的。我们将导航到新创建项目的WEB-INF文件夹,并创建一个web.xml文件,内容如下。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
        id="WebApp_ID" version="3.0">

    <display-name>cargo-deploy</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

To enable Maven to recognize Cargo’s commands without typing the fully qualified name, we need to add the Cargo Maven plugin to a plugin group in Maven’s settings.xml.

为了让Maven在不输入全称的情况下识别Cargo的命令,我们需要将Cargo Maven插件添加到Maven的settings.xml中的一个插件组。

As an immediate child of the root <settings></settings> element, we’ll add:

作为根<settings></settings>元素的直接子元素,我们将添加。

<pluginGroups>
    <pluginGroup>org.codehaus.cargo</pluginGroup>
</pluginGroups>

5.2. Local Deploy

5.2.本地部署

In this subsection, we’ll edit our pom.xml to suit our new deployment requirements.

在本小节中,我们将编辑我们的pom.xml以适应我们新的部署要求。

We’ll add this plugin:

我们将添加这个插件。

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>1.5.0</version>
            <configuration>
                <container>
                    <containerId>tomcat7x</containerId>
                    <type>installed</type>
                    <home>Insert absolute path to tomcat 7 installation</home>
                </container>
                <configuration>
                    <type>existing</type>
                    <home>Insert absolute path to tomcat 7 installation</home>
                </configuration>
            </configuration>
       </plugin>
    </plugins>
</build>

The latest version, at the time of writing, is 1.5.0. However, the latest version can always be found here.

在撰写本文时,最新的版本是1.5.0。然而,最新的版本总是可以在这里找到。

Notice that we explicitly define the packaging as a WAR; without this, our build will fail. In the plugins section, we’ll then add the cargo maven2 plugin. Additionally, we’ll add a configuration section where we tell Maven that we’re using a Tomcat container and also an existing installation.

请注意,我们明确地将包装定义为WAR;如果不这样做,我们的构建就会失败。在插件部分,我们将添加cargo maven2插件。此外,我们将添加一个配置部分,告诉Maven我们使用的是Tomcat容器和现有安装。

By setting the container type to installed, we tell Maven that we have an instance installed on the machine, and we provide the absolute URL to this installation.

通过将容器类型设置为installed,我们告诉Maven我们在该机器上安装了一个实例,并提供了该安装的绝对URL。

By setting the configuration type to existing, we tell Tomcat that we have an existing setup that we’re using, and no further configuration is required.

通过将配置类型设置为existing,我们告诉Tomcat,我们有一个正在使用的现有设置,不需要进一步配置。

The alternative would be to tell cargo to download and setup the version specified by providing a URL. However, our focus is on WAR deployment.

另一种方法是告诉货物通过提供一个URL来下载和设置指定的版本。然而,我们的重点是WAR部署。

It’s worth noting that whether we’re using Maven 2.x or Maven 3.x, the cargo maven2 plugin works for both.

值得注意的是,无论我们使用的是Maven 2.x还是Maven 3.x,cargo maven2插件都适用于两者。

We can now install our application by executing:

现在我们可以通过执行以下命令来安装我们的应用程序。

mvn install

Then we’ll deploy it:

然后,我们将部署它。

mvn cargo:deploy

If all goes well, we should be able to run our web application by loading http://localhost:8080/cargo-deploy.

如果一切顺利,我们应该能够通过加载http://localhost:8080/cargo-deploy.来运行我们的网络应用。

5.3. Remote Deploy

5.3.远程部署

To do a remote deploy, we only need to change the configuration section of our pom.xml. Remote deploy means that we don’t have a local installation of Tomcat, but have access to the manager dashboard on a remote server.

要进行远程部署,我们只需要修改pom.xml的配置部分。远程部署意味着我们没有Tomcat的本地安装,但可以访问远程服务器上的管理器仪表板。

Let’s change the pom.xml so that the configuration section looks like this:

让我们改变pom.xml,使配置部分看起来像这样。

<configuration>
    <container>
        <containerId>tomcat8x</containerId>
        <type>remote</type>
    </container>
    <configuration>
        <type>runtime</type>
        <properties>
            <cargo.remote.username>admin</cargo.remote.username>
            <cargo.remote.password>admin</cargo.remote.password>
            <cargo.tomcat.manager.url>http://localhost:8080/manager/text
              </cargo.tomcat.manager.url>
        </properties>
    </configuration>
</configuration>

This time, we change the container type from installed to remote, and the configuration type from existing to runtime. Finally, we add authentication and remote URL properties to the configuration.

这一次,我们把容器类型从installed改为remote,,配置类型从existing改为runtime。最后,我们在配置中添加认证和远程URL属性。

We’ll ensure that the roles and users are already present in $CATALINA_HOME/conf/tomcat-users.xml, just as before.

我们将确保角色和用户已经存在于$CATALINA_HOME/conf/tomcat-users.xml中,和以前一样。

If we’re editing the same project for remote deployment, we’ll first un-deploy the existing WAR:

如果我们为远程部署编辑同一个项目,我们将首先取消部署现有的WAR。

mvn cargo:undeploy

Then we’ll clean the project:

然后,我们将清理项目。

mvn clean

Next, we’ll install it:

接下来,我们将安装它。

mvn install

Finally, we’ll deploy it:

最后,我们将部署它。

mvn cargo:deploy

That’s it.

就这样了。

6. Deploy From Eclipse

6.从Eclipse部署

Eclipse allows us to embed servers in order to add web project deployment in the normal workflow without navigating away from the IDE.

Eclipse允许我们嵌入服务器,以便在正常的工作流程中增加网络项目的部署,而不需要从IDE中导航出来。

6.1. Embed Tomcat in Eclipse

6.1.在Eclipse中嵌入Tomcat

To embed an installation into eclipse, we select the window menu item from the taskbar, and then preferences from the drop down.

为了将安装嵌入eclipse,我们从任务栏选择window菜单项,然后从下拉菜单选择preferences

Then we’ll see a tree grid of preference items on the left panel of the window that appears. Next, we navigate to eclipse -> servers, or just type servers in the search bar.

然后我们会在出现的窗口左侧面板上看到一个树状的偏好项目网格。接下来,我们导航到eclipse -> servers,或者直接在搜索栏中输入servers

We can then select the installation directory, if not already open for us, and choose the Tomcat version we downloaded.

然后我们可以选择安装目录,如果尚未为我们打开,并选择我们下载的Tomcat版本。

On the right hand side of the panel, a configuration page will appear. Here we select the Enable option to activate this server version and browse to the installation folder.

在面板的右侧,将出现一个配置页面。在这里我们选择启用选项来激活这个服务器版本,并浏览到安装文件夹。

Capture1

We’ll apply the changes, and then the next time we open the servers view from Eclipse’s windows -> show view sub-menu, the newly configured server will be present, and we can start, stop and deploy applications to it.

我们将应用这些更改,然后下次我们从Eclipse的windows -> show view子菜单中打开服务器视图时,新配置的服务器将出现,我们可以启动、停止并向其部署应用程序。

6.2. Deploy Web Application in Embedded Tomcat

6.2.在嵌入式Tomcat中部署Web应用程序

To deploy a web application to Tomcat, it must exist in our workspace.

要将一个Web应用程序部署到Tomcat,它必须存在于我们的工作空间中。

Let’s open the servers view from window -> show view and look for servers. When open, we can just right click on the server we configured and select add deployment from the context menu that appears.

让我们从window -> show view打开servers视图,寻找服务器。打开后,我们可以直接右击我们配置的服务器,并从出现的上下文菜单中选择添加部署

 

Capture-1-1-2

From the New Deployment dialog box that appears, we’ll open the project drop down, and select the web project.

在出现的新部署对话框中,我们将打开项目下拉菜单,并选择网络项目。

There’s a Deploy Type section beneath the Project combo box. When we select Exploded Archive(development mode), our changes in the application will be synced live without having to redeploy. This is the best option during development as it’s very efficient.

项目组合框的下面有一个部署类型部分。当我们选择Exploded Archive(development mode)时,我们在应用程序中的变化将被实时同步,而无需重新部署。这是开发期间的最佳选择,因为它非常高效。

Capture-2-1-2

 

Selecting Packaged Archive(production mode) will require us to redeploy every time we make changes and see them in the browser. This is best only for production, but still, Eclipse makes it equally easy.

选择Packaged Archive(production mode)将要求我们在每次进行修改并在浏览器中看到它们时都要重新部署。这只对生产来说是最好的,但仍然,Eclipse使它同样简单。

6.3. Deploy Web Application in External Location

6.3.在外部位置部署Web应用程序

We usually choose to deploy a WAR through Eclipse to make debugging easier. However, there may come a time when we want it deployed to a location other than those used by Eclipse’s embedded servers. The most common instance is when our production server is online, and we want to update the web application.

我们通常选择通过Eclipse来部署WAR,以使调试更容易。然而,可能会有这样的时候,我们想把它部署到Eclipse的嵌入式服务器所使用的位置以外的地方。最常见的情况是当我们的生产服务器在线时,我们想更新Web应用程序。

We can bypass this procedure by deploying in production mode, noting the Deploy Location in the New Deployment dialog box, and then picking the WAR from there.

我们可以绕过这一程序,在生产模式下进行部署,在新部署对话框中注意到部署位置,然后从那里挑选WAR。

During deployment, instead of selecting an embedded server, we can select the <Externally Launched> option from the servers view alongside the list of embedded servers. Then we navigate to the webapps directory of an external Tomcat installation.

在部署期间,我们可以从嵌入式服务器列表旁边的servers视图中选择<Externally Launched>选项,而不是选择一个嵌入式服务器。然后我们导航到外部Tomcat安装的webapps目录。

7. Deploy From IntelliJ IDEA

7.从IntelliJ IDEA部署

To deploy a web application to Tomcat, it must exist and have already been downloaded and installed.

要在Tomcat上部署一个Web应用程序,它必须存在并且已经被下载和安装。

7.1. Local Configuration

7.1.本地配置

Let’s open the Run menu, and click the Edit Configurations options.

让我们打开Run菜单,点击Edit Configurations选项。

 

Capture

In the panel on the left, we’ll search for Tomcat Server. If it’s not there, we’ll click the + sign in the menu, search for Tomcat, and select Local. In the name field, we’ll put Tomcat 7/8 (depending on our version).

在左边的面板中,我们将搜索Tomcat服务器。如果它不在那里,我们将点击菜单中的+号,搜索Tomcat,并选择Local。在名称字段中,我们将填写Tomcat 7/8(取决于我们的版本)。

Capture-1-2

 

Then we’ll click the Configure… button, and in the Tomcat Home field, we’ll navigate to the home location of our installation and select it.

然后我们将点击Configure…按钮,在Tomcat Home字段中,我们将导航到我们安装的主页位置并选择它。

 

Capture-2

Optionally, we can set the Startup page to be http://localhost:8080/ and HTTP port: 8080; we’ll change the port as appropriate.

另外,我们可以将启动页面设置为http://localhost:8080/ HTTP端口。8080;我们将根据情况改变端口。

Finally, we’ll go to the Deployment tab, click on the + symbol, select the artifact we want to add to the server, and click OK.

最后,我们将进入部署标签,点击+符号,选择我们想要添加到服务器的工件,然后点击确定。

 

Capture-3-1

 

7.2. Remote Configuration

7.2.远程配置

We’ll follow the same instructions as for local Tomcat configurations, but in the server tab, we must enter the remote location of the installation.

我们将遵循与本地Tomcat配置相同的说明,但在服务器标签中,我们必须输入安装的远程位置。

8. Deploy by Copying Archive

8.通过复制存档进行部署

We’ve learned how to export a WAR from Eclipse. Another thing we can do is to deploy it by simply dropping it into the $CATALINA_HOME\webapps directory of any Tomcat instance. If the instance is running, the deployment will start instantly as Tomcat unpacks the archive and configures its context path.

我们已经学会了如何从Eclipse导出一个WAR。我们可以做的另一件事是简单地将其部署到任何Tomcat实例的$CATALINA_HOME\webapps目录下。如果该实例正在运行,当Tomcat解压存档并配置其上下文路径时,部署将立即开始。

If the instance isn’t running, then the server will deploy the project the next time it’s started.

如果该实例没有运行,那么服务器将在下次启动时部署该项目。

9. Deploy From Tomcat Manager

9.从Tomcat管理器进行部署

Assuming we already have our WAR file to hand, and would like to deploy it using the management dashboard, we can access the manager dashboard by visiting: http://localhost:8080/manager.

假设我们已经有了我们的WAR文件,并希望使用管理仪表板进行部署,我们可以通过访问管理器仪表板。http://localhost:8080/manager

The dashboard has five different sections: Manager, Applications, Deploy, Diagnostics, and Server Information.

仪表板有五个不同的部分。Manager, Applications, Deploy, Diagnostics, 和Server Information.

If we go to the Deploy section, we’ll find two subsections.

如果我们进入Deploy部分,我们会发现两个分节。

9.1. Deploy Directory or WAR File Located on Server

9.1.位于服务器上的部署目录或WAR文件

If the WAR file is located on the server where the Tomcat instance is running, then we can fill the required Context Path field preceded by a forward slash “/”.

如果WAR文件位于运行Tomcat实例的服务器上,那么我们可以填写所需的Context Path字段,前面加一个正斜杠”/”。

For example, if we’d like our web application to be accessed from the browser with the URL http://localhost:8080/myapp, then our context path field will have /myapp.

例如,如果我们想让我们的网络应用从浏览器中以http://localhost:8080/myapp的方式被访问,那么我们的上下文路径字段将是/myapp.

We can also skip the XML Configuration file URL field and head over to the WAR or Directory URL field. Here, we’ll enter the absolute URL to the Web Archive file as it appears on our server. For example, if our file’s location is C:/apps/myapp.war, then we enter this location. It’s important we don’t forget the WAR extension.

我们也可以跳过XML配置文件URL字段,前往WAR目录URL字段。在这里,我们将输入Web存档文件的绝对URL,因为它出现在我们的服务器上。例如,如果我们文件的位置是C:/apps/myapp.war,那么我们就输入这个位置。重要的是,我们不要忘记WAR扩展名。

After that, we can click the deploy button. The page will reload, and we should see this message at the top of the page:

之后,我们可以点击deploy按钮。页面将重新加载,我们应该在页面顶部看到这个消息。

OK - Deployed application at context path /myapp

Our application should also appear in the Applications section of the page.

我们的应用程序也应该出现在页面的应用程序部分。

9.2. WAR File to Deploy

9.2.要部署的WAR文件

Here, we just click the choose file button, navigate to the location of the WAR file, select it, and then click the deploy button.

在这里,我们只是点击选择文件按钮,导航到WAR文件的位置,选择它,然后点击部署按钮。

In both situations, if all goes well, the Tomcat console will inform us that the deployment has been successful with the following message:

在这两种情况下,如果一切顺利,Tomcat控制台将用以下信息通知我们,部署已经成功。

INFO: Deployment of web application archive \path\to\deployed_war has finished in 4,833 ms

10. Conclusion

10.结论

In this article, we focused on how to deploy a WAR into a Tomcat server.

在这篇文章中,我们重点讨论了如何将WAR部署到Tomcat服务器中。