Jetty embedding Server 基本概念

1.Server对象
Jetty HTTP Servlet   Server.  这个类是Jetty HTTP Servlet服务器的主类。 它聚集连接器 Connectors  (HTTP请求接收者)和请求处理程序。 server  和ThreadPool本身就是一个处理程序。 连接器使用ThreadPool的方法运行Jobs,回调 Handle 方法。
Server对象继承了HandlerWrapper类
2. HandlerWrapper 对象
HandlerWrapper对象作为一个处理程序Handler,将其handle方法和生命周期中的各种事件(life cyle events)委托给其他处理程序Handler,这主要是为了实现装饰模式(Decorator pattern)
3.Connector接口
一个Connector主要负责接收网络连接和从远程端点发送的数据。并且允许应用程序通过设定处理这些任务的处理程序向远程端发送数据。
其主要实现类有 ServerConnector ,AbstractConnector
(原文:A  Connector  accept connections and data from remote peers, and allows applications to send data to remote peers, by setting up the machinery needed to handle such tasks.)
4. Handler 接口
一个Handler实例需要一个Server服务器来处理传入的HTTP请求。一个Handle可以有以下几种行为
(1)完全生成HTTP响应
(2)检查/修改请求并调用另一个处理程序,这里主要是运行HandlerWrapper和装饰模式
(3)将请求传递给一个或多个其他处理程序。这里主要是指HandlerCollection
Handler接收传递过来的Servelt API的request和response,但是Handler本身不是Servlet容器。   The servlet container is implemented by handlers for context, security, session and servlet that modify the request object before passing it to the next stage of handling.
Handle接口的一个主要方法是:
void  handle ( String  target,  Request  baseRequest,  HttpServletRequest  request,  HttpServletResponse  response)
这里的Request对象继承了HttpServletRequest对象
该对象主要是扩展的Setters方法,方便Handle来改变request,服务器接收的每一个链接都会创建一个request对象
5. ScopedHandler 对象
该对象继承自HandlerWrapper,该对象其实是定义在每一个作用域内的HandlerWrapper
当在HandlerWrapper作用链中的第一个ScopeHandler调用了方法 handle(String, Request, HttpServletRequest, HttpServletResponse)

For example if Scoped handlers A, B & C were chained together, then the calling order would be:

A.handle(...)
   A.doScope(...)
     B.doScope(...)
       C.doScope(...)
         A.doHandle(...)
           B.doHandle(...)
              C.doHandle(...)


6.  HandlerContainer 接口
该对象是一个Handler容器,可以包含多个Handler实例,该类提供了可以获取其内部存储的Handler实例的方法。实现该接口的主要有以下几个类:
Holds a collection of other handlers and calls each handler in order. This is useful for combining statistics and logging handlers with the handler that generates the response.
A Handler Collection that calls each handler in turn until either an exception is thrown, the response is committed or the request.isHandled() returns true. You can use it to combine handlers that conditionally handle a request, such as calling multiple contexts until one matches a virtual host.
A Handler base class that you can use to daisy chain handlers together in the style of aspect-oriented programming. For example, a standard web application is implemented by a chain of a context, session, security and servlet handlers.
(4) ContextHandlerCollection
A specialized HandlerCollection that uses the longest prefix of the request URI (the contextPath) to select a contained ContextHandler to handle the request.
Scoped Handlers

Much of the standard Servlet container in Jetty is implemented with HandlerWrappers that daisy chain handlers together: ContextHandler to SessionHandler to SecurityHandler to ServletHandler. However, because of the nature of the servlet specification, this chaining cannot be a pure nesting of handlers as the outer handlers sometimes need information that the inner handlers process. For example, when a ContextHandler calls some application listeners to inform them of a request entering the context, it must already know which servlet the ServletHandler will dispatch the request to so that the servletPath method returns the correct value.

The HandlerWrapper is specialized to the ScopedHandler abstract class, which supports a daisy chain of scopes. For example if a ServletHandler is nested withing a ContextHandler, the order and nesting of execution of methods is:

Server.handle(...)
  ContextHandler.doScope(...)
    ServletHandler.doScope(...)
      ContextHandler.doHandle(...)
        ServletHandler.doHandle(...)
          SomeServlet.service(...)

Thus when the ContextHandler handles the request, it does so within the scope the ServletHandler has established.










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值