安裝和執行 Jetty

為了這個例子,假設使用者正在執行 Jetty 作為分發。有關如何將 Jetty 作為嵌入式 Web 伺服器執行的資訊,請參閱官方文件

Jetty 可以從這裡下載,並提供 .zip 和 .gzip 格式。當前版本的 Jetty 需要安裝 Java 1.8,可以從 Oracle 的網站獲得。

將分發提取到你選擇的目錄。在以下示例中,此目錄將稱為$ JETTY_HOME。要啟動 Jetty,只需從命令列啟動分發中的 start.jar 檔案:

$ cd $JETTY_HOME
$ java -jar start.jar

從$ JETTY_HOME 目錄執行此操作將產生類似於此的輸出:

2016-07-25 09:24:46.019:INFO::main: Logging initialized @313ms
2016-07-25 09:24:46.068: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
2016-07-25 09:24:46.191:INFO:oejs.Server:main: jetty-9.3.11.v20160721
2016-07-25 09:24:46.207:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///Users/user/installs/repository/jetty-distribution-9.3.11.v20160721/webapps/] at interval 1
2016-07-25 09:24:46.226:INFO:oejs.AbstractConnector:main: Started ServerConnector@735f7ae5{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2016-07-25 09:24:46.227:INFO:oejs.Server:main: Started @521ms

伺服器正在執行,但你會注意到,如果你將瀏覽器指向 localhost:8080,則會收到 404 錯誤。

在上面的輸出中,有一個關於從$ JETTY_HOME 目錄執行伺服器的警告。Jetty 9.1 引入了 Jetty Home 目錄和 Jetty Base 目錄(或目錄)的概念。基本前提是 Jetty Home 目錄是所有伺服器檔案的標準。它包含執行伺服器所需的所有檔案以及所有伺服器擴充套件的預設值。Jetty Base 目錄(或多個目錄)用於管理 Jetty 例項,幷包含伺服器的所有 Web 應用程式,第三方外掛和自定義項。有關更多資訊,請參閱有關管理 Jetty Base 和 Jetty Home 的官方文件。

Jetty 發行版附帶一個基本目錄,其中已配置了多個 Web 應用程式。這個目錄恰當地命名為 demo-base

$ cd demo-base
$ java -jar $JETTY_HOME/start.jar
2016-07-25 09:25:48.435:INFO::main: Logging initialized @316ms
2016-07-25 09:25:48.645:WARN::main: demo test-realm is deployed. DO NOT USE IN PRODUCTION!
2016-07-25 09:25:48.647:INFO:oejs.Server:main: jetty-9.3.11.v20160721
2016-07-25 09:25:48.662:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///Users/user/installs/repository/jetty-distribution-9.3.11.v20160721/demo-base/webapps/] at interval 1
2016-07-25 09:25:48.923:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=81ms
2016-07-25 09:25:49.078:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@527740a2{/proxy,file:///private/var/folders/h6/yb_lbnnn11g0y1jjlvqg631h0000gn/T/jetty-0.0.0.0-8080-xref-proxy.war-_xref-proxy-any-1184996072304577986.dir/webapp/,AVAILABLE}{/xref-proxy.war}
2016-07-25 09:25:49.083:INFO:oejsh.ContextHandler:main: Started o.e.j.s.h.MovedContextHandler@229d10bd{/oldContextPath,null,AVAILABLE}
2016-07-25 09:25:49.166:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=34ms
2016-07-25 09:25:49.168:WARN::main: test-spec webapp is deployed. DO NOT USE IN PRODUCTION!
2016-07-25 09:25:49.238:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@647fd8ce{/test-spec,[file:///private/var/folders/h6/yb_lbnnn11g0y1jjlvqg631h0000gn/T/jetty-0.0.0.0-8080-test-spec.war-_test-spec-any-195142828825451628.dir/webapp/, jar:file:///private/var/folders/h6/yb_lbnnn11g0y1jjlvqg631h0000gn/T/jetty-0.0.0.0-8080-test-spec.war-_test-spec-any-195142828825451628.dir/webapp/WEB-INF/lib/test-web-fragment-9.3.11.v20160721.jar!/META-INF/resources],AVAILABLE}{/test-spec.war}
2016-07-25 09:25:49.259:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=15ms
2016-07-25 09:25:49.284:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@55b7a4e0{/,file:///Users/user/installs/repository/jetty-distribution-9.3.11.v20160721/demo-base/webapps/ROOT/,AVAILABLE}{/ROOT}
2016-07-25 09:25:49.323:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=16ms
2016-07-25 09:25:49.325:WARN::main: test-jaas webapp is deployed. DO NOT USE IN PRODUCTION!
2016-07-25 09:25:49.349:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@a514af7{/test-jaas,file:///private/var/folders/h6/yb_lbnnn11g0y1jjlvqg631h0000gn/T/jetty-0.0.0.0-8080-test-jaas.war-_test-jaas-any-6378488515259627366.dir/webapp/,AVAILABLE}{/test-jaas.war}
2016-07-25 09:25:49.436:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=28ms
2016-07-25 09:25:49.438:WARN::main: test-jndi webapp is deployed. DO NOT USE IN PRODUCTION!
2016-07-25 09:25:49.476:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@3febb011{/test-jndi,file:///private/var/folders/h6/yb_lbnnn11g0y1jjlvqg631h0000gn/T/jetty-0.0.0.0-8080-test-jndi.war-_test-jndi-any-5492379371528358988.dir/webapp/,AVAILABLE}{/test-jndi.war}
2016-07-25 09:25:49.553:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=38ms
2016-07-25 09:25:49.555:WARN::main: async-rest webapp is deployed. DO NOT USE IN PRODUCTION!
2016-07-25 09:25:49.576:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@36fc695d{/async-rest,[file:///private/var/folders/h6/yb_lbnnn11g0y1jjlvqg631h0000gn/T/jetty-0.0.0.0-8080-async-rest.war-_async-rest-any-7692963153880036981.dir/webapp/, jar:file:///private/var/folders/h6/yb_lbnnn11g0y1jjlvqg631h0000gn/T/jetty-0.0.0.0-8080-async-rest.war-_async-rest-any-7692963153880036981.dir/webapp/WEB-INF/lib/example-async-rest-jar-9.3.11.v20160721.jar!/META-INF/resources],AVAILABLE}{/async-rest.war}
2016-07-25 09:25:49.669:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=34ms
2016-07-25 09:25:49.670:WARN::main: test webapp is deployed. DO NOT USE IN PRODUCTION!
2016-07-25 09:25:49.913:INFO:oejsh.ManagedAttributeListener:main: update PushFilter null->org.eclipse.jetty.servlets.PushCacheFilter@4e0ae11f on o.e.j.w.WebAppContext@5dda768f{/test,file:///private/var/folders/h6/yb_lbnnn11g0y1jjlvqg631h0000gn/T/jetty-0.0.0.0-8080-test.war-_test-any-2157501520644819622.dir/webapp/,STARTING}{/test.war}
2016-07-25 09:25:49.921:INFO:oejsh.ManagedAttributeListener:main: update QoSFilter null->org.eclipse.jetty.servlets.QoSFilter@376a0d86 on o.e.j.w.WebAppContext@5dda768f{/test,file:///private/var/folders/h6/yb_lbnnn11g0y1jjlvqg631h0000gn/T/jetty-0.0.0.0-8080-test.war-_test-any-2157501520644819622.dir/webapp/,STARTING}{/test.war}
2016-07-25 09:25:49.922:WARN:oeju.DeprecationWarning:main: Using @Deprecated Class org.eclipse.jetty.servlets.MultiPartFilter
2016-07-25 09:25:49.941:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@5dda768f{/test,file:///private/var/folders/h6/yb_lbnnn11g0y1jjlvqg631h0000gn/T/jetty-0.0.0.0-8080-test.war-_test-any-2157501520644819622.dir/webapp/,AVAILABLE}{/test.war}
2016-07-25 09:25:49.952:INFO:oejs.AbstractConnector:main: Started ServerConnector@74f6c5d8{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2016-07-25 09:25:49.957:INFO:oejus.SslContextFactory:main: x509=X509@43015c69(jetty,h=[jetty.eclipse.org],w=[]) for SslContextFactory@19b89d4(file:///Users/user/installs/repository/jetty-distribution-9.3.11.v20160721/demo-base/etc/keystore,file:///Users/user/installs/repository/jetty-distribution-9.3.11.v20160721/demo-base/etc/keystore)
2016-07-25 09:25:49.958:INFO:oejus.SslContextFactory:main: x509=X509@4bbf6d0e(mykey,h=[],w=[]) for SslContextFactory@19b89d4(file:///Users/user/installs/repository/jetty-distribution-9.3.11.v20160721/demo-base/etc/keystore,file:///Users/user/installs/repository/jetty-distribution-9.3.11.v20160721/demo-base/etc/keystore)
2016-07-25 09:25:49.968:INFO:oejs.AbstractConnector:main: Started ServerConnector@13608c15{SSL,[ssl, http/1.1]}{0.0.0.0:8443}
2016-07-25 09:25:49.968:INFO:oejs.Server:main: Started @1850ms

現在,如果你將瀏覽器指向 *localhost:8080,*你將看到一個 Jetty 歡迎頁面以及一些指向示例 Web 應用程式的連結以及指向官方 Jetty 文件和使用者組的連結。注意 :Jetty 發行版附帶的 Web 應用程式並非設計為安全或功能齊全,它們僅作為示例提供。建議不要在生產環境中使用它們。

恭喜! 你已經安裝並啟動了 Jetty Web 伺服器。有關如何配置伺服器以及更高階主題的更多說明,請參閱官方文件。