Jetty实战之 嵌入式Jetty运行web app

来源:互联网 发布:白夜追凶小说结局知乎 编辑:程序博客网 时间:2024/03/29 01:03

要说嵌入式运行Jetty,最常用的还应该是运行一个标准的war文件或者指定一个webapp目录。

0. 首先需要添加Jetty运行时webapp的依赖包,下面是一个完整的pom.xml文件

[html] view plaincopyprint?
  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>  
1. 运行标准的war文件

1.1 首先找一个完整的war包,这里使用了struts2自带的一个例子应用程序struts2-blank.war;

1.2 创建自己的Jetty Server启动类WebAppContextWithWarServer,其中指定了war文件的路径,并指定context路径为"/myapp"

[java] view plaincopyprint?
  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"

[java] view plaincopyprint?
  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.         context.setDescriptor("E:/share/test/struts2-blank/WEB-INF/web.xml");  
  13.         context.setResourceBase("E:/share/test/struts2-blank");  
  14.         context.setParentLoaderPriority(true);  
  15.         server.setHandler(context);  
  16.   
  17.         server.start();  
  18.         server.join();  
  19.     }  
  20. }  
2.3 运行WebAppContextWithFolderServer类,然后访问// http://localhost:8080/myapp/就可以看到struts2的例子界面了。

0 0
原创粉丝点击