Filter責任鏈模式

2022-07-29 21:03:13 字數 1770 閱讀 9431

filter責任鏈的建立

public

static

//傳入引數必須有乙個servlet,這個servlet是在鏈的末尾被呼叫的. 這個servlet的查詢過程是根據request的uri從web.xml配置中匹配出來的

//if there is no servlet to execute, return null

if (servlet == null

)

return

null

;

//create and initialize a filter chain object

;

if (request instanceof

request)

else

// 責任鏈上繫結乙個servlet

filterchain.setservlet(servlet);

// filtermap filtermaps =context.findfiltermaps();

//if ((filtermaps == null) || (filtermaps.length == 0))

return

(filterchain);

//...

//根據web.xml中filter的配置查詢是否和當前request匹配

//for (int i = 0; i < filtermaps.length; i++)

if (!matchfiltersurl(filtermaps[i], requestpath))

continue

; context.findfilterconfig(filtermaps[i].getfiltername());

if (filterconfig == null

) // 和request uri匹配,則filter會加入到責任鏈中

filterchain.addfilter(filterconfig);

}/.....

//return the completed filter chain

return

filterchain;

}

1

private

void

internaldofilter(servletrequest request,

2servletresponse response)

3throws

ioexception, servletexception catch (ioexception | servletexception |runtimeexception e) catch

(throwable e)

20return;21

}2223//

we fell off the end of the chain -- call the servlet instance

24try

29//

... 在鏈的末尾,呼叫具體servlet的doservice

30servlet.service(request, response);

31//

32 } catch (ioexception | servletexception |runtimeexception e) catch

(throwable e) finally

39 }

責任鏈模式

責任鏈模式 chain of responsibility 的目標是使多個物件都有機會處理請求,從而避免請求的傳送者和接收者之間的耦合關係。將這些物件連成一條鏈,並沿著這條鏈傳遞請求,直到有乙個物件處理它為止。舉個例子。你到銀行去辦理業務,只是簡單的存個錢,也許atm就解決你的問題。如果你是為了交費...

責任鏈模式

有3個request與3個handler,每個request由相應的handler來處理,當乙個handler與request不匹配時則傳遞給下乙個handler來處理 inte ce request class request1 implements request class request2 ...

責任鏈模式

了解了一下責任鏈模式,簡單的說下自己的理解吧 責任鏈就是說待處理的資料在request的時候,先經過filter1的處理,再經過filter2的處理,然後response時,先經過filter2的處理,在經過filter1的處理。實現思路是filterchain包括filter1和filter2,同...