jetty是什麼?html
jetty是輕量級的web服務器和servlet引擎。java
它的最大特色是:能夠很方便的做爲嵌入式服務器。git
它是eclipse的一個開源項目。不用懷疑,就是你經常使用的那個eclipse。github
它是使用Java開發的,因此自然對Java支持良好。web
官方網址spring
github源碼地址apache
什麼是嵌入式服務器?api
以jetty來講明,就是隻要引入jetty的jar包,能夠經過直接調用其API的方式來啓動web服務。服務器
用過Tomcat、Resin等服務器的朋友想必不會陌生那一套安裝、配置、部署的流程吧,仍是挺繁瑣的。使用jetty,就不須要這些過程了。app
jetty很是適用於項目的開發、測試,由於很是快捷。若是想用於生產環境,則須要謹慎考慮,它不必定能像成熟的Tomcat、Resin等服務器同樣支持企業級Java EE的須要。
我以爲嵌入式啓動方式的一個好處在於:能夠直接運行項目,無需每次部署都得再配置服務器。
jetty的嵌入式啓動使用有兩種方式:
API方式
maven插件方式
添加maven依賴
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>9.3.2.v20150730</version> <scope>test</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-annotations</artifactId> <version>9.3.2.v20150730</version> <scope>test</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>apache-jsp</artifactId> <version>9.3.2.v20150730</version> <scope>test</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>apache-jstl</artifactId> <version>9.3.2.v20150730</version> <scope>test</scope> </dependency>
官方的啓動代碼
public class SplitFileServer { public static void main( String[] args ) throws Exception { // 建立Server對象,並綁定端口 Server server = new Server(); ServerConnector connector = new ServerConnector(server); connector.setPort(8090); server.setConnectors(new Connector[] { connector }); // 建立上下文句柄,綁定上下文路徑。這樣啓動後的url就會是:http://host:port/context ResourceHandler rh0 = new ResourceHandler(); ContextHandler context0 = new ContextHandler(); context0.setContextPath("/"); // 綁定測試資源目錄(在本例的配置目錄dir0的路徑是src/test/resources/dir0) File dir0 = MavenTestingUtils.getTestResourceDir("dir0"); context0.setBaseResource(Resource.newResource(dir0)); context0.setHandler(rh0); // 和上面的例子同樣 ResourceHandler rh1 = new ResourceHandler(); ContextHandler context1 = new ContextHandler(); context1.setContextPath("/"); File dir1 = MavenTestingUtils.getTestResourceDir("dir1"); context1.setBaseResource(Resource.newResource(dir1)); context1.setHandler(rh1); // 綁定兩個資源句柄 ContextHandlerCollection contexts = new ContextHandlerCollection(); contexts.setHandlers(new Handler[] { context0, context1 }); server.setHandler(contexts); // 啓動 server.start(); // 打印dump時的信息 System.out.println(server.dump()); // join當前線程 server.join(); } }
直接運行Main方法,就能夠啓動web服務。
注:以上代碼在eclipse中運行沒有問題,若是想在Intellij中運行還須要爲它指定配置文件。
若是想了解在Eclipse和Intellij都能運行的通用方法能夠參考個人github代碼示例。
個人實現也是參考springside的方式。
代碼行數有點多,不在這裏貼代碼了。
若是你熟悉maven,那麼實在太簡單了
注: Maven版本必須在3.3及以上版本。
(1) 添加maven插件
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.12.v20160915</version> </plugin>
(2) 執行maven命令:
mvn jetty:run
講真,就是這麼簡單。jetty默認會爲你建立一個web服務,地址爲127.0.0.1:8080。
固然,你也能夠在插件中配置你的webapp環境
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.12.v20160915</version> <configuration> <webAppSourceDirectory>${project.basedir}/src/staticfiles</webAppSourceDirectory> <!-- 配置webapp --> <webApp> <contextPath>/</contextPath> <descriptor>${project.basedir}/src/over/here/web.xml</descriptor> <jettyEnvXml>${project.basedir}/src/over/here/jetty-env.xml</jettyEnvXml> </webApp> <!-- 配置classes --> <classesDirectory>${project.basedir}/somewhere/else</classesDirectory> <scanClassesPattern> <excludes> <exclude>**/Foo.class</exclude> </excludes> </scanClassesPattern> <scanTargets> <scanTarget>src/mydir</scanTarget> <scanTarget>src/myfile.txt</scanTarget> </scanTargets> <!-- 掃描target目錄下的資源文件 --> <scanTargetPatterns> <scanTargetPattern> <directory>src/other-resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <excludes> <exclude>**/myspecial.xml</exclude> <exclude>**/myspecial.properties</exclude> </excludes> </scanTargetPattern> </scanTargetPatterns> </configuration> </plugin>
官方給的jetty-env.xml範例
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd"> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <!-- Add an EnvEntry only valid for this webapp --> <New id="gargle" class="org.eclipse.jetty.plus.jndi.EnvEntry"> <Arg>gargle</Arg> <Arg type="java.lang.Double">100</Arg> <Arg type="boolean">true</Arg> </New> <!-- Add an override for a global EnvEntry --> <New id="wiggle" class="org.eclipse.jetty.plus.jndi.EnvEntry"> <Arg>wiggle</Arg> <Arg type="java.lang.Double">55.0</Arg> <Arg type="boolean">true</Arg> </New> <!-- an XADataSource --> <New id="mydatasource99" class="org.eclipse.jetty.plus.jndi.Resource"> <Arg>jdbc/mydatasource99</Arg> <Arg> <New class="com.atomikos.jdbc.SimpleDataSourceBean"> <Set name="xaDataSourceClassName">org.apache.derby.jdbc.EmbeddedXADataSource</Set> <Set name="xaDataSourceProperties">databaseName=testdb99;createDatabase=create</Set> <Set name="UniqueResourceName">mydatasource99</Set> </New> </Arg> </New> </Configure>