1. 程式人生 > >Tomcat處理Http請求和Pipeline機制

Tomcat處理Http請求和Pipeline機制

tomcat對Http處理:

這裡寫圖片描述

請求被髮送到本機埠8080,被在那裡偵聽的Coyote HTTP/1.1 Connector獲得
Connector把該請求交給它所在的Service的Engine來處理,並等待Engine的迴應
Engine獲得請求localhost:8080/test/index.jsp,匹配它所有虛擬主機Host
Engine匹配到名為localhost的Host(即使匹配不到也把請求交給該Host處理,因為該Host被定義為該Engine的預設主機)
localhost Host獲得請求/test/index.jsp,匹配它所擁有的所有Context
Host匹配到路徑為/test的Context(如果匹配不到就把該請求交給路徑名為”“的Context去處理)
path=”/test”的Context獲得請求/index.jsp,在它的mapping table中尋找對應的servlet
Context匹配到URL PATTERN為*.jsp的servlet,對應於JspServlet類
構造HttpServletRequest物件和HttpServletResponse物件,作為引數呼叫JspServlet的doGet或doPost方法
Context把執行完了之後的HttpServletResponse物件返回給Host
Host把HttpServletResponse物件返回給Engine
Engine把HttpServletResponse物件返回給Connector
Connector把HttpServletResponse物件返回給客戶browser

pipeline 實現:

    @Override
    public void addValve(Valve valve) {

        // Validate that we can add this Valve
        if (valve instanceof Contained)
            ((Contained) valve).setContainer(this.container);

        // Start the new component if necessary
        if (getState().isAvailable()) {
            if
(valve instanceof Lifecycle) { try { ((Lifecycle) valve).start(); } catch (LifecycleException e) { log.error("StandardPipeline.addValve: start: ", e); } } } // basic永遠最後,表示沒有nextValue()
if (first == null) { first = valve; valve.setNext(basic); } else { Valve current = first; while (current != null) { if (current.getNext() == basic) { current.setNext(valve); valve.setNext(basic); break; } current = current.getNext(); } } container.fireContainerEvent(Container.ADD_VALVE_EVENT, valve); }