# jetty介绍

## 1. jetty简介

Jetty是当下非常流行的一款轻量级Java Web服务器和Servlet容器实现。由于其轻量、灵活的特性，Jetty被广泛用于一系列知名产品，例如ActiveMQ、Maven、Spark、Google App Engine、Eclipse、Hadoop等等。

jetty官方文档：<http://www.eclipse.org/jetty/documentation/current/>

## 2. 下载安装

从官网下载jetty适合的版本，LZ目前下载的是9.4版本，解压。

```
cd demo-base/
java -jar ../start.jar
```

然后访问`localhost:8080`,就可以看到jetty的主页了。

## 3. 部署项目

将打包好的war包丢进demo-base的webapps目录下，jetty就会自动编译项目，然后就可以访问你的项目了。需要注意的是jetty不会像tomcat一样将war包解压到当前目录，而是解压到了用户的临时目录下。

## 4. jetty和tomcat比较

1. Jetty更轻量级。这是相对Tomcat而言的。&#x20;

   由于Tomcat除了遵循Java Servlet规范之外，自身还扩展了大量JEE特性以满足企业级应用的需求，所以Tomcat是较重量级的，而且配置较Jetty亦复杂许多。但对于大量普通互联网应用而言，并不需要用到Tomcat其他高级特性，所以在这种情况下，使用Tomcat是很浪费资源的。这种劣势放在分布式环境下，更是明显。换成Jetty，每个应用服务器省下那几兆内存，对于大的分布式环境则是节省大量资源。而且，Jetty的轻量级也使其在处理高并发细粒度请求的场景下显得更快速高效。
2. jetty更灵活，体现在其可插拔性和可扩展性，更易于开发者对Jetty本身进行二次开发，定制一个适合自身需求的Web Server。相比之下，重量级的Tomcat原本便支持过多特性，要对其瘦身的成本远大于丰富Jetty的成本。用自己的理解，即增肥容易减肥难。
3. 然而，当支持大规模企业级应用时，Jetty也许便需要扩展，在这场景下Tomcat便是更优的。

**总结：** Jetty更满足公有云的分布式环境的需求，而Tomcat更符合企业级环境。

> 参考：<https://www.cnblogs.com/fengli9998/p/7247559.html>
>
> <http://www.eclipse.org/jetty/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jun-wang.gitbook.io/learnjava/ji-shu-xue-xi/web-zhong-jian-jian-xue-xi/jetty-jie-shao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
