Jetty 嵌入式开发(实例)
我尝试了jetty几个版本,类的使用有些差异,在此记录下jettyVersion = 9.0.2.v20130417 的部分实例
maven 依赖及配置:
<properties>
<!-- Adapt this to a version found on http://central.maven.org/maven2/org/eclipse/jetty/jetty-maven-plugin/ -->
<jettyVersion>9.0.2.v20130417</jettyVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies> <!-- jetty 依赖 -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jettyVersion}</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.example.HelloWorld</mainClass>
</configuration>
</plugin>
</plugins>
</build>
以下是示例代码:
package com.bocom.testjetty.httpserver; import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerList; public class httpserver { public static void main(String[] arg0) throws Exception{
server1();
server2(); } /**
* 多个连接器实例
* @throws Exception
*/
public static void server1() throws Exception{
Server server = new Server();
ServerConnector connector1 = new ServerConnector(server);
connector1.setPort(8082);
ServerConnector connector2 = new ServerConnector(server);
connector2.setPort(9001); server.setConnectors(new Connector[] { connector1, connector2 });
server.setHandler(new HelloHandler()); server.start();
} /**
* 多个handler实例
* @throws Exception
*/
public static void server2() throws Exception { Server server = new Server(9080);
ContextHandler context1 = new ContextHandler();
context1.setContextPath("/hello");
context1.setHandler(new HelloHandler());
ContextHandler context2 = new ContextHandler();
context2.setContextPath("/content");
context2.setHandler(new HelloHandler());
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{context1,context2});
server.setHandler(handlers);
server.start();
server.join();
} }
package com.bocom.testjetty.httpserver; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler; public class HelloHandler extends AbstractHandler{ public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException { response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);//响应状态码
baseRequest.setHandled(true);
response.getWriter().println("<a>你好!</a>"); } }
参考资料:
http://www.eclipse.org/jetty/documentation/current/index.html
http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
http://blog.sina.com.cn/s/articlelist_2664952023_14_1.html
Jetty 嵌入式开发(实例)的更多相关文章
- jersery+jetty嵌入式restful的框架开发
随着微服务的流程,越来越多的后台服务采用了restful api风格的开放API,jersery+jetty嵌入式变成了一个很好的选择, 我自己写了一个简单的框架,https://github.com ...
- 【推荐图书】+ 基于Nios II的嵌入式SoPC系统设计与Verilog开发实例+C#入门经典等
[推荐图书]+ 基于Nios II的嵌入式SoPC系统设计与Verilog开发实例+C#入门经典等 3赞 发表于 2016/7/4 21:14:12 阅读(1921) 评论(3) 初次接触FPGA,到 ...
- Java Native Interface 基于JNI的嵌入式手机软件开发实例
1.通过JNI和c/c++的库组件.其他代码交互 2.java和c不能互通的原因时数据类型问题 Introduction https://docs.oracle.com/javase/8/docs/t ...
- 学习嵌入式开发板的Android平台体系结构和源码结构
本文转自迅为论坛资料:http://www.topeetboard.com 推荐学习嵌入式开发板平台:iTOP-4412开发板 下面这张图出自Google官方,展示了Android系统的主要组成部分. ...
- Android嵌入式开发初学者的几个注意点
一:首先你必须了解ARM平台 Android 移植与驱动核心开发,当然也可以是X86和其他的平台,不过其他平台的Android智能终端开发并不是很多. Android嵌入式智能操作系统是基于Linux ...
- 嵌入式开发—C语言面试题
嵌入式开发—C语言面试题 源地址:http://blog.csdn.net/xdx2ct1314/article/details/7358929 1. 用预处理指令#define 声明一个常数,用 ...
- 嵌入式开发中常见3个的C语言技巧
Hey,大家好!我是CrazyCatJack.今天我来说几个在嵌入式开发中常用的C语言技巧吧.也许你曾经用过,也许你只是见到过但是没有深入理解.那么今天好好补充下吧^_^ 1.指向函数的指针 指针不光 ...
- [嵌入式开发]Linux性能分析——上下文切换
一.从一个问题说起 相信很多人在玩手机还是PC时,都曾碰到过这样一种情况,安装的软件多了系统性能就变慢了,但是去查看CPU利用率一直都低于10%,内存也很充足.我在近期的开发工作中就碰到了类似的情况, ...
- ARM嵌入式开发板
iTOP-4412 ARM嵌入式开发板----主要特点 iTOP-4412开发平台是北京迅为电子研发设计的嵌入式开发板平台,核心板配备64位双通道2GB DDR3,16GBEMMC存储,三星原厂S5M ...
随机推荐
- system执行shell命令
system - execute a shell command #include <stdlib.h> int system (const char *command); 描述 syst ...
- [Slimdx]顶点和索引缓冲,绘制了2个分离的三角形
定义网格顶点和索引缓冲,绘制了2个分离的三角形. using System; using System.Drawing; using RGeos.SlimScene.Core; using SlimD ...
- UISegment属性
1.segmentedControlStyle 设置segment的显示样式. typedefNS_ENUM(NSInteger, UISegmentedControlStyle) { UISegme ...
- autorelease应用
// // main.m // 02-autorelease应用 // // Created by apple on 14-3-18. // Copyright (c) 2014年 apple ...
- Mybatis的批量CRUD
CRUD与批量CRUD 分页前后缀(方言 Mysql与Oracle情况下不一样) 批量插入数据 http://chenzhou123520.iteye.com/blog/1583407/ 亟待完善 ...
- java collections读书笔记(10) Set
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVgAAADbCAIAAACnXR7VAAAgAElEQVR4nOx9d1hVV9Y3880zb2YmM3 ...
- Opencv读取各种格式图片,在TBitmap上面重绘
//opencv读取图片 cv::Mat image; //const char *fileName = "HeadImage-UI/Photo-001.bmp"; const c ...
- Java基础(35):装箱与拆箱---Java 中基本类型和包装类之间的转换(Wrapper类)
基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 JDK1.5 引入自动装箱和拆箱的机制后,包装类和基本类型之间的转换就更加轻松便利了. 那什么是装箱 ...
- SPOJ 220 Relevant Phrases of Annihilation(后缀数组)
You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages ...
- the serializable class XXX does not declare a static final seriaVersionUID...的问题
关于myeclips提示The serializable class XXX does not declare a static final serialVersionUID field of typ ...