Tomcat Configuration In Eclipse – 在Eclipse中配置Tomcat

最后修改: 2018年 8月 18日

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

1. Overview

1.概述

One of the key challenges of web development is being able to debug effectively when running on web servers. Since building, packaging, and deploying consumes a lot of time, this might be difficult to achieve.

网络开发的关键挑战之一是在网络服务器上运行时能够有效地进行调试。由于构建、打包和部署需要消耗大量的时间,这可能很难实现。

Luckily, Eclipse allows us to start a server in the IDE itself, saving build and packaging time. Also, helping us to investigate issues by starting the server in debug mode.

幸运的是,Eclipse允许我们在IDE本身中启动服务器,节省了构建和打包时间。同时,通过在调试模式下启动服务器,帮助我们调查问题。

In this quick tutorial, we’ll see how to configure a Tomcat server in Eclipse to achieve this.

在这个快速教程中,我们将看到如何在Eclipse中配置Tomcat服务器以实现这一目标。

2. Defining a Server in Eclipse

2.在Eclipse中定义一个服务器

Before configuring Tomcat in Eclipse, we’ll have to install it first.

在Eclipse中配置Tomcat之前,我们要先安装它。

Now, let’s start by invoking the New Server wizard in Eclipse using File > New > Other:

现在,让我们开始在Eclipse中使用File > New > Other:来调用New Server向导。

Define Server Image 1

Clicking on Next will take us to the window where we can select the version of Tomcat. Here, we have selected version 9.0:

点击下一步将把我们带到窗口,我们可以选择Tomcat的版本。这里,我们选择了版本9.0:

Define Server Image 2

The wizard will default the name of the server to localhost and the server name to Tomcat v9.0 Server at localhost. 

该向导将默认服务器的名称为localhost,服务器名称为Tomcat v9.0 Server at localhost。

We’ll see that the very first time we add a Tomcat server in Eclipse, the wizard will ask us to configure the server runtime environment:

我们将看到,第一次在Eclipse中添加Tomcat服务器时,向导会要求我们配置服务器的运行环境。

Define Server Image 3

Here, we’ll specify the location of the Tomcat installation directory. Also, we will specify the JRE for the Tomcat server.

在这里,我们将指定Tomcat安装目录的位置。此外,我们还将指定Tomcat服务器的JRE。

If we click Next, Eclipse will allow us to add the web applications to be deployed on the server. But, let’s cover that in a later section and click on Finish instead.

如果我们单击Next,Eclipse将允许我们添加要在服务器上部署的Web应用程序。但是,让我们在后面的章节中讨论这个问题,并点击Finish代替。

Now we can see the new server in the Project Explorer and the Server views.

现在我们可以在项目浏览器服务器视图中看到新的服务器。

3. Configuring the Server

3.配置服务器

In the Project Explorer, we’ll see the usual tomcat server configuration files, e.g. server.xml, tomcat-users.xml etc.

项目浏览器中,我们将看到通常的tomcat服务器配置文件,例如server.xml、tomcat-users.xml等

Also, if we double-click on Tomcat v9.0 Server at localhost, we can configure the server using the provided UI:

此外,如果我们双击Tomcat v9.0 Server at localhost,我们可以使用提供的用户界面配置服务器。

config

On this screen, we can configure:

在这个屏幕上,我们可以进行配置。

  • server name – this is the name that will appear in the server view
  • configuration path – this is where the files we see in the Project Explorer reside
  • server location – this is where we configure the location of the server installation. Also, we can set the application deploy location here
  • module publishing – this is where we configure how the web modules are published
  • timeouts – these are timeouts for starting/stopping the server
  • ports – here we can set the various server ports
  • MIME mappings – these are the various MIME type mappings
  • server launch configuration – here we can configure the VM arguments, classpath etc.
  • server options – here we can enable/disable features like security, auto reload of modules by default etc.

4. Adding Applications to the Server

4.将应用程序添加到服务器上

We can now deploy our web applications on this server. Consequently, we have to make sure that the Dynamic Web Module facet is enabled for the project before we can add them.

我们现在可以在这个服务器上部署我们的网络应用。因此,我们必须确保项目的动态网络模块面已经启用,然后才可以添加它们。

So let’s right-click on the tomcat server in the Servers view and choose the Add and Remove… menu item. Then, on the screen that follows, we’ll select the spring-rest web module:

因此,让我们在服务器视图中的tomcat服务器上点击右键,选择添加和删除…菜单项。然后,在接下来的屏幕上,我们将选择spring-rest网络模块。

6

Finally, if we now click on Finish, we’ll see spring-rest in the Servers view.

最后,如果我们现在点击Finish,我们将在Servers视图中看到spring-rest

5. Running the Server

5.运行服务器

Now all that remains to do is start the tomcat server. Then we’ll see the server logs in the Console view, while the server is starting.

现在所要做的就是启动Tomcat服务器。然后我们将在Console 视图中看到服务器的日志,同时服务器正在启动。

Keep in mind, if the server timeout is very low, the server might fail to start. Consequently, we can resolve this by increasing the server start timeout on the configuration screen we saw above.

请记住,如果服务器超时很低,服务器可能无法启动。因此,我们可以通过在上面看到的配置屏幕上增加服务器启动超时来解决这个问题。

It is important to note that eclipse will not publish the application to the server’s webapps folder. It will deploy this web application to a temporary folder. Therefore, leaving the Tomcat installation unmodified. If we do not change the configuration, Eclipse will publish the applications to the workspace folder:

需要注意的是,eclipse不会将应用程序发布到服务器的webapps文件夹中。它将把这个Web应用程序部署到一个临时文件夹中。因此,让Tomcat的安装不被修改。如果我们不改变配置,Eclipse将把应用程序发布到工作区文件夹中。

<workspace>/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps

Now, Eclipse will keep monitoring our source code and look for code changes. Then, we can sync these changes with the server so that the latest code is deployed on the server.

现在,Eclipse将持续监控我们的源代码,寻找代码的变化。然后,我们可以将这些变化与服务器同步,以便在服务器上部署最新的代码。

6. Conclusion

6.结语

In this tutorial, we saw how we are able to deploy our web applications in the Eclipse IDE itself.

在本教程中,我们看到了我们如何能够在Eclipse IDE本身中部署我们的Web应用程序。

This helps us avoid having to explicitly build, package and deploy the applications, thus save us precious development time that can be used more effectively.

这有助于我们避免明确地构建、打包和部署应用程序,从而为我们节省宝贵的开发时间,可以更有效地利用。