`
changhongbao
  • 浏览: 117241 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Jetty实战(6)之嵌入式Jetty运行web app

阅读更多

要说嵌入式运行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的例子界面了。

分享到:
评论

相关推荐

    &nbsp;基于Android的嵌入式Web服务器设计

    &nbsp;随着Internet技术的兴起,在嵌入式设备的管理与交互中,基于Web方式...本文主要论述了基于Android系统环境,在家庭网关中实现嵌入式Web服务器的设计方法,介绍了i-jetty嵌入式Web服务器,及其Web应用功能的实现。

    emb-app:带有嵌入式 Jetty 的简单微服务

    构建代码 mvn package使用 belly-shade 插件和 Jetty 作为 servlet 容器制作一个 uberjar。创建泊坞窗图像使用作为基础。 给出约300M的图像 sudo docker build --rm=true -t simple-microservice .以为基础,镜像160...

    spring-boot-web-ecc

    这个小示例演示了如何使用您选择的嵌入式 Servlet 容器(Tomcat、Jetty 或 Undertow)设置可运行的 Spring Boot 应用程序。 此示例可用于快速简便的嵌入式容器配置(因此后缀ecc )。 嵌入式Tomcat 此示例显示...

    jsf_primefaces_app_with_embedded_jetty_9_container:一个非常酷的成就。 我设法运行了一个 JSF

    jsf_primefaces_app_with_embedded_jetty_9_container 真是一件很酷的事情。 我设法使用嵌入式 Jetty9 容器运行带有 primefaces 框架的 JSF 应用程序。

    lcs-jersey-webapp

    U-Equations首次推送到GitHub:LCS Jersey RESTful Web App U-Equations首次推向Github的是经典的最长公共子字符串算法,该算法在具有... 可以使用以下命令在Jetty 9嵌入式服务器上运行Web应用程序: mvn clean

    jetty-cloud:用于Cloudfoundry部署的示例嵌入式码头项目

    这是Jetty Web服务器的示例项目,该项目能够在Cloud Foundry环境中运行,例如IBM Bluemix。 这个maven项目创建了一个jar,其中包含启动应用程序所需的所有内容。 您可以在App类中自定义Web服务器,该类类似于码头...

    jettyhelloworld

    适用于Java 11的Google App Engine标准的嵌入式Jetty服务器要迁移到Java 11运行时,您的应用程序必须具有启动Web服务器的Main类。 此样本是一个共享工件,该工件提供Main类来实例化HTTP服务器以运行嵌入式Web应用...

    JavaBlogFeedBurner:带有 Maven、Spring、Hibernate 的 Java 项目

    基于 Spring 和 Hibernate在开发模式下运行(使用嵌入式 HSQL 数据库): 使用嵌入式 Jetty 服务器运行此应用程序: mvn -P dev jetty:run -Dspring.profiles.active="dev" 这将在端口 8080 上启动嵌入式 Jetty ...

    Jfinalplugin,angularjfinal-angular-icedog.zip

    一个Jfinal angular的框架实现,属于jfinal-dreampie的一个demo,在线访问:... 可直接使用mvn jetty:run 使用嵌入式数据库h2,数据库可以自动验证生成初始化数据 图片: 标签:jfinal

    Electronic-Document-Management-System:使用 Spring 和 Hibernate 进行 EDMS 软件开发

    在开发模式下运行(使用嵌入式 HSQL 数据库): 使用嵌入式 Jetty 服务器运行此应用程序: mvn -P dev jetty:run -Dspring.profiles.active="dev" 这将在端口 8080 上启动嵌入式 Jetty 服务器,您可以在此处访问您的...

    java-springmvc-heroku-template:使用 Java、Jetty、SpringMVC、Spock 测试、Swagger 和 Heroku 进行 API 开发的模板项目

    使用 Java、Jetty、SpringMVC、Spock、Swagger 和 Heroku 进行 API 开发的模板项目 该项目的目的是为独立的 Java 应用程序提供模板: 用于 HTTP 支持的嵌入式码头容器, SpringMVC 用于轻松开发 REST API, ...

    donovan:用于模拟和测试的致命简单 Java 8 Web 应用程序框架

    Donovan 使用嵌入式 Jetty 或 Tomcat 来实现这一目的。 这个框架的灵感来自 。 概要 带有嵌入式码头 应用程序.java @Slf4j public class App { public static void main ( String [] args ) throws Exception {...

    jfinalpluginsdreampie-jfinal.zip

    可直接使用mvn jetty:run 使用嵌入式数据库h2,数据库可以自动验证生成初始化数据 使用方式:1.导入IDEA,VCS-&gt;Checkout from Version Control-&gt;Github(如果没有在plugin里下载或启用) 然后输入项目地址 ...

    jacklyn-app:Jacklyn入门应用程序

    嵌入式Jetty服务器用于处理所有HTTP请求。 使用内存中的HSQLDB作为应用程序数据库,默认情况下将端口7080和7081用于HTTP。 您可以在... \ WEB-INF \ conf \ unify.xml中更改数据库和端口设置。建立项目mvn clean ...

    spring-no-xml

    这是一个演示没有xml的spring web应用程序的项目。 在目标目录中创建了一个战争。 与 dropwizard 一样,Jax-rs 注释用于 mvc 框架。 在resources/app.properties有一个属性占位符只是为了演示这一点。 有一个码头...

    padloper-base:教程首页项目

    Jetty App Server(使用嵌入式模式) 阿贾克斯 HTML Gson 库 Maven 我还使用了由创建的json2.js文件将 JSON 字符串转换为 JavaScript 对象。 ####怎么跑? 所以,如果你有兴趣运行这个应用程序,你需要的一切: ...

    springds:基本 spring 应用程序(可能称为 devstack)

    新项目的 Spring devstack 基于 Spring 4、Gradle 和 Java 8 构建。 与嵌入式Jetty 9服务器捆绑为jar以便简单启动构建和启动简单如 gradle build && java -jar app/build/lib/app-*.jar

Global site tag (gtag.js) - Google Analytics