Jetty 是一個開源的servlet容器,它爲基於Java的web容器,例如JSP和servlet提供運行環境。Jetty是使用Java語言編寫的,它的API以一組JAR包的形式發佈。開發人員可以將Jetty容器實例化成一個對象,可以迅速爲一些獨立運行(stand-alone)的Java應用提供網絡和web連接。
特性
編輯易用性
可擴展性
易嵌入性
和Tomcat比較
編輯代碼實例
編輯
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//代碼:以嵌入模式啓動Jetty
import
org.mortbay.http.HttpContext;
import
org.mortbay.http.HttpServer;
import
org.mortbay.http.SocketListener;
import
org.mortbay.http.handler.ResourceHandler;
public
class
JettySample{
public
static
void
main(String[]args)
throws
Exception{
//創建JettyHttpServer對象
HttpServer server=
new
HttpServer();
//在端口8080上給HttpServer對象綁上一個listener,使之能夠接收HTTP請求
SocketListener listener=
new
SocketListener();
listener.setPort(
8080
);
server.addListener(listener);
//創建一個HttpContext,處理HTTP請求。
HttpContext context=
new
HttpContext();
//用setContextPath把Context映射到(/web)URL上。
context.setContextPath(
"/web"
);
//setResourceBase方法設置文檔目錄以提供資源
context.setResourceBase(
"C:\\j2sdk1.4.1_05"
);
//添加資源處理器到HttpContext,使之能夠提供文件系統中的文件
context.addHandler(
new
ResourceHandler());
server.addContext(context);
//啓動服務器
server.start();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?
xmlversion
=
"1.0"
encoding
=
"utf-8"
?>
<
plugin
>
<
groupId
>org.mortbay.jetty</
groupId
>
<
artifactId
>maven-jetty-plugin</
artifactId
>
<
version
>6.1.10</
version
>
<
configuration
>
<
scanIntervalSeconds
>10</
scanIntervalSeconds
>
<
stopKey
>foo</
stopKey
>
<
stopPort
>9999</
stopPort
>
</
configuration
>
<
executions
>
<
execution
>
<
id
>start-jetty</
id
>
<
phase
>pre-integration-test</
phase
>
<
goals
>
<
goal
>run</
goal
>
</
goals
>
<
configuration
>
<
scanIntervalSeconds
>0</
scanIntervalSeconds
>
<
daemon
>true</
daemon
>
</
configuration
>
</
execution
>
<
execution
>
<
id
>stop-jetty</
id
>
<
phase
>post-integration-test</
phase
>
<
goals
>
<
goal
>stop</
goal
>
</
goals
>
</
execution
>
</
executions
>
</
plugin
>
|
1
|
<pluginGroup>org.mortbay.jetty</pluginGroup>
|
一、Jetty簡介
1.1 什麼是Jetty
- 第一部分部分重點介紹如何使用Jetty,它提供如下信息,如什麼是Jetty,從哪可以下載它,怎麼在像Maven一樣的倉庫中找到它。這一部分同樣會提供啓動Jetty和如何配置Jetty的快速入門。
- 第二部分從更細緻的方面介紹Jetty的配置,介紹怎麼用Jetty來部署一個web應用程序,怎麼配置容器和連接,以及如何實現SSL和其它安全措施。
- Jetty的管理員應該關注第三部分。從啓動Jetty容器開始到session管理,日誌記錄,HTTP/2支持和Jetty優化,這一章節將幫助Jetty管理員獲得更多關於Jetty服務以外的知識,這一章節同樣包含容器最常用的特性配置如JNDI和JMX。
- 針對使用Jetty的高級用戶,第四部分着重於Jetty的開發,本章節的重點是如何將Jetty嵌入一個已經存在的應用程序中。這部分包含幾個簡單的例子和操作Jetty框架的指南。這一部分同樣包含如何使用Jetty的maven插件以及Jetty調試。
- 最後一個部分是引用部分,也包含Jetty的架構信息,Jetty的XML語法介紹,以及常見問題的解析,這章也介紹如何參與Jetty社區,如何貢獻代碼,以及如何尋求幫助。
1.2 如何選擇Jetty的版本
版本 | Year | Home | JVM | 協議 | Servlet | JSP | 狀態 |
---|---|---|---|---|---|---|---|
9.3 |
2015 |
Eclipse |
1.8 |
HTTP/1.1 (RFC 7230), HTTP/2 (RFC 7540), WebSocket (RFC 6455, JSR 356), FastCGI |
3.1 |
2.3 |
穩定版本 |
9.2 |
2014 |
Eclipse |
1.7 |
HTTP/1.1 RFC2616, javax.websocket, SPDY v3 |
3.1 |
2.3 |
穩定版本 |
8 |
2009- |
Eclipse/Codehaus |
1.6 |
HTTP/1.1 RFC2616, WebSocket RFC 6455, SPDY v3 |
3.0 |
2.2 |
珍貴版本 |
7 |
2008- |
Eclipse/Codehaus |
1.5 |
HTTP/1.1 RFC2616, WebSocket RFC 6455, SPDY v3 |
2.5 |
2.1 |
珍貴版本 |
6 |
2006-2010 |
Codehaus |
1.4-1.5 |
HTTP/1.1 RFC2616 |
2.5 |
2.0 |
已經作廢 |
5 |
2003-2009 |
Sourceforge |
1.2-1.5 |
HTTP/1.1 RFC2616 |
2.4 |
2.0 |
已經作廢 |
4 |
2001-2006 |
Sourceforge |
1.2, J2ME |
HTTP/1.1 RFC2616 |
2.3 |
1.2 |
遠古時期 |
3 |
1999-2002 |
Sourceforge |
1.2 |
HTTP/1.1 RFC2068 |
2.2 |
1.1 |
石器時代 |
2 |
1998-2000 |
Mortbay |
1.1 |
HTTP/1.0 RFC1945 |
2.1 |
1.0 |
傳說級別 |
1 |
1995-1998 |
Mortbay |
1.0 |
HTTP/1.0 RFC1945 |
- |
- |
神話級別 |
1.3 Jetty 和Java EE Web規範
1.3.1 Java EE 7 Web規範
JSR | 名稱 | jetty-9.1.x是否包含 | 支持插件 |
---|---|---|---|
JSR 340 |
Servlet Specification API 3.1 |
Yes |
|
JSR 344 |
Java Server Faces 2.2 (JSF) |
No |
Yes, Mojarra or MyFaces |
JSR 245 / JSR 341 |
Java Server Pages 2.3/Java Expression Language 3.0 (JSP/EL) |
Yes |
Yes |
JSR 52 |
Java Standard Tag Library 1.2 (JSTL) |
Yes |
Yes |
JSR 45 |
Debugging Support for Other Languages 1.0 |
Yes (via JSP) |
Yes (via JSP) |
JSR 346 |
Contexts and Dependency Injection for the JavaEE Platform 1.1 (Web Beans) |
No |
Yes, Weld |
JSR 330 |
Dependency Injection for Java 1.0 |
No |
Yes as part of a CDI implementation, Weld |
JSR 316 |
Managed Beans 1.0 |
No |
Yes, as part of another technology |
JSR 345 |
Enterprise JavaBeans 3.2 Lite |
No |
|
JSR 338 |
Java Persistance 2.1 (JPA) |
No |
Yes, eg Hibernate |
JSR 250 |
Common Annotations for the Java Platform 1.2 |
Yes |
Partially (for non-core Servlet Spec annotations) |
JSR 907 |
Java Transaction API 1.2 (JTA) |
Yes |
Yes |
JSR 349 |
Bean Validation 1.1 |
No |
Yes as part of another technology eg JSF, or a stand-alone implementation such as Hiberate Validator |
JSR 339 |
Java API for RESTful Web Services 2.0 (JAX-RS) |
No |
|
JSR 356 |
Java API for Websocket 1.0 |
Yes |
No |
JSR 353 |
Java API for JSON Processing 1.0 (JSON-P) |
No |
Yes, eg JSON-P reference implementation |
JSR 318 |
Interceptors 1.2 |
No |
Yes as part of a CDI implementation |
1.3.2 Jetty EE 6 Web Profile
表格 1.3. Java EE 6 Web Profile
JSR | Name | Included with jetty-9.0.x | Pluggable |
---|---|---|---|
JSR 315 |
Servlet Specification API 3.0 |
Yes |
|
JSR 314 |
JavaServer Faces 2.0 (JSF) |
No |
Yes, for example, Mojarra or MyFaces |
JSR 245 |
JavaServer Pages 2.2/Java Expression Language 2.2 (JSP/EL) |
Yes |
Yes |
JSR 52 |
Java Standard Tag Library 1.2 (JSTL) |
Yes |
Yes |
JSR 45 |
Debugging Support for Other Languages 1.0 |
Yes (via JSP) |
Yes (via JSP) |
JSR 299 |
Contexts and Dependency Injection for the Java EE Platform 1.0 (Web Beans) |
No |
Yes, Weld or OpenWebBeans |
JSR 330 |
Dependency Injection for Java 1.0 |
No |
Yes as part of a CDI implementation, Weld |
JSR 316 |
Managed Beans 1.0 |
No |
Yes, as part of another technology. |
JSR 318 |
Enterprise JavaBeans 3.1 |
No |
Yes, OpenEJB |
JSR 317 |
Java Persistance 2.0 (JPA) |
No |
Yes, Hibernate |
JSR 250 |
Common Annotations for the Java Platform |
Yes |
Partially (for non-core Servlet Spec annotations) |
JSR 907 |
Java Transaction API (JTA) |
Yes |
Implementations are pluggable, such as Atomikos, JOTM, Jencks (Geronimo Transaction Manager) |
JSR 303 |
Bean Validation 1.0 |
No |
Yes as part of another technology (JSF), or a stand-alone implementation such as Hiberate Validator |
1.4 在Maven中獲取Jetty
1.4.1 Maven座標
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-project</artifactId> <version>${project.version}</version> </dependency>
1.4.2 在Maven中心的更新日誌
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-project</artifactId> <version>${project.version}</version> <classifier>version</classifier> <type>txt</type> </dependency>
二、Jetty的使用
2.1 下載Jetty
2.1.1 下載Jetty項目
2.1.2 Jetty工程簡介
表格 2.1. Contents
Location | Description |
---|---|
license-eplv10-aslv20.html |
Jetty的許可文件 |
README.txt |
有用的開始信息 |
VERSION.txt |
版本信息 |
bin/ |
存放在Unix系統下運行的shell腳本 |
demo-base/ |
一個可運行包含示例web應用的Jetty服務器基目錄 |
etc/ |
Jetty的配置文件 |
lib/ |
Jetty運行所必須的jar文件 |
logs/ |
日誌 |
modules/ |
各個模塊 |
notice.html |
許可信息等 |
resources/ |
包含新增到classpath配置文件夾,如log4j.properties |
start.ini |
存放啓動信息 |
start.jar |
運行Jetty的jar |
webapps/ |
一個用來存放運行在默認配置下的Jetty Web應用目錄 |
2.2 運行Jetty
cd $JETTY_HOME java -jar start.jar
若執行成功會輸出以下信息
2015-06-04 10:50:44.806:INFO::main: Logging initialized @334ms 2015-06-04 10:50:44.858:WARN:oejs.HomeBaseWarning:main: This instance of Jetty is not running from a separate {jetty.base} directory, this is not recommended. See documentation at http://www.eclipse.org/jetty/documentation/current/startup.html 2015-06-04 10:50:44.995:INFO:oejs.Server:main: jetty-9.3.0.v20150601 2015-06-04 10:50:45.012:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///opt/jetty-distribution-9.3.0.v20150601/webapps/] at interval 1 2015-06-04 10:50:45.030:INFO:oejs.ServerConnector:main: Started [email protected]{HTTP/1.1,[http/1.1]}{0.0.0.0:8080} 2015-06-04 10:50:45.030:INFO:oejs.Server:main: Started @558ms
你可以通過瀏覽器訪問http://localhost:8080.。然而在$JETTY_HOME/webapps目錄下並沒有部署任何web應用,所以你將會看到一個Jetty提供的404錯誤頁面,並不推薦在$JETTY_HOME下運行Jetty,而是建議運行一個Jetty基礎應用。錯誤頁面如下
2.2.1 基礎應用例子
> cd $JETTY_HOME/demo-base/ > java -jar $JETTY_HOME/start.jar
成功運行將有如下信息輸出:
2015-06-04 10:55:24.161:INFO::main: Logging initialized @308ms 2015-06-04 10:55:24.431:WARN::main: demo test-realm is deployed. DO NOT USE IN PRODUCTION! 2015-06-04 10:55:24.434:INFO:oejs.Server:main: jetty-9.3.0.v20150601 2015-06-04 10:55:24.457:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///opt/jetty-distribution-9.3.0.v20150601/demo-base/webapps/] at interval 1 2015-06-04 10:55:24.826:INFO:oejsh.ContextHandler:main: Started [email protected]{/,file:///opt/jetty-distribution-9.3.0.v20150601/demo-base/webapps/ROOT/,AVAILABLE}{/ROOT} 2015-06-04 10:55:24.929:WARN::main: test-jaas webapp is deployed. DO NOT USE IN PRODUCTION! 2015-06-04 10:55:24.978:INFO:oejsh.ContextHandler:main: Started [email protected]{/test-jaas,file:///tmp/jetty-0.0.0.0-8080-test-jaas.war-_test-jaas-any-9105214562680121772.dir/webapp/,AVAILABLE}{/test-jaas.war} 2015-06-04 10:55:25.162:WARN::main: async-rest webapp is deployed. DO NOT USE IN PRODUCTION! 2015-06-04 10:55:25.208:INFO:oejsh.ContextHandler:main: Started [email protected]{/async-rest,[file:///tmp/jetty-0.0.0.0-8080-async-rest.war-_async-rest-any-1023939491558622183.dir/webapp/, jar:file:///tmp/jetty-0.0.0.0-8080-async-rest.war-_async-rest-any-1023939491558622183.dir/webapp/WEB-INF/lib/example-async-rest-jar-9.3.0.v20150601.jar!/META-INF/resources],AVAILABLE}{/async-rest.war} 2015-06-04 10:55:25.311:WARN::main: test-jndi webapp is deployed. DO NOT USE IN PRODUCTION! 2015-06-04 10:55:25.386:INFO:oejsh.ContextHandler:main: Started [email protected]{/test-jndi,file:///tmp/jetty-0.0.0.0-8080-test-jndi.war-_test-jndi-any-1692053319754270133.dir/webapp/,AVAILABLE}{/test-jndi.war} 2015-06-04 10:55:25.508:WARN::main: test-spec webapp is deployed. DO NOT USE IN PRODUCTION! 2015-06-04 10:55:25.594:INFO:oejsh.ContextHandler:main: Started [email protected]{/test-spec,[file:///tmp/jetty-0.0.0.0-8080-test-spec.war-_test-spec-any-5518740932795802823.dir/webapp/, jar:file:///tmp/jetty-0.0.0.0-8080-test-spec.war-_test-spec-any-5518740932795802823.dir/webapp/WEB-INF/lib/test-web-fragment-9.3.0.v20150601.jar!/META-INF/resources],AVAILABLE}{/test-spec.war} 2015-06-04 10:55:25.781:INFO:oejsh.ContextHandler:main: Started [email protected]{/proxy,file:///tmp/jetty-0.0.0.0-8080-xref-proxy.war-_xref-proxy-any-3068657547009829038.dir/webapp/,AVAILABLE}{/xref-proxy.war} 2015-06-04 10:55:25.786:INFO:oejsh.ContextHandler:main: Started [email protected]{/oldContextPath,null,AVAILABLE} 2015-06-04 10:55:25.951:WARN::main: test webapp is deployed. DO NOT USE IN PRODUCTION! 2015-06-04 10:55:26.248:INFO:oejsh.ContextHandler:main: Started [email protected]{/test,file:///tmp/jetty-0.0.0.0-8080-test.war-_test-any-5238659347611323540.dir/webapp/,AVAILABLE}{/test.war} 2015-06-04 10:55:26.255:INFO:oejs.ServerConnector:main: Started [email protected]{HTTP/1.1,[http/1.1]}{0.0.0.0:8080} 2015-06-04 10:55:26.259:INFO:oejus.SslContextFactory:main: x509={jetty.eclipse.org=jetty} wild={} alias=null for [email protected](file:///opt/jetty-distribution-9.3.0.v20150601/demo-base/etc/keystore,file:///opt/jetty-distribution-9.3.0.v20150601/demo-base/etc/keystore) 2015-06-04 10:55:26.269:INFO:oejs.ServerConnector:main: Started [email protected]{SSL,[ssl, http/1.1]}{0.0.0.0:8443} 2015-06-04 10:55:26.270:INFO:oejs.Server:main: Started @2417ms
現在可以通過瀏覽器訪問 http://localhost:8080, 此時可以看到一個Jetty的歡迎頁面,頁面上包含幾個簡單的例子,歡迎頁面如下
警告
示例的web應用程序不一定是絕對安全的,所以不應該部署在生產環境上。
你可以通過以下命令查看示例應用的配置信息
> cd $JETTY_HOME/demo-base/ > java -jar $JETTY_HOME/start.jar --list-modules ... > java -jar %JETTY_HOME/start.jar --list-config ...
--list-modules:此命令將返回當前服務所有可用的模塊,同時也會顯示本地的模塊,信息包括模塊實現的順序,依賴模塊以及相應的jar信息
2.2.2 創建一個新的Jetty基目錄
> JETTY_BASE=/tmp/mybase > mkdir $JETTY_BASE > cd $JETTY_BASE > java -jar $JETTY_HOME/start.jar WARNING: Nothing to start, exiting ... Usage: java -jar start.jar [options] [properties] [configs] java -jar start.jar --help # for more information > java -jar $JETTY_HOME/start.jar --add-to-startd=http,deploy INFO: server initialised (transitively) in ${jetty.base}/start.d/server.ini INFO: http initialised in ${jetty.base}/start.d/http.ini INFO: security initialised (transitively) in ${jetty.base}/start.d/security.ini INFO: servlet initialised (transitively) in ${jetty.base}/start.d/servlet.ini INFO: webapp initialised (transitively) in ${jetty.base}/start.d/webapp.ini INFO: deploy initialised in ${jetty.base}/start.d/deploy.ini MKDIR: ${jetty.base}/webapps INFO: Base directory was modified > cp $JETTY_HOME/demo-base/webapps/async-rest.war webapps/ROOT.war > java -jar $JETTY_HOME/start.jar 2015-06-04 11:10:16.286:INFO::main: Logging initialized @274ms 2015-06-04 11:10:16.440:INFO:oejs.Server:main: jetty-9.3.0.v20150601 2015-06-04 11:10:16.460:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///tmp/mybase/webapps/] at interval 1 2015-06-04 11:10:16.581:WARN::main: async-rest webapp is deployed. DO NOT USE IN PRODUCTION! 2015-06-04 11:10:16.589:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet 2015-06-04 11:10:16.628:INFO:oejsh.ContextHandler:main: Started [email protected]{/,[file:///tmp/jetty-0.0.0.0-8080-ROOT.war-_-any-4510228025526425427.dir/webapp/, jar:file:///tmp/jetty-0.0.0.0-8080-ROOT.war-_-any-4510228025526425427.dir/webapp/WEB-INF/lib/example-async-rest-jar-9.3.0.v20150601.jar!/META-INF/resources],AVAILABLE}{/ROOT.war} 2015-06-04 11:10:16.645:INFO:oejs.ServerConnector:main: Started [email protected]{HTTP/1.1,[http/1.1]}{0.0.0.0:8080} 2015-06-04 11:10:16.646:INFO:oejs.Server:main: Started @634ms
以上命令是Linux下的命令,簡單解釋:創建一個JETTY_BASE環境變量,並創建指定的文件夾,在此文件夾下運行start.jar時提示沒有可以啓動的東西,系統退出,此時輸入java -jar $JETTY_HOME/start.jar --add-to-startd=http,deploy命令爲當前基目錄添加標準文件及文件夾,然後java -jar $JETTY_HOME/start.jar運行即可運行成功。在Windows下操作如下:
2.2.3 改變Jetty的端口
> cd $JETTY_BASE > java -jar $JETTY_HOME/start.jar jetty.http.port=8081 ...
另外,可以將要設置的端口屬性添加到start.ini或者start.d/http.ini文件中。默認情況,在start.d/http.ini文件中定義的jetty.http.port屬性可以被修改成另一個值。
提示
配置屬性信息通過如下方式獲得
- 首先去start.d/http.ini文件中找jetty.http.port=8080配置信息,若找到配置端口即爲指定端口
- 根據模塊modules/http.mod文件找到指定配置默認爲etc/jetty-http.xml,在此配置文件中有jetty.http.port配置信息,若找到即爲此端口配置
- 若上面兩個都沒有,則隨機一個沒有使用的端口
2.2.4 爲HTTPS & HTTP2增加SSL
> java -jar $JETTY_HOME/start.jar --add-to-startd=https,http2 [...] > java -jar $JETTY_HOME/start.jar [...] 2015-06-04 13:52:01.933:INFO:oejs.ServerConnector:main: Started [email protected]{SSL,[ssl, alpn, h2, http/1.1]}{0.0.0.0:8443} [...]
--add-to-startd:此命令在ini文件中設置有效的配置信息,使HTTPS和HTTP2支持SSL連接
2.2.5 修改Jetty的HTTPS端口
> cd $JETTY_BASE > java -jar $JETTY_HOME/start.jar jetty.ssl.port=8444
除了在命令行增加參數的方法外,還可以在start.ini和start.d/*.ini文件中進行配置,所以如果用戶使用--add-to-startd命令來使HTTPS生效,同樣也可以在start.d/https.ini文件中進行修改。
2.2.6 更多的start.jar操作選項
> java -jar $JETTY_HOME/start.jar --help
2.3 部署Web應用
- 例如一個包含 WEB-INF/ 子文件夾的example/文件夾做爲一個標準的web應用進行部署,否則就按靜態資源部署,context路徑爲/example(訪問路徑: http://localhost:8080/example/);若文件夾名字爲ROOT,則訪問路徑爲/;若文件夾名字末尾爲.d則此文件夾被忽略(除非被特殊的配置文件引用)
- 若一個文件example.war被部署在webapps下,則context路徑爲example/,如果名字爲ROOT則訪問路徑爲/,如果example.war文件和example/文件夾同時存在,只有war文件會被部署(這個文件會被當做一個沒有被解壓的目錄來使用)
- 一個像example.xml的配置文件,若文件中是標準的配置信息,也會被部署,文件中必須包含context path,如果context path和example.xml同時存在,只有example.xml會被部署。
2.3.1 Jetty示例Web Application
三、Jetty配置信息介紹
3.1如何配置jetty
3.1.1 Jetty POJO 配置
- 編寫Java代碼來直接實例化並配置Jetty對象,這稱爲嵌入式Jetty。
- 使用Jetty的xml配置文件,這是一個控制反轉框架模式,從xml文件中實例化Jetty對象。 etc/jetty.xml配置文件是Jetty的主要配置文件,但是也有很多etc/jetty-__feature__.xml類型的文件被Jetty使用
- 使用第三方IOC框架,如Spring來實例化Jetty對象做爲一個Spring Beans
3.1.2 Jetty啓動配置文件
- **的模塊 --module=name
- 通過name=value的來爲Jetty的IOC配置屬性
- XML files in Jetty IoC (or Spring) XML format
- 一個標準的Java屬性文件包含額外的啓動屬性
- 其他start.jar選項(see java -jar start.jar --help)
- 一些JVM參數組合通過--exec,例如-Xbootclasspath.
提示
在Jetty9,只有Jetty基路徑下的配置文件被編輯,屬性纔會被修改
- 模塊依賴如何排序以及**
- 被模塊需要並且要就在到classpath的jar包
- 被模塊需要並且內容會被執行的xml文件
- 被**模塊需要的文件
- 一個示例的ini文件,當被-add-to-start=name選項**時使用
Note
常見典型的模塊配置信息通長很少被改動,*.mod文件通常位於$JETTY_HOME/modules/文件夾下,但是額外增加的或者編輯過的通常放在 $JETTY_BASE/module下,如果一個模塊需要被修改,最好的做法是在修改前從$JETTY_HOME/modules/拷貝*.mod到 $JETTY_BASE/modules/
XML files:
xml文件一般用與IoC以及spring使用,用在命令行或者模塊定義中。xml文件裏面的配置將被注入到描述服務的一個對象裏。通常改變屬性的方法是改變對應的ini文件。xml文件通常放在$JETTY_HOME/etc/下,但是新增的或者被編輯的xml應該被放在 $JETTY_BASE/etc/下,如果xml配置文件有修改的必要,最好的做法是在修改前將xml文件從$JETTY_HOME/etc拷貝到$JETTY_BASE/etc/ 。3.1.3 其它配置文件
- 設置一個web項目的默認的根路徑
- 通過WEB-INF/web.xml進行特殊的配置
- Interpret descriptor fragments included in the META-INF directory of Jar files within WEB-INF/lib.
Note
通常web應用的web.xml文件應該在WEB-INF/web.xml位置被發現,Jetty還支持多個web.xml,可以在 WEB-INF/web.xml文件加載前或者加載後加載(使用方法見test.xml)
- 可以爲Jetty IoC增加參數
- 配置默認日誌信息(StdErrLog),其他日誌框架也可以通過配置被使用(例如,log4j)
- 數據庫的登錄配置信息等
3.1.4 Jetty IoC XML使用
// // ======================================================================== // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. // ------------------------------------------------------------------------ // All rights reserved. This program and the accompanying materials // are made available under the terms of the Eclipse Public License v1.0 // and Apache License v2.0 which accompanies this distribution. // // The Eclipse Public License is available at // http://www.eclipse.org/legal/epl-v10.html // // The Apache License v2.0 is available at // http://www.opensource.org/licenses/apache2.0.php // // You may elect to redistribute this code under either of these licenses. // ======================================================================== // package org.eclipse.jetty.embedded; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.servlet.ServletContextHandler; public class ExampleServer { public static void main( String[] args ) throws Exception { Server server = new Server(); ServerConnector connector = new ServerConnector(server); connector.setPort(8080); server.setConnectors(new Connector[] { connector }); ServletContextHandler context = new ServletContextHandler(); context.setContextPath("/"); context.addServlet(HelloServlet.class, "/hello"); context.addServlet(AsyncEchoServlet.class, "/echo/*"); HandlerCollection handlers = new HandlerCollection(); handlers.setHandlers(new Handler[] { context, new DefaultHandler() }); server.setHandler(handlers); server.start(); server.join(); } }
- 注意Jetty的最低使用的jdk版本,最新版本使用jdk1.8
- HelloServlet是一個繼承了HttpServlet的servlet
- 運行成功後程序會被掛起,等待用戶連接,在瀏覽器中輸入http://localhost:8080/hello即可訪問
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd"> <Configure id="ExampleServer" class="org.eclipse.jetty.server.Server"> <Set name="connectors"> <Array type="org.eclipse.jetty.server.Connector"> <Item> <New class="org.eclipse.jetty.server.ServerConnector"> <Arg><Ref refid="ExampleServer"/></Arg> <Set name="port">8080</Set> </New> </Item> </Array> </Set> <New id="context" class="org.eclipse.jetty.servlet.ServletContextHandler"> <Set name="contextPath">/hello</Set> <Call name="addServlet"> <Arg>org.eclipse.jetty.embedded.HelloServlet</Arg> <Arg>/</Arg> </Call> </New> <Set name="handler"> <New class="org.eclipse.jetty.server.handler.HandlerCollection"> <Set name="handlers"> <Array type="org.eclipse.jetty.server.Handler"> <Item> <Ref refid="context" /> </Item> <Item> <New class="org.eclipse.jetty.server.handler.DefaultHandler" /> </Item> </Array> </Set> </New> </Set> </Configure>
3.2 可以在Jetty中配置什麼
3.2.1 配置服務
3.2.2配置連接
Note
虛擬主機不能配置連接,用戶應該通過虛擬主機的context來實現它們的相應。
Note
Jetty9同時支持連接協議(HTTP,HTTPS,AJP,SPDY)和自然連接(NIO或者BIO),Jetty9以後只有一個主要的連接器ServerConnector
3.2.3配置上下文環境
Note
servlet規範定一個web應用。在Jetty規範中一個標準的web項目必須有一個標準的WEB-INF/web.xml文件,用來配置classpath,資源,session處理,登錄,以及servlet,jsp,以及靜態資源。標準的web應用需要很少的額外的配置文件。
3.2.3.1 通過API配置上下文
// // ======================================================================== // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. // ------------------------------------------------------------------------ // All rights reserved. This program and the accompanying materials // are made available under the terms of the Eclipse Public License v1.0 // and Apache License v2.0 which accompanies this distribution. // // The Eclipse Public License is available at // http://www.eclipse.org/legal/epl-v10.html // // The Apache License v2.0 is available at // http://www.opensource.org/licenses/apache2.0.php // // You may elect to redistribute this code under either of these licenses. // ======================================================================== // package org.eclipse.jetty.embedded; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ContextHandler; public class OneContext { public static void main( String[] args ) throws Exception { Server server = new Server( 8080 ); // Add a single handler on context "/hello" ContextHandler context = new ContextHandler(); context.setContextPath( "/hello" ); context.setHandler( new HelloHandler() ); // Can be accessed using http://localhost:8080/hello server.setHandler( context ); // Start the server server.start(); server.join(); } }
3.2.3.2 通過Ioc XML配置上下文
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <!-- Configure a custom context for serving javadoc as static resources --> <Configure class="org.eclipse.jetty.server.handler.ContextHandler"> <Set name="contextPath">/javadoc</Set> <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>/javadoc/</Set> <Set name="handler"> <New class="org.eclipse.jetty.server.handler.ResourceHandler"> <Set name="welcomeFiles"> <Array type="String"> <Item>index.html</Item> </Array> </Set> <Set name="cacheControl">max-age=3600,public</Set> </New> </Set> </Configure>
3.2.4 配置web應用
- 初始化classpath,在WEB-INF/lib和WEB-INF/classes下。
- 根據標準的WEB-INF/web.xml進行解析初始化參數,過濾器,監聽器,歡迎頁面等信息。
- WEB-INF/jetty-web.xml文件可能包含Jetty IoC的配置。
3.2.4.1 配置contextpath
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <Set name="contextPath">/contextpath</Set> </Configure>
還有一種做法是,不用增加配置文件,在war文件同目錄,建一個同名的xml文件,內容如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test.war</Set> <Set name="contextPath">/test</Set> </Configure>
配置例子可以參照$JETTY_HOME/webapps/test.xml
3.2.4.2 Web應用部署
- 一個標準的war文件(必須以war結尾)
- 一個標準的web應用(必須有{dir}/WEB-INF/web.xml 文件)
- 一個包含靜態資源的文件夾
- 一個Jetty描述XML,引用一個web應用
- 隱藏文件將被忽略(以.開頭的文件)
- 以.d開頭的文件夾將被忽略
- 如果一個文件夾和war名字一樣,則只有war會部署
- 如果一個文件夾和同名xml配置,則xml配置的會被部署
- 如果一個war文件和xml文件同名,則xml配置的會被部署
3.2.4.3 設置身份驗證區域
... <login-config> <auth-method>BASIC</auth-method> <realm-name>Test Realm</realm-name> </login-config> ...
這個例子使用名爲Test Realm.的驗證,但是沒有說明其是怎麼實現的,在Jetty中有幾種實現方式,其中一種是HashLoginService可以讀取登錄用戶,並在配置文件中查找,爲了配置HashLoginService用來匹配Test Realm,可以配置$JETTY_BASE/etc/test-realm.xml文件,並且該文件必須在start.ini或者start.d/server.ini中設置。
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-" "http://www.eclipse.org/jetty/configure_9_3.dtd"> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <!-- =========================================================== --> <!-- Configure Authentication Login Service --> <!-- Realms may be configured for the entire server here, or --> <!-- they can be configured for a specific web app in a context --> <!-- configuration (see $(jetty.home)/webapps/test.xml for an --> <!-- example). --> <!-- =========================================================== --> <Call name="addBean"> <Arg> <New class="org.eclipse.jetty.security.HashLoginService"> <Set name="name">Test Realm</Set> <Set name="config"><Property name="jetty.demo.realm" default="etc/realm.properties"/></Set> <Set name="refreshInterval">0</Set> </New> </Arg> </Call> <Get class="org.eclipse.jetty.util.log.Log" name="rootLogger"> <Call name="warn"><Arg>demo test-realm is deployed. DO NOT USE IN PRODUCTION!</Arg></Call> </Get> </Configure>
通過創建和配置LoginService到web應用中,當部署應用時,會通過名字去尋找匹配的登錄Service。
參考 https://www.cnblogs.com/yiwangzhibujian/p/5832597.html