一、Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境。Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布。开发人员可以将Jetty容器实例化成一个对象,可以迅速为一些独立运行(stand-alone)的Java应用提供网络和web连接。

  二、嵌入式的开发,可以将容器直接嵌入到代码中,这样启动容器,就不需要单独使用中间件了,比如:tomcat等

  三、这里介绍两种jetty的启动方式,1:代码直接启动,2:spring的ClassPathXmlApplicationContext启动(推荐后一种启动方式)

  四、具体的例子:

  1、需要的依赖包

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.pinnet</groupId>
<artifactId>spring-jetty</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.9.v20150224</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.2.9.v20150224</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>9.2.9.v20150224</version>
</dependency>
</dependencies>
</project>

  2、web.xml的配置,基本上没啥变化

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
</web-app>

  3、启动方式:

  1)代码手写启动

package com.pinnet.jetty;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext; public class JettyStart { public static void main(String[] args) throws Exception {
//设置端口,可以设置多个端口
Server server = new Server();
//web应用参数设置
WebAppContext context = new WebAppContext();
//项目上下文
context.setContextPath("/");
//web.xml路径,主要是解析其中的配置
context.setDescriptor("./src/main/webapp/web.xml");
//静态资源路径
context.setResourceBase("./src/main/webapp");
context.setParentLoaderPriority(true);
//展示URL日志
context.setLogUrlOnStart(true);
server.setHandler(context);
server.start();
}
}

  2)容器启动:

package com.pinnet.jetty;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppStart {

    public static void main(String[] args) {
new ClassPathXmlApplicationContext("spring-jetty.xml");
}
}

  配置:

<?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.xsd"> <bean id="webAppContext" class="org.eclipse.jetty.webapp.WebAppContext">
<property name="contextPath" value="/"/>
<property name="descriptor" value=".\src\main\webapp\web.xml"/>
<property name="resourceBase" value=".\src\main\webapp\"/>
<property name="parentLoaderPriority" value="true"/>
<property name="logUrlOnStart" value="true"/>
</bean> <bean id="server" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop">
<property name="connectors">
<list>
<bean class="org.eclipse.jetty.server.ServerConnector">
<constructor-arg ref="server"/>
<property name="port" value="8080"/>
</bean>
</list>
</property>
<property name="handler" ref="webAppContext"/>
</bean>
</beans>

  具体:解释如代码启动一样!

jetty之嵌入式开发的更多相关文章

  1. Jetty 9嵌入式开发

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

  2. jetty 9 嵌入式开发示例

    jetty 9 嵌入应用程序后,小型的web应用直接打成一个单独的jar包,就可以直接运行,非常适合做Demo演示或云端集群部署. 主要代码: JettyServer的封装类 package yjmy ...

  3. Java使用Jetty实现嵌入式Web服务器及Servlet容器

     Jetty是一个Java实现的开源的servlet容器,它既可以像Tomcat一样作为一个完整的Web服务器和Servlet容器,同时也可以嵌入在Java应用程序中,在Java程序中调用Jetty. ...

  4. 嵌入式开发中常见3个的C语言技巧

    Hey,大家好!我是CrazyCatJack.今天我来说几个在嵌入式开发中常用的C语言技巧吧.也许你曾经用过,也许你只是见到过但是没有深入理解.那么今天好好补充下吧^_^ 1.指向函数的指针 指针不光 ...

  5. [嵌入式开发]Linux性能分析——上下文切换

    一.从一个问题说起 相信很多人在玩手机还是PC时,都曾碰到过这样一种情况,安装的软件多了系统性能就变慢了,但是去查看CPU利用率一直都低于10%,内存也很充足.我在近期的开发工作中就碰到了类似的情况, ...

  6. ARM嵌入式开发板

    iTOP-4412 ARM嵌入式开发板----主要特点 iTOP-4412开发平台是北京迅为电子研发设计的嵌入式开发板平台,核心板配备64位双通道2GB DDR3,16GBEMMC存储,三星原厂S5M ...

  7. 构建 ARM Linux 4.7.3 嵌入式开发环境 —— U-BOOT 引导 Kernel

    经过若干天的反复测试,搜索.终于成功利用 Qemu 在 u-boot 下引导 ARM Linux 4.7.3 内核.如下详细解释整个构建过程. 准备环境 运行环境:Ubuntu 16.04 需要的虚拟 ...

  8. 嵌入式开发平台-iTOP-4418开发板

    详情转自:http://topeetboard.com S5P4418核心板可以无缝支持核心系统S5P6818,并保持底板设计不变,将兼顾更高端 的应用领域,为项目和产品提供更好的灵活性以及可伸缩性. ...

  9. zju(1)嵌入式开发环境构建

    1. 实验目的 搭建嵌入式开发环境,安装ubntu,编译交叉工具链,安装配置tftp,nfs.用makefile 编译几个文件,在实验台上运行. 2. 实验内容 1) 安装ubuntu12.04 2) ...

随机推荐

  1. IDEA 搭建授权服务器

    引用地址  http://blog.lanyus.com/archives/317.html 今天突出发现基本公开的激活地址都被屏蔽了(IDEA 2017.3 版本) 记录一下 docker pull ...

  2. git看不到别人创建的远程分支

    解决办法: $ git fetch //取回所有分支(branch)的更新.如果只想取回特定分支的更新,可以指定分支名,例:$ git fetch <远程主机名> <分支名> ...

  3. 《图解Http》 2-6章: 基础,报文,状态码,首部。

    HTTP协议和Cookie 是stateless协议,自身不对请求和响应之间的通信状态进行保存.但随着技术发展,为了实现保存状态的功能,引入了Cookie技术. Cookie在请求和响应报文中写入信息 ...

  4. git 重写历史

    重写最后一次提交的commit git commit --amend 修改多个历史 git rebase -i HEAD~3 命令执行后结果如下: pick f7f3f6d changed my na ...

  5. hdu1564博弈+找规律

    #include<map> #include<set> #include<cmath> #include<queue> #include<stac ...

  6. Java实现ping功能的三种方法及Linux的区分

    前大半部份转自:https://blog.csdn.net/futudeniaodan/article/details/52317650 检测设备的运行状态,有的是使用ping的方式来检测的.所以需要 ...

  7. windows下的IO模型之选择(select)模型

    1.选择(select)模型:选择模型:通过一个fd_set集合管理套接字,在满足套接字需求后,通知套接字.让套接字进行工作. 选择模型的核心是FD_SET集合和select函数.通过该函数,我们可以 ...

  8. Hibernate主键生成器

    主键生成器负责生成数据表记录的主键:increment:为long,short或者int类型主键生成唯一标识.只有在没有其他进程往同一张表中插入数据时才能使用.在集群下不能使用! identity:在 ...

  9. 97. Interleaving String *HARD* -- 判断s3是否为s1和s2交叉得到的字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  10. openfalcon源码分析之graph

    openfalcon源码分析之graph 本节内容 graph功能 graph源码分析 2.1 graph中重要的数据结构 2.2 graph的简要流程图 2.3 graph处理数据过程 2.4 gr ...