轉(zhuǎn)自:
http://topic.csdn.net/t/20060322/23/4633313.html2.4版本的servlet規(guī)范在部屬描述符中新增加了一個<dispatcher>元素,這個元素有四個可能的值:即REQUEST,FORWARD,INCLUDE和ERROR,可以在一個<filter-mapping>元素中加入任意數(shù)目的<dispatcher>,使得filter將會作用于直接從客戶端過來的request,通過forward過來的request,通過include過來的request和通過<error-page>過來的request。如果沒有指定任何< dispatcher >元素,默認值是REQUEST??梢酝ㄟ^下面幾個例子來輔助理解。
例1:
<filter-mapping>
<filter-name>Logging Filter</filter-name>
<url-pattern>/products/*</url-pattern>
</filter-mapping>
這種情況下,過濾器將會作用于直接從客戶端發(fā)過來的以/products/…開始的請求。因為這里沒有制定任何的< dispatcher >元素,默認值是REQUEST。
例2:
<filter-mapping>
<filter-name>Logging Filter</filter-name>
<servlet-name>ProductServlet</servlet-name>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
這種情況下,如果請求是通過request dispatcher的include方法傳遞過來的對ProductServlet的請求,則要經(jīng)過這個過濾器的過濾。其它的諸如從客戶端直接過來的對ProductServlet的請求等都不需要經(jīng)過這個過濾器。
指定filter的匹配方式有兩種方法:直接指定url-pattern和指定servlet,后者相當(dāng)于把指定的servlet對應(yīng)的url-pattern作為filter的匹配模式
filter的路徑匹配和servlet是一樣的,都遵循servlet規(guī)范中《SRV.11.2 Specification of Mappings》一節(jié)的說明
例3:
<filter-mapping>
<filter-name>Logging Filter</filter-name>
<url-pattern>/products/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
在這種情況下,如果請求是以/products/…開頭的,并且是通過request dispatcher的forward方法傳遞過來或者直接從客戶端傳遞過來的,則必須經(jīng)過這個過濾器。
The dispatcher has four legal values: FORWARD, REQUEST, INCLUDE,and ERROR. A value of FORWARD means the Filter will be appliedunder RequestDispatcher.forward() calls. A value of REQUESTmeans the Filter will be applied under ordinary client calls to the path or servlet. A value of INCLUDE means the Filter will be applied under RequestDispatcher.include() calls. A value of ERROR means the Filter will be applied under the error page mechanism. The absence of any dispatcher elements in a filter-mapping indicates a default of applying filters only under ordinary client calls to the path or servlet..