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

转载地址:http://blog.csdn.net/kongxx/article/details/7237034

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

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

写道
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.code.garbagecan.jettystudy</groupId>
<artifactId>jettystudy</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>jettystudy</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<inherited>true</inherited>
<version>2.3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<debug>true</debug>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Spring support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>

<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
<version>8.0.4.v20111024</version>
</dependency>

<!-- Jetty Webapp -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.0.4.v20111024</version>
</dependency>

<!-- JSP Support -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.servlet.jsp</artifactId>
<version>2.2.3</version>
</dependency>

<!-- EL Support -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.3</version>
</dependency>

<!-- JSTL Support -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.servlet.jsp.jstl</artifactId>
<version>1.2.1</version>
<exclusions>
<exclusion>
<artifactId>jstl-api</artifactId>
<groupId>javax.servlet.jsp.jstl</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

 1. 运行标准的war文件

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

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

    package com.google.code.garbagecan.jettystudy.sample6;  
      
    import org.eclipse.jetty.server.Server;  
    import org.eclipse.jetty.webapp.WebAppContext;  
      
    public class WebAppContextWithWarServer {  
        public static void main(String[] args) throws Exception {  
            Server server = new Server(8080);  
      
            WebAppContext context = new WebAppContext();  
            context.setContextPath("/myapp");  
            context.setWar("E:/share/test/struts2-blank.war");  
            server.setHandler(context);  
      
            server.start();  
            server.join();  
        }  
    }  

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

 

2. 运行一个webapp目录

2.1 还是用上面的struts2-blank.war,将这个war包解压后放到一个目录下;

2.2 创建自己的Jetty Server启动类WebAppContextWithFolderServer,其中指定了webapp目录,并指定context路径为"/myapp"

    package com.google.code.garbagecan.jettystudy.sample6;  
      
    import org.eclipse.jetty.server.Server;  
    import org.eclipse.jetty.webapp.WebAppContext;  
      
    public class WebAppContextWithFolderServer {  
        public static void main(String[] args) throws Exception {  
            Server server = new Server(8080);  
      
            WebAppContext context = new WebAppContext();  
            context.setContextPath("/myapp");  
            context.setDescriptor("E:/share/test/struts2-blank/WEB-INF/web.xml");  
            context.setResourceBase("E:/share/test/struts2-blank");  
            context.setParentLoaderPriority(true);  
            server.setHandler(context);  
      
            server.start();  
            server.join();  
        }  
    }  

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

你可以按照以下步骤创建一个基于Maven的Web项目: 1. 安装Java开发工具包(JDK):确保你的系统中已经安装了适当版本的JDK。 2. 安装Apache Maven:Maven是一个用于构建和管理Java项目的工具。你可以从Maven官方网站(https://maven.apache.org)下载并安装最新版本的Maven。 3. 创建Maven项目:打开命令行终端或者使用你喜欢的IDE(例如Eclipse、IntelliJ IDEA等),运行以下命令创建一个新的Maven项目: ``` mvn archetype:generate -DgroupId=com.example -DartifactId=my-web-app -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false ``` 这将使用Maven的Web Application骨架创建一个名为`my-web-app`的项目,你可以根据需要修改groupId和artifactId参数。 4. 进入项目目录:执行以下命令进入项目目录: ``` cd my-web-app ``` 5. 编辑项目配置:使用你喜欢的编辑器打开`pom.xml`文件,可以在此文件中添加项目依赖、插件和其他配置信息。 6. 构建项目:运行以下命令构建项目: ``` mvn clean install ``` Maven将会下载所需的依赖包并构建项目。 7. 运行项目:你可以通过不同的方法来运行你的Web应用程序,例如使用Maven插件或将项目部署到支持Java Web应用服务器中。 - 使用Maven插件运行项目:执行以下命令启动嵌入式应用服务器并运行你的Web应用程序: ``` mvn jetty:run ``` - 部署到应用服务器:将生成的war文件部署到支持Java Web应用服务器(如Tomcat、Jetty等)中运行。 这,你就成功创建了一个基于Maven的Web项目。根据你的需求,你可以添加其他依赖、配置servlet、编写业务逻辑等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值