我尝试了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 嵌入式开发(实例)的更多相关文章

  1. jersery+jetty嵌入式restful的框架开发

    随着微服务的流程,越来越多的后台服务采用了restful api风格的开放API,jersery+jetty嵌入式变成了一个很好的选择, 我自己写了一个简单的框架,https://github.com ...

  2. 【推荐图书】+ 基于Nios II的嵌入式SoPC系统设计与Verilog开发实例+C#入门经典等

    [推荐图书]+ 基于Nios II的嵌入式SoPC系统设计与Verilog开发实例+C#入门经典等 3赞 发表于 2016/7/4 21:14:12 阅读(1921) 评论(3) 初次接触FPGA,到 ...

  3. Java Native Interface 基于JNI的嵌入式手机软件开发实例

    1.通过JNI和c/c++的库组件.其他代码交互 2.java和c不能互通的原因时数据类型问题 Introduction https://docs.oracle.com/javase/8/docs/t ...

  4. 学习嵌入式开发板的Android平台体系结构和源码结构

    本文转自迅为论坛资料:http://www.topeetboard.com 推荐学习嵌入式开发板平台:iTOP-4412开发板 下面这张图出自Google官方,展示了Android系统的主要组成部分. ...

  5. Android嵌入式开发初学者的几个注意点

    一:首先你必须了解ARM平台 Android 移植与驱动核心开发,当然也可以是X86和其他的平台,不过其他平台的Android智能终端开发并不是很多. Android嵌入式智能操作系统是基于Linux ...

  6. 嵌入式开发—C语言面试题

    嵌入式开发—C语言面试题 源地址:http://blog.csdn.net/xdx2ct1314/article/details/7358929   1. 用预处理指令#define 声明一个常数,用 ...

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

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

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

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

  9. ARM嵌入式开发板

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

随机推荐

  1. Linux 基本收集

    ifconfig eth0 192.168.1.223 切换到root账号开始是$符号输入su输入root密码转换成# 就变成了root账号 dr 查看盘符ls /etc/ 查看etc文件夹下面的文件 ...

  2. 第二篇 SQL Server安全验证

    本篇文章是SQL Server安全系列的第二篇,详细内容请参考原文. 验证是检验主体的过程.主体需要唯一标识,那样SQL Server可以确定主体有哪些权限.正确的验证是提供安全访问数据库对象的必要的 ...

  3. 将hadoop源代码导入eclipse

    1. 安装JDK,eclipse,下载hadoop源代码并解压到某一个目录. 2. 安装maven,将bin目录添加到PATH环境变量中. 3. 安装protobuf2.5.0,将protoc-2.5 ...

  4. 使用xib封装一个自定义view的步骤

    使用xib封装一个自定义view的步骤 1> 新建一个继承UIView的自定义view,假设类名叫做(MJAppView) 2> 新建一个MJAppView.xib文件来描述MJAppVi ...

  5. Java基础之处理事件——使用动作Action(Sketcher 6 using Action objects)

    控制台程序. 动作Action是任何实现了javax.swing.Action接口的类的对象.这个接口声明了操作Action对象的方法,例如,存储与动作相关的属性.启用和禁用动作.Action接口扩展 ...

  6. IntelliJ IDEA 常用设置讲解3

    IntelliJ IDEA 有很多人性化的设置我们必须单独拿出来讲解,也因为这些人性化的设置让我们这些 IntelliJ IDEA 死忠粉更加死心塌地使用它和分享它. 常用设置 如上图 Gif 所示, ...

  7. TPageControl组件

    TPageControl的功能是创建多个Dialog页,而这些重叠的每一个页Dialog就是通过TTabSheet对象实现的

  8. 利用并查集求最大生成树和最小生成树(nlogn)

    hdu1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. 20145207 《Java程序设计》第10周学习总结

    前言:   最后一篇java博客好激动啊..不过猜猜我在干什么?没错,安虚拟机,唉!紧接着又是一大波信安系统的博客,真开心~好啦边敲博客,边装虚拟机. 教材知识汇总 13.1 网络概述 13.1.1计 ...

  10. hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...