要說嵌入式運行Jetty,最常用的還應該是運行一個標准的war文件或者指定一個webapp目錄。
0. 首先需要添加Jetty運行時webapp的依賴包,下面是一個完整的pom.xml文件
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.google.code.garbagecan.jettystudy</groupId> 5 <artifactId>jettystudy</artifactId> 6 <packaging>jar</packaging> 7 <version>1.0-SNAPSHOT</version> 8 <name>jettystudy</name> 9 <url>http://maven.apache.org</url> 10 <build> 11 <plugins> 12 <plugin> 13 <artifactId>maven-compiler-plugin</artifactId> 14 <inherited>true</inherited> 15 <version>2.3.1</version> 16 <configuration> 17 <source>1.6</source> 18 <target>1.6</target> 19 <debug>true</debug> 20 </configuration> 21 </plugin> 22 </plugins> 23 </build> 24 <dependencies> 25 <!-- Spring support --> 26 <dependency> 27 <groupId>org.springframework</groupId> 28 <artifactId>spring</artifactId> 29 <version>2.5.6</version> 30 </dependency> 31 32 <!-- Jetty --> 33 <dependency> 34 <groupId>org.eclipse.jetty.aggregate</groupId> 35 <artifactId>jetty-all</artifactId> 36 <version>8.0.4.v20111024</version> 37 </dependency> 38 39 <!-- Jetty Webapp --> 40 <dependency> 41 <groupId>org.eclipse.jetty</groupId> 42 <artifactId>jetty-webapp</artifactId> 43 <version>8.0.4.v20111024</version> 44 </dependency> 45 46 <!-- JSP Support --> 47 <dependency> 48 <groupId>org.glassfish.web</groupId> 49 <artifactId>javax.servlet.jsp</artifactId> 50 <version>2.2.3</version> 51 </dependency> 52 53 <!-- EL Support --> 54 <dependency> 55 <groupId>org.glassfish.web</groupId> 56 <artifactId>javax.el</artifactId> 57 <version>2.2.3</version> 58 </dependency> 59 60 <!-- JSTL Support --> 61 <dependency> 62 <groupId>org.glassfish.web</groupId> 63 <artifactId>javax.servlet.jsp.jstl</artifactId> 64 <version>1.2.1</version> 65 <exclusions> 66 <exclusion> 67 <artifactId>jstl-api</artifactId> 68 <groupId>javax.servlet.jsp.jstl</groupId> 69 </exclusion> 70 </exclusions> 71 </dependency> 72 </dependencies> 73 </project>
其實不需要這么多依賴,maven的依賴只需要一個依賴就ok了
1 <dependency> 2 <groupId>org.mortbay.jetty</groupId> 3 <artifactId>jetty-maven-plugin</artifactId> 4 <version>8.1.16.v20140903</version> 5 </dependency>
1. 運行標准的war文件
1.1 首先找一個完整的war包,這里使用了struts2自帶的一個例子應用程序struts2-blank.war;
1.2 創建自己的Jetty Server啟動類WebAppContextWithWarServer,其中指定了war文件的路徑,並指定context路徑為"/myapp"
1 package com.google.code.garbagecan.jettystudy.sample6; 2 3 import org.eclipse.jetty.server.Server; 4 import org.eclipse.jetty.webapp.WebAppContext; 5 6 public class WebAppContextWithWarServer { 7 public static void main(String[] args) throws Exception { 8 Server server = new Server(8080); 9 10 WebAppContext context = new WebAppContext(); 11 context.setContextPath("/myapp"); 12 context.setWar("E:/share/test/struts2-blank.war"); 13 server.setHandler(context); 14 15 server.start(); 16 server.join(); 17 } 18 }
1.3 運行WebAppContextWithWarServer類,然后訪問// http://localhost:8080/myapp/就可以看到struts2的例子界面了。
2. 運行一個webapp目錄
2.1 還是用上面的struts2-blank.war,將這個war包解壓后放到一個目錄下;
2.2 創建自己的Jetty Server啟動類WebAppContextWithFolderServer,其中指定了webapp目錄,並指定context路徑為"/myapp"
1 package com.google.code.garbagecan.jettystudy.sample6; 2 3 import org.eclipse.jetty.server.Server; 4 import org.eclipse.jetty.webapp.WebAppContext; 5 6 public class WebAppContextWithFolderServer { 7 public static void main(String[] args) throws Exception { 8 Server server = new Server(8080); 9 10 WebAppContext context = new WebAppContext(); 11 context.setContextPath("/myapp"); 12 //web.xml可以改成其他名字,如web1.xml。 13 //如果不指明配置文件的位置,會根據項目資源setResourceBase路徑找下面的web.xml。如果找不到,默認頁面index.html優先於index.jsp 14 context.setDescriptor("E:/HLZT/chat/src/main/webapp/WEB-INF/web.xml"); //maven項目web.xml路徑 15 16 context.setResourceBase("E:/HLZT/chat/src/main/webapp/");//maven項目資源路徑 17 //context.setDescriptor("E:/HLZT/chat/WEB-INF/web.xml"); //普通項目web.xml路徑 18 //context.setResourceBase("E:/HLZT/chat/WebRoot");//普通項目資源路徑 19 20 21 context.setParentLoaderPriority(true); 22 server.setHandler(context); 23 24 server.start(); 25 server.join(); 26 } 27 }
2.3 運行WebAppContextWithFolderServer類,然后訪問// http://localhost:8080/myapp/就可以看到struts2的例子界面
