2008年1月28日 星期一

Jetty 6 handler 階層

1.root 是server

server -> HandlerCollection -> ContextHandlerCollection (pathmap) -> ContextHandler
而如果ContextHandler是屬於WebAppContext的話。其下包含了一連串的handler
WebAppContext -> SessionHandler -> SecurityHandler -> ServletHandler

原文:

  1. The Server delegates the request to the its configured handler, which is normally an instance of HandlerCollection.
    Server分派request給屬於server的handler,這常常是一個HandlerCollection的實例。

  2. The HandlerCollection acts like Jetty 5, and calls each of it's contained handlers in turn. Typically it is configured with
    a ContextHandlerCollection, a DefaultHandler and a RequestLogHandler. This allows a request to be handled by a context or the default handler, and then be passed to the request log handler.
    HandlerCollection會依序呼叫其下的每一個Handler,慣例上,其下會有ContextHandlerCollection,DefaultHandler,RequestHandler.

  3. The ContextHandlerCollection
    maintains a map of context path to lists of ContextHandlers. For a given request, each the URI is used to find matching context paths, and each is called in turn until the request is handled.
    ContextHandlerCollection維護一個path的map,根據request的URI mapping到其下對應的ContextHandler

  4. If the ContextHandler accepts a request (it may reject it due to virtual hosts), it will delegate the request to a nested handler and for the duration of that call it will set:
    • the request context path
    • the current thread context classloader
    • the resource base
    • the error handler
    • context attributes and init parameters.
  5. Typically the ContextHandler will be an instance of WebAppContext and will thus contain a nested chain of SessionHandler, SecurityHandler and ServletHandler. The request is delegated to the first handler in this chain, a SessionHandler
  6. The SessionHandler will examine the request for any session ID to be activated and will activate the mechanism for creating new sessions before delegating the request to the SecurityHandler.
  7. The SecurityHandler will check any constraints and authentication before delegating the request to the ServletHandler
  8. The ServletHandler implements the dispatch to Filters and Servlets to handle the request according to the servlet specification.
簡單範例程式
public static void main(String[] args) {
Server service = new Server();
// connector
Connector selconnector = new SelectChannelConnector();
selconnector.setPort(80);
service.setConnectors(new Connector[] { selconnector });
// thread pool
BoundedThreadPool pool = new BoundedThreadPool();
pool.setMinThreads(10);
pool.setMaxThreads(100);
service.setThreadPool(pool);
// handler collection
HandlerList handlerlist = new HandlerList();
service.setHandlers(new Handler[] { handlerlist, new DefaultHandler(), new RequestLogHandler() });
// static resource handler
ContextHandler resContext = new ContextHandler(handlerlist, "/html");
ResourceHandler reshandler = new ResourceHandler();
reshandler.setResourceBase("./html/");
resContext.setHandler(reshandler);
// dynamic resource handler
WebAppContext webappcontext = new WebAppContext(handlerlist, "./webapp/", "/webapp");

try {
service.join();
service.start();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

沒有留言: