要说嵌入式运行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的例子界面

posted on 2017-10-28 22:21  Sharpest  阅读(2889)  评论(0编辑  收藏  举报