ITPub博客

首页 > 应用开发 > IT综合 > Jetty实战之 maven+嵌入式Jetty运行web app

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

IT综合 作者:dishuikongming 时间:2014-01-02 13:56:28 0 删除 编辑

要说嵌入式运行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>

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.         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的例子界面了。

个人实践总结:
1、上面实例是介绍的绝对路径,但是如果想让jetty在项目中真正嵌入必须使用相对路径,要不移植性不高,以下是个人代码:

点击(此处)折叠或打开

  1. package com.sunny.server;
  2. import org.eclipse.jetty.server.Server;
  3. import org.eclipse.jetty.webapp.WebAppContext;
  4. public class JettyServer {
  5. public static void main(String[] args) {
  6. Server server = new Server(8080);
  7.        WebAppContext context = new WebAppContext();
  8.        context.setContextPath(\"/ext\");
  9.       String ProPath= System.getProperty(\"user.dir\");
  10.        context.setDescriptor(ProPath+\"/src/main/webapp/WEB-INF/web.xml\");
  11.        context.setResourceBase(ProPath+\"/src/main/webapp\");
  12.        context.setParentLoaderPriority(true);
  13.        server.setHandler(context);
  14.        try {
  15. server.start();
  16. server.join();
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }


2、如果想运行简单jsp页面必须加入<!-- JSP Support -->和<!-- EL Support -->下面的依赖包,其他的根据个人需要,以下是个人pom;

点击(此处)折叠或打开

  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.sunnyit</groupId>
  5. <artifactId>ExtJsDemo</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>ExtJsDemo Maven Webapp</name>
  9. <url>http://maven.apache.org</url>

  10. <!-- 指定Maven仓库 -->
  11. <repositories>
  12. <repository>
  13. <snapshots>
  14. <enabled>true</enabled>
  15. </snapshots>
  16. <id>public</id>
  17. <name>Public Repositories</name>
  18. <url>http://192.168.17.x:8081/nexus/content/groups/public/</url>
  19. </repository>
  20. </repositories>
  21. <pluginRepositories>
  22. <pluginRepository>
  23. <id>public</id>
  24. <name>Public Repositories</name>
  25. <url>http://192.168.17.x:8081/nexus/content/groups/public/</url>
  26. </pluginRepository>
  27. </pluginRepositories>

  28. <properties>
  29. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  30. </properties>

  31. <dependencies>
  32. <dependency>
  33. <groupId>junit</groupId>
  34. <artifactId>junit</artifactId>
  35. <version>3.8.1</version>
  36. <scope>test</scope>
  37. </dependency>


  38. <!-- Jetty -->
  39. <dependency>
  40. <groupId>org.eclipse.jetty.aggregate</groupId>
  41. <artifactId>jetty-all</artifactId>
  42. <version>8.0.4.v20111024</version>
  43. </dependency>


  44. <!-- Jetty Webapp -->
  45. <dependency>
  46. <groupId>org.eclipse.jetty</groupId>
  47. <artifactId>jetty-webapp</artifactId>
  48. <version>8.0.4.v20111024</version>
  49. </dependency>

  50. <!-- JSP Support -->
  51.         <dependency>
  52.             <groupId>org.glassfish.web</groupId>
  53.             <artifactId>javax.servlet.jsp</artifactId>
  54.             <version>2.2.3</version>
  55.         </dependency>
  56.         
  57.         <!-- EL Support -->
  58.         <dependency>
  59.             <groupId>org.glassfish.web</groupId>
  60.             <artifactId>javax.el</artifactId>
  61.             <version>2.2.3</version>
  62.         </dependency>
  63. </dependencies>
  64. <build>
  65. <finalName>ExtJsDemo</finalName>
  66. </build>
  67. </project>







来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28562677/viewspace-1066401/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-12-31

  • 博文量
    7
  • 访问量
    106923