首先來介紹下 Jetty,根據 wiki 的介紹:html
Jetty 是一個純粹的基於 Java 的網頁服務器和 Java Servlet 容器。儘管網頁服務器一般用來爲人們呈現文檔,可是 Jetty 一般在較大的軟件框架中用於計算機與計算機之間的通訊。java
Jetty 做爲 Eclipse 基金會的一部分,是一個自由和開源項目。該網頁服務器被用在 Apache ActiveMQ、Alfresco、Apache Geronimo、Apache Maven、Google App Engine、Eclipse、FUSE 等產品上。git
Jetty 也是 Lift、Eucalyptus、Red五、Hadoop、I2P 等開源項目的服務器。Jetty 支持最新的 Java Servlet API(帶 JSP 的支持),支持 SPDY 和 WebSocket 協議。github
2016年,Jetty 的代碼主倉庫已經遷移到了 Github ,可是其仍然處於 Eclipse IP Process 政策下開發。web
Jetty 在嵌入式的 Java 應用程序中提供 Web 服務,其已是 Eclipse IDE 中的一個組成部分。它支持 AJP、JASPI、JMX、JNDI、OSGi、WebSocket 和其餘的 Java 技術。apache
Apache Hadoop 是 Jetty 應用在框架中的典型範例。 Hadoop 在幾個模塊中使用Jetty做爲 Web 服務器瀏覽器
總結一下:tomcat
Jetty 是一個 Java 實現的開源的 servlet 容器,它既能夠像 Tomcat 同樣做爲一個完整的 Web 服務器和 Servlet 容器,同時也能夠嵌入在 Java 應用程序中,在 Java 程序中調用 Jetty服務器
由於它的「輕量級」,在不是很複雜的小項目中是個不錯的選擇,啓動(加載)也很是的快速websocket
下面主要看下 Jetty 在嵌入式的 Java 應用程序中的應用
導入依賴就不說了,Jetty 自己就是經過 jar 包的方式分發,或者可使用 Maven 來構建:
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>9.2.1.v20140609</version> </dependency>
固然 Servlet 相關的那些依賴不要忘了加入,而後是 Java 代碼入口:
public static void main(String[] args) throws Exception { Server server = new Server(8080); ResourceHandler resourceHandler = new ResourceHandler(); resourceHandler.setResourceBase("D:/test"); // 可顯示目錄結構,相似 FTP resourceHandler.setDirectoriesListed(true); server.setHandler(resourceHandler); server.start(); }
運行 Java 程序,Jetty 服務器就會啓動了,在瀏覽器中就能夠訪問了,可是這種方式只能訪問靜態頁面,不支持 Servlet/JSP
Java 代碼主入口:
public static void main(String[] args) throws Exception { Server server = new Server(8080); WebAppContext webapp = new WebAppContext(); webapp.setResourceBase("E:/apache-tomcat-7.0.47/webapps/test"); // 也能夠經過設置 war 包的方式 // webapp.setWar("C:/TVPlay.war"); server.setHandler(webapp); server.start(); }
就是設置一個 Java Web 應用程序的目錄就能夠了,這種是使用外部文件(地址)的方式
不少時候是咱們須要寫幾個 Servlet,犯不着建個 web 工程,這時候用 Jetty 來嵌入一個服務器最合適不過了,主入口:
public static void main(String[] args) throws Exception { Server server = new Server(8080); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); // Or ServletContextHandler.NO_SESSIONS context.setContextPath("/"); server.setHandler(context); // http://localhost:8080/hello context.addServlet(new ServletHolder(new HelloServlet()), "/hello"); // http://localhost:8080/hello/Kerronex context.addServlet(new ServletHolder(new HelloServlet("Hello Kerronex!")), "/hello/Kerronex"); server.start(); server.join(); }
具體對應的 Servlet 我就不貼了,很簡單的 doGet 測試下就能夠了~~
打包後直接用命令 java -jar xxx.jar
容許就能夠啦
若是 server 沒有起來,這裏面 join() 函數起到的做用就是使線程阻塞, 這裏 join() 函數實質上調用的 jetty 的線程池( 這裏和 Thread 中的 join 函數類似 )
若是沒有 join 函數,jetty 服務器也能正常啓動或運行正常,是由於 jetty 比較小,啓動速度很是快
然而若是你的 application 比較重的話, 調用 join 函數,可以保證你的 server 真正的起來(也就是說在 jetty start 以前 join 方法都是阻塞狀態,避免 JVM 退出)
TODO:使用 Jetty 構建 web 項目
須要使用插件及相關依賴:
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-websocket</artifactId> <version>8.1.11.v20130520</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>8.1.11.v20130520</version> </dependency> <!-- jetty --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>8.1.11.v20130520</version> </dependency> <build> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.7.v20160115</version> <configuration> <webApp> <contextPath>/</contextPath> </webApp> <scanIntervalSeconds>3</scanIntervalSeconds> <scanTargetPatterns> <scanTargetPattern> <directory>src/main/webapp</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </scanTargetPattern> </scanTargetPatterns> <webAppConfig> <defaultsDescriptor>src/main/resource/webdefault222.xml</defaultsDescriptor> </webAppConfig> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>8010</port> <maxIdleTime>400000</maxIdleTime> </connector> </connectors> </configuration> </plugin> </plugins> </build>
相關命令:
mvn jetty:run
mvn -Djetty.http.port=9999 jetty:run
參見:
https://www.zhihu.com/question/52433013
http://www.blogjava.net/fancydeepin/archive/2015/06/23/maven-jetty-plugin.html
http://blog.csdn.net/tomato__/article/details/37927813
單獨下載的 Jetty 的 jar 包就能夠單獨運行,也是使用 Java -jar 命令