jetty之嵌入式开发
一、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之嵌入式开发的更多相关文章
- Jetty 9嵌入式开发
官方网址:http://www.eclipse.org/jetty/ 下载地址:http://download.eclipse.org/jetty/stable-9/dist/ 文档网址:http:/ ...
- jetty 9 嵌入式开发示例
jetty 9 嵌入应用程序后,小型的web应用直接打成一个单独的jar包,就可以直接运行,非常适合做Demo演示或云端集群部署. 主要代码: JettyServer的封装类 package yjmy ...
- Java使用Jetty实现嵌入式Web服务器及Servlet容器
Jetty是一个Java实现的开源的servlet容器,它既可以像Tomcat一样作为一个完整的Web服务器和Servlet容器,同时也可以嵌入在Java应用程序中,在Java程序中调用Jetty. ...
- 嵌入式开发中常见3个的C语言技巧
Hey,大家好!我是CrazyCatJack.今天我来说几个在嵌入式开发中常用的C语言技巧吧.也许你曾经用过,也许你只是见到过但是没有深入理解.那么今天好好补充下吧^_^ 1.指向函数的指针 指针不光 ...
- [嵌入式开发]Linux性能分析——上下文切换
一.从一个问题说起 相信很多人在玩手机还是PC时,都曾碰到过这样一种情况,安装的软件多了系统性能就变慢了,但是去查看CPU利用率一直都低于10%,内存也很充足.我在近期的开发工作中就碰到了类似的情况, ...
- ARM嵌入式开发板
iTOP-4412 ARM嵌入式开发板----主要特点 iTOP-4412开发平台是北京迅为电子研发设计的嵌入式开发板平台,核心板配备64位双通道2GB DDR3,16GBEMMC存储,三星原厂S5M ...
- 构建 ARM Linux 4.7.3 嵌入式开发环境 —— U-BOOT 引导 Kernel
经过若干天的反复测试,搜索.终于成功利用 Qemu 在 u-boot 下引导 ARM Linux 4.7.3 内核.如下详细解释整个构建过程. 准备环境 运行环境:Ubuntu 16.04 需要的虚拟 ...
- 嵌入式开发平台-iTOP-4418开发板
详情转自:http://topeetboard.com S5P4418核心板可以无缝支持核心系统S5P6818,并保持底板设计不变,将兼顾更高端 的应用领域,为项目和产品提供更好的灵活性以及可伸缩性. ...
- zju(1)嵌入式开发环境构建
1. 实验目的 搭建嵌入式开发环境,安装ubntu,编译交叉工具链,安装配置tftp,nfs.用makefile 编译几个文件,在实验台上运行. 2. 实验内容 1) 安装ubuntu12.04 2) ...
随机推荐
- Gym 101334F Feel Good
http://codeforces.com/gym/101334 题意:给定一串数,求一个区间,使得该区间的所有数之和乘以该区间内最小的数的乘积最大. 思路:先预处理一下,计算出前缀和. 我们可以把每 ...
- Gym - 100676H H. Capital City (边双连通分量缩点+树的直径)
https://vjudge.net/problem/Gym-100676H 题意: 给出一个n个城市,城市之间有距离为w的边,现在要选一个中心城市,使得该城市到其余城市的最大距离最短.如果有一些城市 ...
- Android -- service 利用广播调用服务的方法
1. 实现原理,在Service里面注册一个广播接收者, 想要调用的时候app发送出广播, 后台的service里面的广播接收者接收到广播,并调用service里面的方法. 2. 示例代码 MainA ...
- Java 集合-Map接口和三个子类实现
2017-10-31 22:05:59 Map 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值. HashMap是基于散列表实现的,插入.删除和定位元素时间复杂度平均能达到O ...
- 雷林鹏分享:Ruby 正则表达式
Ruby 正则表达式 正则表达式是一种特殊序列的字符,它通过使用有专门语法的模式来匹配或查找其他字符串或字符串集合. 语法 正则表达式从字面上看是一种介于斜杠之间或介于跟在 %r 后的任意分隔符之间的 ...
- linux挂载windows共享文件夹出错,提示mount error(13): Permission denied
完整的可以工作的命令行: mount -v -t cifs -o username=clouder,password=123456,iocharset=utf8,sec=ntlm //172.28.1 ...
- PrestaShop 1.7 订单生成后下载服务器出现 505 的错误
PrestaShop 生成订单后下载,服务器上有 505 的错误. 经查看应该是服务器上的错误: Allowed memory size of 134217728 bytes exhausted (t ...
- Granting and Managing Item Level Permission using SharePoint2013 Designer Workflow
https://gnanasivamgunasekaran.wordpress.com/2015/12/29/granting-and-managing-item-level-permission-u ...
- bzoj3901
题解: 就是按照常规的合并 期望有一点麻烦 首先计算全部的和 再减去有多少种 具体看看http://blog.csdn.net/PoPoQQQ/article/category/2542261这个博客 ...
- 分水岭分割算法(watershed segmentation)的C++实现(法2)
运行环境:ubuntu16.04+Qt+opencv2.4.13.3 watershed.cpp #include "opencv2/imgproc/imgproc.hpp" #i ...