Jetty实战之 嵌入式Jetty运行web app
2016-06-08 17:57
357 查看
Jetty实战之 嵌入式Jetty运行web app
要说嵌入式运行Jetty,最常用的还应该是运行一个标准的war文件或者指定一个webapp目录。首先需要添加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"
Java代码
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代码
2.3 运行WebAppContextWithFolderServer类,然后访问//http://localhost:8080/myapp/就可以看到struts2的例子界面了。
要说嵌入式运行Jetty,最常用的还应该是运行一个标准的war文件或者指定一个webapp目录。首先需要添加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"
Java代码
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"
Java代码
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的例子界面了。
相关文章推荐
- i-jetty环境搭配与编译
- jetty小试
- maven学习
- maven插件maven-war-plugin的使用
- jenkins------部署项目到jboss eap下
- maven使用经验集
- 开发阶段Jetty运行Jsp报错且响应空白
- 实战无线局域网
- JavaScript使用DeviceOne开发实战(三)仿微信应用
- JavaScript使用DeviceOne开发实战(二) 生成调试安装包
- JavaScript使用DeviceOne开发实战(一) 配置和起步
- java application maven项目打自定义zip包实例(推荐)
- window下安装和配置maven环境
- Eclipse使用maven搭建spring mvc图文教程
- 使用maven+eclipse搭建struts2开发环境
- myeclipse中使用maven前常见错误及解决办法
- 深入jetty的使用详解
- Java随手笔记8之包、环境变量和访问控制及maven profile实现多环境打包
- maven利用tomcat插件部署远程Linux服务器的步骤详解
- maven搭建java ee项目图文教程