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



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

要说嵌入式运行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.         <!– Jetty –>
  32.         <dependency>
  33.             <groupId>org.eclipse.jetty.aggregate</groupId>
  34.             <artifactId>jetty-all</artifactId>
  35.             <version>8.0.4.v20111024</version>
  36.         </dependency>
  37.         <!– Jetty Webapp –>
  38.         <dependency>
  39.             <groupId>org.eclipse.jetty</groupId>
  40.             <artifactId>jetty-webapp</artifactId>
  41.             <version>8.0.4.v20111024</version>
  42.         </dependency>
  43.         <!– JSP Support –>
  44.         <dependency>
  45.             <groupId>org.glassfish.web</groupId>
  46.             <artifactId>javax.servlet.jsp</artifactId>
  47.             <version>2.2.3</version>
  48.         </dependency>
  49.         <!– EL Support –>
  50.         <dependency>
  51.             <groupId>org.glassfish.web</groupId>
  52.             <artifactId>javax.el</artifactId>
  53.             <version>2.2.3</version>
  54.         </dependency>
  55.         <!– JSTL Support –>
  56.         <dependency>
  57.             <groupId>org.glassfish.web</groupId>
  58.             <artifactId>javax.servlet.jsp.jstl</artifactId>
  59.             <version>1.2.1</version>
  60.             <exclusions>
  61.                 <exclusion>
  62.                     <artifactId>jstl-api</artifactId>
  63.                     <groupId>javax.servlet.jsp.jstl</groupId>
  64.                 </exclusion>
  65.             </exclusions>
  66.         </dependency>
  67.     </dependencies>
  68. </project>

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. import org.eclipse.jetty.server.Server;
  3. import org.eclipse.jetty.webapp.WebAppContext;
  4. public class WebAppContextWithWarServer {
  5.     public static void main(String[] args) throws Exception {
  6.         Server server = new Server(8080);
  7.         WebAppContext context = new WebAppContext();
  8.         context.setContextPath(“/myapp”);
  9.         context.setWar(“E:/share/test/struts2-blank.war”);
  10.         server.setHandler(context);
  11.         server.start();
  12.         server.join();
  13.     }
  14. }


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. import org.eclipse.jetty.server.Server;
  3. import org.eclipse.jetty.webapp.WebAppContext;
  4. public class WebAppContextWithFolderServer {
  5.     public static void main(String[] args) throws Exception {
  6.         Server server = new Server(8080);
  7.         WebAppContext context = new WebAppContext();
  8.         context.setContextPath(“/myapp”);
  9.         context.setDescriptor(“E:/share/test/struts2-blank/WEB-INF/web.xml”);
  10.         context.setResourceBase(“E:/share/test/struts2-blank”);
  11.         context.setParentLoaderPriority(true);
  12.         server.setHandler(context);
  13.         server.start();
  14.         server.join();
  15.     }
  16. }

2.3 运行WebAppContextWithFolderServer类,然后访问// http://localhost:8080/myapp/就可以看到struts2的例子界面了。