一.maven依赖

  1. pom配置

    <dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-server</artifactId>
    <version>9.2.11.v20150529</version>
    </dependency>
    <dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-webapp</artifactId>
    <version>9.2.11.v20150529</version>
    </dependency>
    <dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-annotations</artifactId>
    <version>9.2.11.v20150529</version>
    </dependency>
    <!-- Jetty Dependencies end here -->
    <!--Jetty Apache JSP dependency -->
    <dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>apache-jsp</artifactId>
    <version>9.2.11.v20150529</version>
    </dependency>
    <!-- JSTL Dependency -->
    <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    </dependency>

二. jetty加载spring,并在jetty外面获取jetty创建的spring容器

  1. web.xml配置spring监听器
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    </welcome-file-list> <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring-context.xml</param-value>
    </context-param> <!--Spring的ApplicationContext 载入 -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> <!--jerssy servlet-->
    <servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.example</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/jerssyTest/*</url-pattern>
    </servlet-mapping>
    </web-app>
  2. spring-contxt.xml注入带ApplicationContext的bean
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
    default-lazy-init="true"> <!--web容器中注入applicatiobContext,使得获取的ctx使web容器中的ctx-->
    <bean id="springcontextHolder" class="server.com.SpringContextHolder" lazy-init="false"/>
    </beans>
  3. SpringContextHolder类
    public class SpringContextHolder implements ApplicationContextAware{
    
        private static ApplicationContext applicationContext;
    public static ApplicationContext getApplicationContext() {
    return applicationContext;
    } public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
    SpringContextHolder.applicationContext = applicationContext;
    }
    }

三. 启动jetty

  1. startup类
    public class Startup {
    public static void main(String[] args) throws Exception {
    Server server = new Server(8080); WebAppContext context = new WebAppContext();
    context.setContextPath("/test");
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    context.setDescriptor(loader.getResource("webroot/WEB-INF/web.xml").getPath());
    context.setResourceBase(loader.getResource("webroot").getPath());
    context.setParentLoaderPriority(true);
    server.setHandler(context); server.start(); //获取jetty创建的spring容器
    ApplicationContext ctx = SpringContextHolder.getApplicationContext();
    System.out.println(ctx);
    }
    }

嵌入式jetty的更多相关文章

  1. 嵌入式jetty的HTTP实现

    2    嵌入式jetty的HTTP实现 布拉君君 2.1 简单HTTP实现 2.1.1 HTTP SERVER端实现 2.1.1.1 HTTP SERVER端实现概述 在代码中嵌入一个Jetty s ...

  2. Jetty实战之 嵌入式Jetty运行Servlet

    http://blog.csdn.net/kongxx/article/details/7230080 Jetty实战之 嵌入式Jetty运行Servlet 分类:JettyJava (19530)  ...

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

    Jetty实战之 嵌入式Jetty运行web app 博客分类: 应用服务器 jettywar  转载地址:http://blog.csdn.net/kongxx/article/details/72 ...

  4. 使用嵌入式jetty实现文件服务器

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  5. Jetty 9嵌入式开发

    官方网址:http://www.eclipse.org/jetty/ 下载地址:http://download.eclipse.org/jetty/stable-9/dist/ 文档网址:http:/ ...

  6. 一.配置简单的嵌入式tomcat和jetty

    我们写了一个web应用,打成war包后,就需要找一个server来部署.对于我们的实际应用,我们基本没必要自己再写一个嵌入式的server.接下来两篇文章只是以钻研的心态来学习一下嵌入式tomcat和 ...

  7. Jetty使用教程(四:24-27)—Jetty开发指南

    二十四.处理器(Handler ) 24.1 编写一个常用的Handler Jetty的Handler组件用来处理接收到的请求. 很多使用者不需要编写Jetty的Handler ,而是通过使用Serv ...

  8. Jetty使用教程(四:23)—Jetty开发指南

    二十三.Maven和Jetty 这一章节将说明如何通过Maven管理Jetty和使用Jetty的Maven插件. 23.1 使用Maven Apache Maven是一个款软件项目管理工具.基于项目对 ...

  9. Jetty使用教程(四:21-22)—Jetty开发指南

    二十一.嵌入式开发 21.1 Jetty嵌入式开发HelloWorld 本章节将提供一些教程,通过Jetty API快速开发嵌入式代码 21.1.1 下载Jetty的jar包 Jetty目前已经把所有 ...

随机推荐

  1. matlab演奏《卡农》

    % Cripple Pachebel’s Canon on Matlab% Have fun fs = 44100; % sample ratedt = 1/fs; T16 = 0.125; t16 ...

  2. 关于duplicate symbol _main in的解决办法

    报错:duplicate symbol _main in: duplicate symbol _main in:    /Users/gavin/Library/Developer/Xcode/Der ...

  3. hdu1003 dp(最大子段和)

    题意:给出一列数,求其中的最大子段和以及该子段的开头和结尾位置. 因为刚学过DP没几天,所以还会这题,我开了一个 dp[100002][2],其中 dp[i][0] 记录以 i 为结尾的最大子段的和, ...

  4. 关于正则表达式处理textarea里的换行

    将textarea里的内容存入数据库时,会自动将回车换行符过滤成空格,也会将多个空格转换成一个空格,即:将\n等换成 “  ”存入数据库 因此为了将内容从数据库中按照原来格式读出写入到html 就必须 ...

  5. java定时框架:表达式设置

    Quartz中时间表达式的设置-----corn表达式 (注:这是让我看比较明白的一个博文,但是抱歉,没有找到原作者,如有侵犯,请告知) 时间格式: <!-- s m h d m w(?) y( ...

  6. 如何用 matlab 在图片上绘制矩形框 和 添加文字 ?

    如何给图像添加矩形框?以及添加想要输入的文字 ? 案例程序,如下所示: clc; close all; clear all;image = imread('/home/wangxiao/Picture ...

  7. MapReduce: 一个巨大的倒退

    前言 databasecolumn 的数据库大牛们(其中包括PostgreSQL的最初伯克利领导:Michael Stonebraker)最近写了一篇评论当前如日中天的MapReduce 技术的文章, ...

  8. MySQL Thread Pool: Problem Definition

    A new thread pool plugin is now a part of the MySQL Enterprise Edition.In this blog we will cover th ...

  9. leaflet 了解

    Leaflet 是一个为建设移动设备友好的互动地图,而开发的现代的.开源的 JavaScript 库.它是由 Vladimir Agafonkin 带领一个专业贡献者团队开发,虽然代码仅有 33 KB ...

  10. each和$(this)配合循环_siblings选取同级不同类型元素

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...