Jetty 是一个 Web server/servlet container, 支持 SPDY,WebSocket, OSGi, JMX,JNDI, JAAS 。Jetty非常高效而且灵活,Google App Engine 选择了Jetty,而放弃了Tomcat,或是其他的服务器。

Jetty has a slogan, "Don't deploy your application in Jetty, deploy Jetty in your application." What this means is that, putting an HTTP module into your application, rather than putting your application into an HTTP server.

Jetty的口号是:“不要把你的程序部署到Jetty里,而是把Jetty部署到你的程序里”,意味着,你可以把Jetty当成程序的一个HTTP模块放到你的程序里。

本文先通过一个简单的HelloWorld示例,展示了java应用中的Jetty是如何启动的;接着详细分析了Jetty的整体架构;最后展示了用Jetty启动一个标准的Java web app。

Hello World 示例

需要的jar包:

jetty-server-8.1.11.v20130520.jar
javax.servlet-3.0.0.v201112011016.jar
jetty-continuation-8.1.11.v20130520.jar
jetty-http-8.1.11.v20130520.jar
jetty-io-8.1.11.v20130520.jar
jetty-util-8.1.11.v20130520.jar

HelloWorldHandler 类:

packageedu.shao.jetty.sample;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.eclipse.jetty.server.Request;importorg.eclipse.jetty.server.handler.AbstractHandler;public class HelloWorldHandler extendsAbstractHandler {public voidhandle(String target, Request baseRequest,HttpServletRequest request, HttpServletResponse response)throwsIOException, ServletException {response.setContentType("text/html;charset=utf-8");response.setStatus(HttpServletResponse.SC_OK);baseRequest.setHandled(true);response.getWriter().println("<h1>Hello World</h1>");}
}

MyServer 类:

packageedu.shao.jetty.sample;importorg.eclipse.jetty.server.Server;public classMyServer {public static void main(String[] args) throwsException {Server server= new Server(8081); server.setHandler(newHelloWorldHandler()); server.start(); server.join();}
}

运行main()函数,在浏览器内输入:http://localhost:8081/ 就可以看得结果。

Jetty架构

1、整体架构图:

The Jetty Server is the plumbing between a collection of Connectors that accept HTTP connections, and a collection of Handlers that service requests from the connections and produce responses, with the work being done by threads taken from a thread pool.(The concept of a Servlet itself is implemented by a Servlet Handler.  you can build a Jetty server using only connectors and handlers, without using Servlets.)

2、顶层类结构:

受JSR77规范的启发,Jetty的绝大多数的组件(Connector, Handler ,Buffer)都实现了LifeCycle接口。

3、Connectors:

The connectors represent the protocol handlers that accept connections, parse requests and generate responses. The different types of connectors available are based on the protocols, scheduling model and IO APIs used:

  1、SocketConnector - for few busy connections or when NIO is not available

  2、BlockingChannelConnector - for few busy connections when NIO is available

  3、SelectChannelConnector - for many mostly idle connections or asynchronous handling of Ajax requests

  4、SslSocketConnector - SSL without NIO

  5、SslSelectChannelConnector - SSL with non blocking NIO support

  6、AJPConnector - AJP protocol support for connections from apache mod_jk or mod_proxy_ajp

4、Handlers:

  

The Handler is the component that deals with received requests. Three styles of Handler:

  1、Coordinating Handlers - Handlers that route requests to other handlers (eg HandlerCollection, ContextHandlerCollection)
  2、Filtering Handlers - Handlers that augment a request and pass it on to other handlers (eg. HandlerWrapper, ContextHandler, SessionHandler)
  3、Generating Handlers - Handlers that produce content (eg ResourceHandler and ServletHandler)

重点Handler:

  1、The ServletHandler is a Handler that generates content by passing the request to any configured Filters and then to a Servlet mapped by a URI pattern.

  2、A WebAppContext combines handlers for security, session and servlets in a single unit that can be configured with a web.xml descriptor.

你可以顺序调用Handler,或者嵌套调用Handler,来处理请求的不同方面。

  

5、web应用

A WebAppContext supports the standardized layout of a web application and configuration of session, security, listeners, filter, servlets and JSP via a web.xml descriptor normally found in the WEB-INF directory of a webapplication.

把Jetty“部署”到Web应用中

1、开发时的部署示例:

这种部署方式还有一个诱人的特性:项目启动后,如果某个类没有被加载到内存中,对这个类的修改在下次该类被调用时就会生效,而不用重启动项目;对JSP的修改,任何时候都会在下次被调用时生效,而不用重启项目。这将给开发web应用带来极大的便利。

1、这是用Maven构件的Java Web App项目,项目结构如下:

  

2、WebappStart 类:

importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.webapp.WebAppContext;public classWebappStart {public static void main(String[] args) throwsException {Server server= new Server(8082);WebAppContext context= newWebAppContext();context.setResourceBase("./src/main/webapp");context.setDescriptor("./src/main/webapp/WEB-INF/web.xml");context.setContextPath("/test2");context.setParentLoaderPriority(true);server.setHandler(context);server.start();server.join();}}

WebappStart类是整个项目的入口,运行此类,整个web项目就启动了。

我们可以体验到,把Jetty嵌入到Web项目中,作为Web Server,十分便利、灵活,并且相比其他服务器软件要高效,是开发Web应用的首选WebServer。

2、用Jetty部署war包

我们在此示例中部署上面web工程的war包:TestWebApp2.war

1、文件目录的组织形式如下:

2、StartWar.java

importjava.io.File;importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.webapp.WebAppContext;public classStartWar {public static void main(String[] args) throwsException {String warPath= "E:/jetty/myapp/TestWebApp2.war";//war包的绝对地址String tempDir=new File(warPath).getParent();//war包所在的目录,我们使用此目录为临时目录
Server server= new Server(8083);WebAppContext context= new WebAppContext();//构造Context Handler
context.setWar(warPath);context.setTempDirectory(newFile(tempDir));context.setContextPath("/test");server.setHandler(context);server.start();server.join();}
}

3、start.bat

前面是设置必要的变量,最后两行分别是编译StartWar.java类、运行StarWar.class。

@echo off
set directory=E:\jetty\
set java_dir=D:\Java\jdk1.6.0_35\
set classp=.;%java_dir%lib\dt.jar;%java_dir%lib\tools.jar;%directory%lib\*;%java_dir%bin\javac -classpath %classp% StartWar.java
%java_dir%bin\java -classpath %classp% StartWar

4、此时控制台会显示如下信息

此时已经部署完了,就是这么简单

参考:

http://wiki.eclipse.org/Jetty/Tutorial

http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty

转载于:https://www.cnblogs.com/windlaughing/archive/2013/06/07/3125358.html

Jetty架构解析及应用示例相关推荐

  1. jetty架构及工作原理

    1. jetty 是什么 jetty是一个轻量级的servelt容器,是一个提供HHTP服务器.HTTP客户端和javax.servlet容器的开源项目. 2. 和tomcat比较 2.1 架构层面 ...

  2. [强烈推荐] 新手入门:目前为止最透彻的的Netty高性能原理和框架架构解析

    新手入门:目前为止最透彻的的Netty高性能原理和框架架构解析 1.引言 Netty 是一个广受欢迎的异步事件驱动的Java开源网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端. 本文基 ...

  3. 从程序员到CTO的Java技术路线图 JAVA职业规划 JAVA职业发展路线图 系统后台框架图、前端工程师技能图 B2C电子商务基础系统架构解析...

    http://zz563143188.iteye.com/blog/1877266在技术方面无论我们怎么学习,总感觉需要提升自已不知道自己处于什么水平了.但如果有清晰的指示图供参考还是非常不错的,这样 ...

  4. java职业发展路线图_从程序员到CTO的Java技术路线图 JAVA职业规划 JAVA职业发展路线图 系统后台框架图、前端工程师技能图 B2C电子商务基础系统架构解析...

    http://zz563143188.iteye.com/blog/1877266在技术方面无论我们怎么学习,总感觉需要提升自已不知道自己处于什么水平了.但如果有清晰的指示图供参考还是非常不错的,这样 ...

  5. NVIDIA VPI架构解析

    VPI 架构解析 文章目录 VPI 架构解析 概述 支持的平台 算法 算法负载 无负载算法 后端 CPU CUDA PVA VIC NVENC OFA 流 缓冲器 Images 图像视图 锁 图像格式 ...

  6. 万达网络科技的DevOps平台架构解析

    转载本文需注明出处:微信公众号EAWorld,违者必究. 目录: 一.万达DevOps平台建设历程 二.平台架构解析 三.建设过程中的难点分享 四.总结 一.万达DevOps平台建设历程 我们从201 ...

  7. Netty框架架构解析+API+运行流程+网络编程文章集锦

    新手入门:目前为止最透彻的的Netty高性能原理和框架架构解析 <!-- 作者区域 --><div class="author"><a class=& ...

  8. 推荐我的新书《深入理解Nginx:模块开发与架构解析》

    http://www.china-pub.com/STATIC/zt_mb/zt_huodong_2013_3.asp?filename=2013_jsj_nginx_20130401 目录 < ...

  9. 特斯拉Tesla Model 3整体架构解析(上)

    特斯拉Tesla Model 3整体架构解析(上) 一辆特斯拉 Model 3型车在硬件改造后解体 Sensors for ADAS applications 特斯拉 Model 3型设计的传感器组件 ...

最新文章

  1. 项目管理:多项目同时进行如何做好进度管理?
  2. Boosting和Bagging: 如何开发一个鲁棒的机器学习算法
  3. C语言中的extern关键字用法
  4. Android——怎么在一个 Activity 中销毁另外一个 Activity
  5. 计算机安全的最后一道防线,汪文勇:灾备,数据安全的最后一道防线
  6. ABAP 740里的新语法 - LET表达式
  7. java 读取控制台_Java从控制台读入数据的几种方法总结
  8. 漫步数学分析二十九——幂级数
  9. 计算机科学竞赛加拿大,滑铁卢大学计算机科学与数学竞赛最新考试时间!
  10. mysql 上级组织参数值_MYSQL组织结构设计构思(快速查上级和下级)
  11. C# 设置Windows程序窗口为穿透状态
  12. php函数获取数据库中的表格,初步了解PHP获取数据库表信息函数_PHP教程
  13. 自动驾驶 11-4: 优化状态估计 Optimizing State Estimation
  14. git error Missing tree 解决办法
  15. 【git学习】fatal: unable to access XXX: Failed to connect to github.com port 443: Timed out怎么解决
  16. jdbc mysql url写法_详解数据库连接的URL的写法及总结
  17. 【国产替代】推动安全芯片产业国产化,华秋电子与华翼微达成合作!
  18. oracle 连接组件,[2021] node连接oracle数据库示例[使用oracle官方组件]
  19. Mac电脑怎么关闭键盘的重复按键功能?
  20. 科密指纹考勤机B329采集

热门文章

  1. c语言阶乘分解成素因子,阶乘的素因子分解 51nod 1189
  2. python 连续比较_For循环比较python中以前的值
  3. linux非守护线程一直不释放,Linux pthread 和 java thread 的是 / 非守护线程的行为
  4. 与华为交换机用access_学校机房项目交换机的配置,理解这篇,交换机配置不再难...
  5. 开关造成的毛刺_模具清洗机干冰清洗机干冰去毛刺机安全注意事项
  6. python逻辑运算符or的短路求值特性_[Python]计算闰年时候出现的and和or优先级的问题以及短路逻辑...
  7. mpi tcp连接报错_关于WinCC与真实PLC之间的TCP/IP连接问题-工业支持中心-西门子中国...
  8. python如何控制浏览器_控制使用Python浏览器?
  9. Present算法verilog设计实现
  10. frontpage编辑html,怎样用FrontPage软件编辑HTML帖子 | 音画代码学堂 - 中国音画家园 - Po...****...