The 2.4 version of the servlet specification adds a new <dispatcher> element to the subordinate descriptor, which has four possible values: Request,forward,include and error, which can be found in a < Add any number of <dispatcher> to the filter-mapping> element, so that the filter will act on the request that comes directly from the client, through the request of forward. Request via include and request via <error-page>. If no <dispatcher> element is specified, the default value is request. There are a few examples to help you understand.
Example 1:
XML code
- <filter-mapping >
- <filter-name>logging filter</ filter-name>
- <url-pattern>/ Products/*</url-pattern>
- </ Filter-mapping>
In this case, the filter will function directly from the client to/products/... The request started. Since there are no <dispatcher> elements, the default value is request.
Example 2:
XML code
- <filter-mapping>
- <filter-name>logging filter</filter-name>
- <servlet-name>productservlet</servlet-name>
- <dispatcher>include</Dispatcher>
- </filter-mapping>
In this case, if the request is passed to the Productservlet request through the include method of request dispatcher, it is filtered by this filter. Other requests, such as those that come directly from the client, are not required to pass through the filter productservlet.
There are two ways to specify how filter is matched: directly specify Url-pattern and the specified servlet, which is equivalent to the url-pattern of the specified servlet as the filter's matching pattern. The path match for filter is the same as the servlet, followed by the description of the srv.11.2 specification of Mappings section in the servlet specification.
Example 3:
XML code
- <filter-mapping>
- <filter-name>logging filter</filter-name>
- <url-pattern>/products/*</url-pattern>
- <dispatcher>forward</Dispatcher>
- <dispatcher>request</Dispatcher>
- </filter-mapping>
In this case, if the request is in/products/... and is passed through the forward method of the request dispatcher or directly from the client, it must pass through this filter.
<dispatcher> role in <filter-mapping> in Web. xml