關於Tomcat的Pipeline機制

2021-07-09 23:17:09 字數 4291 閱讀 6333

看了tomcat的pipeline和valve的機制。試著寫了乙個簡單的結構,記錄一下。

pipeline--包含了改容器要喚醒的所有任務。每乙個valve表示了乙個特定的任務。容器的pipeline還有乙個basic valve。valve可以自由新增乙個或者多個。

類似於servlet過濾器,乙個pipeline就像乙個過濾鏈,每乙個valve像乙個過濾器。跟過濾器一樣,乙個valve可以操作傳遞給它的request和response方法。讓乙個閥門完成了處理,則進一步處理流水線中的下乙個閥門,基本閥門總是在最後才被呼叫,但是會最先執行裡面的方法。 乙個容器可以實現pipeline介面。當容器的invoke方法被呼叫的時候,容器將會處理流水線中的閥門,並乙個接乙個的處理,直到所有的閥門都被處理完畢。

首先,這個是啟動類:

public final class bootstrap5 

catch (exception e)

}}

headerloggervalve和clientiploggervalve是兩個閥門,用來列印請求頭和使用者ip.

// the servlet instance

private servlet instance = null;

private string servletclass;

private loader loader;

private string name;

private ******pipeline pipeline = new ******pipeline(this);

protected container parent = null;

} @override

public synchronized void addvalve(valve valve)

@override

public servlet allocate() throws servletexception catch (servletexception e) catch (throwable e)

} return instance;

} private servlet loadservlet() throws servletexception

loader loader = getloader();

// acquire an instance of the class loader to be used

if (loader == null)

classloader classloader = loader.getclassloader();

class classclass = null;

try

} catch (classnotfoundexception e)

// instantiate and initialize an instance of the servlet class itself

try catch (throwable e)

// call the initialization method of this servlet

try catch (throwable f)

return servlet;

} @override

public void invoke(request request, response response) throws ioexception,

servletexception

...}這個是誰呢,這個就是下面要說的******pipeline的basic valve,它是用來呼叫真實servlet的service方法的

protected container container;

@override

public void invoke(request request, response response,

valvecontext valvecontext) throws ioexception, servletexception else

} catch (servletexception e)

} @override

public string getinfo()

@override

public container getcontainer()

@override

public void setcontainer(container container)

}

public class ******pipeline implements pipeline 

// the basic valve (if any) associated with this pipeline.

protected valve basic = null;

// the container with which this pipeline is associated.

protected container container = null;

// the array of valves

protected valve valves = new valve[0];

public void setcontainer(container container)

@override

public valve getbasic()

@override

public void setbasic(valve valve)

@override

public void addvalve(valve valve)

} @override

public valve getvalves()

@override

public void invoke(request request, response response) throws ioexception,

servletexception

@override

public void removevalve(valve valve)

// this class is copied from org.apache.catalina.core.standardpipeline

// class's

// standardpipelinevalvecontext inner class.

protected class ******pipelinevalvecontext implements valvecontext

@override

public void invokenext(request request, response response)

throws ioexception, servletexception else if ((subscript == valves.length) && (basic != null)) else

} } // end of inner class

}

下面看下兩個valve類

這樣就實現了pipeline的機制了。

**參考:

how tomcat works

Linux關於tomcat的命令

1.清理tomcat快取檔案 root localhost tomcat rm fr temp 清理tomcat內temp目錄下所有快取 root localhost tomcat rm fr work 清理tomcat內work目錄下所有快取 2.檢視tomcat執行日誌 root localho...

關於配置Tomcat的URIEncoding

遇到的問題 程式需要傳送http get請求到伺服器,請求的引數中包含了中文字元。程式中引數為utf 8格式,且經過了utf 8 url編碼再傳送。使用的tomcat伺服器,但伺服器端後台程式中取到的引數的中文是亂碼。問題原因 經過分析,應該是tomcat在解析引數的時候沒有使用正確的編碼格式 ut...

關於配置Tomcat的URIEncoding

遇到的問題 程式需要傳送http get請求到伺服器,請求的引數中包含了中文字元。程式中引數為utf 8格式,且經過了utf 8 url編碼再傳送。使用的tomcat伺服器,但伺服器端後台程式中取到的引數的中文是亂碼。問題原因 經過分析,應該是tomcat在解析引數的時候沒有使用正確的編碼格式 ut...