Jetty 是一個開源的servlet容器,它為基於Java的web容器,例如JSP和servlet提供運行環境。Jetty是使用Java語言編寫的,它的API以一組JAR包的形式發布。開發人員可以將Jetty容器實例化成一個對象,可以迅速為一些獨立運行(stand-alone)的Java應用提供網絡和web連接。
易用性
易用性是 Jetty 設計的基本原則,易用性主要體現在以下幾個方面:
通過 XML 或者 API 來對Jetty進行配置;默認配置可以滿足大部分的需求;將 Jetty 嵌入到應用程序當中只需要非常少的代碼;
可擴展性
在使用了 Ajax 的 Web 2.0 的應用程序中,每個連接需要保持更長的時間,這樣線程和內存的消耗量會急劇的增加。這就使得我們擔心整個程序會因為單個組件陷入瓶頸而影響整個程序的性能。但是有了 Jetty:
即使在有大量服務請求的情況下,系統的性能也能保持在一個可以接受的狀態。利用 Continuation 機制來處理大量的用戶請求以及時間比較長的連接。 另外 Jetty 設計了非常良好的接口,因此在 Jetty 的某種實現無法滿足用戶的需要時,用戶可以非常方便地對 Jetty 的某些實現進行修改,使得 Jetty 適用於特殊的應用程序的需求。
易嵌入性
Jetty 設計之初就是作為一個優秀的組件來設計的,這也就意味着 Jetty 可以非常容易的嵌入到應用程序當中而不需要程序為了使用 Jetty 做修改。從某種程度上,你也可以把 Jetty 理解為一個嵌入式的Web服務器。
Jetty 可以作為嵌入式服務器使用,Jetty的運行速度較快,而且是輕量級的,可以在Java中可以從test case中控制其運行。從而可以使
自動化測試不再依賴外部環境,順利實現自動化測試。
1)Jetty更輕量級。這是相對Tomcat而言的。
由於Tomcat除了遵循Java Servlet規范之外,自身還擴展了大量JEE特性以滿足企業級應用的需求,所以Tomcat是較重量級的,而且配置較Jetty亦復雜許多。但對於大量普通互聯網應用而言,並不需要用到Tomcat其他高級特性,所以在這種情況下,使用Tomcat是很浪費資源的。這種劣勢放在分布式環境下,更是明顯。換成Jetty,每個應用服務器省下那幾兆內存,對於大的分布式環境則是節省大量資源。而且,Jetty的輕量級也使其在處理高並發細粒度請求的場景下顯得更快速高效。
2)Jetty更靈活,體現在其可插拔性和可擴展性,更易於開發者對Jetty本身進行二次開發,定制一個適合自身需求的Web Server。
相比之下,重量級的Tomcat原本便支持過多特性,要對其瘦身的成本遠大於豐富Jetty的成本。用自己的理解,即增肥容易減肥難。
3)然而,當支持大規模企業級應用時,Jetty也許便需要擴展,在這場景下Tomcat便是更優的。
總結:Jetty更滿足公有雲的分布式環境的需求,而Tomcat更符合企業級環境。
小例子:
本文介紹一個使用Jetty開發的Web的小例子。
Jetty是一個開源的servlet容器,它為基於Java的web容器(例如JSP和servlet)提供運行環境。可以把它理解為和Tomcat一樣,不過Jetty更小更輕量級。如果想更深入學習Jetty推薦看:http://blog.sina.com.cn/s/blog_9ed7f0d70101ivsu.html,這里面的一系列的文章分別對jetty服務流程、對靜態資源的訪問方法、把servlet部署到jetty里面、ContextHandler 的使用,都做了非常簡潔、通俗易懂的介紹,本文就“把servlet部署到jetty里面”做介紹
本文使用的Maven建工程,對Maven不熟悉的請看:http://blog.csdn.net/tiandixuanwuliang/article/details/78752881
下面開始介紹這個小例子:
1、在maven項目中配置jetty和servlet
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>testJetty</groupId> <artifactId>testJetty</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.compile.version>1.8</project.compile.version> <servlet.version>3.1.0</servlet.version> <jsp.version>2.2</jsp.version> <jstl.version>1.2.5</jstl.version> <jetty.version>8.1.10.v20130312</jetty.version> </properties> <dependencies> <!-- Web Dependencies start --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp.version}</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-compat</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-impl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-jstlel</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-spec</artifactId> <version>${jstl.version}</version> </dependency> <!-- Web Dependencies end --> <!-- jetty start --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.0.0.v20130308</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>9.0.0.v20130308</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-continuation</artifactId> <version>9.0.0.v20130308</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-jsp</artifactId> <version>9.0.0.v20130308</version> </dependency> <!-- jetty end --> </dependencies> </project>
2、寫一個Servlet
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class MainServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); resp.setHeader("content-type","text/html;charset=UTF-8"); System.out.println("servlet運行起來了"); resp.getWriter().write("servlet運行起來了"); resp.getWriter().close(); } }
3、寫Jetty啟動文件(注意:不能加包名)
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import com.welljoint.MainServlet; public class HelloJetty { /** * @throws Exception * @Title: main * @Description:啟動jetty,程序入口 * @param args * @throws */ public static void main(String[] args) throws Exception { Server server = new Server(8080); //servletContextHandler是把servlet部署到jetty里面的橋梁 ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); //所有請求都要經過jetty context.setContextPath("/"); server.setHandler(context); //配置servlet請求路徑 context.addServlet(new ServletHolder(new MainServlet()), "/hello"); server.start(); server.join(); } }
4、在Jetty的類的main方法上單擊右鍵選擇java項目運行方式(注意是像運行java項目一樣運行)
參考:
jetty
參考:
jetty小例子