Jetty Configuration in Eclipse – 在Eclipse中配置Jetty

最后修改: 2018年 8月 21日

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

1. Overview

1.概述

Web Applications are one of the most popular use cases of Java. Web Servers and Servlet Containers provide the runtime for deploying applications.

网络应用是Java最流行的使用案例之一。Web服务器和Servlet容器提供了部署应用程序的运行时间。

Unfortunately, deploying and troubleshooting web applications on Web Servers is at times complicated. Luckily, IDEs have good debugging support for most applications. However, to debug web applications, we need to embed a Web Server in the IDE.

不幸的是,在Web服务器上部署Web应用程序并排除故障有时是很复杂的。幸运的是,IDE对大多数应用程序都有良好的调试支持。然而,为了调试Web应用程序,我们需要在IDE中嵌入一个Web服务器。

In this tutorial, we’ll embed Jetty in Eclipse and run and debug an application on it.

在本教程中,我们将在Eclipse中嵌入Jetty,并在上面运行和调试一个应用程序

2. Eclipse Jetty Plugin

2.Eclipse Jetty Plugin

The simplest way of connecting Jetty to Eclipse is using the Eclipse Jetty Plugin.

连接Jetty和Eclipse的最简单方法是使用Eclipse Jetty插件。

The plugin adds a managed Jetty server into Eclipse. Hence, it allows us to deploy and test or debug applications seamlessly. Also, the plugin provides an interface for easily configuring the server.

该插件在Eclipse中添加了一个管理的Jetty服务器。因此,它允许我们无缝地部署和测试或调试应用程序。此外,该插件还提供了一个接口,可以轻松配置服务器。

The quickest way to install the plugin is through the Marketplace. In eclipse, the Marketplace enables us to install plugins with a few clicks:

安装插件的最快捷的方法是通过Marketplace。在eclipse中,Marketplace使我们可以通过几次点击来安装插件。

marketplace

3. Sample Application

3.申请书样本

Let’s now develop a simple web application.

现在让我们来开发一个简单的网络应用。

First, let’s add the web.xml in the /src/main/webapp/WEB-INF folder of our project:

首先,让我们在我们项目的/src/main/webapp/WEB-INF文件夹中添加web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/webapp_4_0.xsd"
	version="4.0">

	<welcome-file-list>
		<welcome-file>helloworld.html</welcome-file>
	</welcome-file-list>

</web-app>

Let’s add a simple static file. In our case, the helloworld.html looks like this:

让我们添加一个简单的静态文件。在我们的例子中,helloworld.html看起来是这样的。

<!DOCTYPE html>
<html>
   <head>
      <meta charset="ISO-8859-1">
      <title>Hello World</title>
   </head>
   <body>Hello World!</body>
</html>

Note that we haven’t added any servlet mapping to our web.xml.

注意,我们没有在我们的web.xml中添加任何servlet映射。

Instead, we are going to use Servlet 3 annotations for our servlet:

相反,我们要为我们的Servlet使用Servlet 3注解。

@WebServlet("/helloworld")
public class HelloWorldServlet extends HttpServlet

WebServlet annotation enables our servlet class to be scanned and deployed on the container.

WebServletannotation使我们的Servlet类能够被扫描并部署在容器上。

We should keep in mind that Jetty doesn’t support annotations with the basic HTTP module. Hence we’ll need to add the annotation support module for this to work.

我们应该记住,Jetty不支持基本HTTP模块的注释。因此,我们需要添加注解支持模块,这样才能发挥作用

We’ll see how to do this in the following sections.

我们将在以下章节中看到如何做到这一点。

4. Running the Application on Jetty

4.在Jetty上运行应用程序

Deploying web application on servers varies from vendor to vendor. The Eclipse Jetty plugin takes care of this process for us. Likewise, it integrates with our IDE debugger, improving the development experience.

在服务器上部署Web应用程序,各厂商的做法不尽相同。Eclipse Jetty插件为我们处理了这个过程。同样地,它与我们的IDE调试器集成,改善了开发体验。

Sometimes we need to run the application with some configuration. Eclipse allows us to do that using the Launch Configuration.

有时我们需要用一些配置来运行应用程序。Eclipse允许我们使用 “启动配置 “来做到这一点。

This is how it looks for running applications on Jetty:

这就是在Jetty上运行应用程序的情况。

Launch config

We can configure the following parameters for our application:

我们可以为我们的应用程序配置以下参数。

  • Context Path – prefix for our application URL
  • HTTP Port – port on which the application is deployed, the default is 8080
  • Enable HTTPS – for deploying on HTTPS along with HTTP
  • HTTPS Port – default is 8443

As with regular Jetty, Eclipse Jetty plugin allows us to manage dependencies for an application before deploying. For a maven application, we could select the dependency scope as classpath if we wish to provide those from the server.

与普通的Jetty一样,Eclipse Jetty插件允许我们在部署前管理应用程序的依赖性。对于一个maven应用程序,如果我们希望从服务器上提供这些,我们可以选择依赖范围作为classpath。

5. Jetty Server Options

5.Jetty服务器选项

Jetty is a highly configurable Servlet Container. There are various parameters we can specify like Thread Pool Size, Shutdown Interval etc.

Jetty是一个高度可配置的Servlet容器。我们可以指定各种参数,如线程池大小关机间隔等。

Besides that, Jetty allows us to add various Modules on top of the basic HTTP Module. These are some of the common modules we can add:

除此之外,Jetty允许我们在基本的HTTP模块之上添加各种模块。这些是我们可以添加的一些常见模块。

  • Annotations Support – enables support for Servlet specific annotations
  • JNDI Support – allows JNDI resource management by Jetty
  • Websocket Support – enables Websocket server and client implementations
  • JMX Support – allows monitoring of Jetty using any MBeans browser
  • JSP Support – enables JSP compiling and deployment in Jetty

These configurations are possible in Eclipse Jetty as well.  Hence, we can configure the server parameters and modules from the launch configuration.

这些配置在Eclipse Jetty中也是可行的。 因此,我们可以从启动配置中配置服务器参数和模块。

Lastly, Eclipse Jetty 4.0 plugin comes with an embedded Jetty 9.3 server. However, we can configure an external jetty server for our application from the launch configuration.

最后,Eclipse Jetty 4.0插件带有一个嵌入式Jetty 9.3服务器。然而,我们可以在启动配置中为我们的应用程序配置一个外部的jetty服务器。

6. Eclipse Jetty Console

6.Eclipse Jetty 控制台

Eclipse Jetty provides a console with some useful control commands.  This console comes in handy to manage the server or gather some metrics from the server.

Eclipse Jetty提供了一个带有一些有用控制命令的控制台。 这个控制台在管理服务器或从服务器上收集一些指标时很方便。

The console needs to be enabled in the launch configuration. When enabled, we can execute the control commands from Eclipse Console.

控制台需要在启动配置中启用。启用后,我们可以从 Eclipse 控制台执行控制命令。

Here is a list of some common commands we can use:

下面是我们可以使用的一些常用命令的清单。

  • Memory – memory information of the current application
  • Threads – thread dump of the running application
  • Restart – restarts the running application
  • Stop – gracefully stops the server and all the apps running on it

7. Conclusion

7.结语

Eclipse Jetty plugin is a great way of quickly running or debugging an application by embedding the Jetty server. It also allows us to configure our application and the underlying Jetty server.

Eclipse Jetty插件是一种通过嵌入Jetty服务器来快速运行或调试应用程序的好方法。它还允许我们配置我们的应用程序和底层的Jetty服务器。

In this tutorial, we installed the Eclipse Jetty plugin and deployed our application. We also created a launch configuration and provided application and server parameters.

在本教程中,我们安装了Eclipse Jetty插件并部署了我们的应用程序。我们还创建了一个启动配置并提供了应用程序和服务器参数。