jetty码头入门


备注

Jetty是一个开源项目,提供轻量级HTTP服务器,HTTP客户端和javax.servlet容器。

Jetty项目由Eclipse Foundation托管,位于: https//www.eclipse.org/jetty/ 。 Eclipse上的Jetty页面包括最新版Jetty的下载 ,以及官方文档JavaDocs

Jetty的代码库托管在GitHub上

安装和运行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服务器。有关如何配置服务器以及更高级主题的更多说明,请参阅官方文档。