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


    [转自] http://blog.csdn.net/kongxx/article/details/7237034


    要说嵌入式运行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的例子界面了。
  • 相关阅读:
    Mysql学习(慕课学习笔记7)修改数据表(下)
    Mysql学习(慕课学习笔记1)启动、登录及常用命令
    Mysql学习(慕课学习笔记2)数据库的创建与删除
    手机测试体系讲解
    Android开发之旅:环境搭建
    免费搭建wordpress博客有感
    第一篇
    浅谈通信网络(二)——信号
    小dai浅谈通信网络(一)——引子
    投票调查系统数据库设计及大家指教
  • 原文地址:https://www.cnblogs.com/pekkle/p/6568707.html
Copyright © 2020-2023  润新知