Which of the following statements about the Filter mechanism are true?
[ Choose two ]
a. Two instances of the same filter class will be instantiated if there are two filter declarations for the same filter class.
b. For associating with groups of servlets and static content, the <servlet-name> element is used in mapping.
c. The last filter in <servlet-name> filter chain invokes the first filter in the <url-pattern> matching chain, or invokes the target Web resource if there are none.
d. Filters are not invoked for request dispatcher forward() and include() calls.
e. Filters are not invoked for request dispatcher forward() and include() calls.
f. The ServletConfig object can be used to read the initialization parameters for a filter.
g. Filters can be mapped using <servlet-name> or <url-pattern> element.
Choice A and Choice F are correct
The container instantiates exactly one instance of the Java class defining the filter per filter declaration in the deployment descriptor. Hence, two instances of the same filter class will be instantiated by the container if the developer makes two filter declarations for the same filter class. Hence choice A is correct.
Filters can be mapped using . servlet-name: To associate with a servlet name . url-pattern: To associate with a group of servlets and static resources Hence choice F is correct while choice B is incorrect.
The order that the container uses in building the chain of filters to be applied for a particular request URI is as follows:
1. First, the <url-pattern> matching filter mappings in the same order that these elements appear in the deployment descriptor.
2. Next, the <servlet-name> matching filter mappings in the same order that these elements appear in the deployment descriptor.
So the last filter in <url-pattern> filter chain invokes the first filter in the <servlet-name> matching chain, or invokes the target Web resource if there are none. Hence choice C is incorrect.
From the Servlets 2.4, it is possible to apply filters for resources invoked using the Request Dispatcher mechanism. The <dispatcher> element under <filter-mapping> provides this possibility. This element has four legal values: . FORWARD - Filter will be applied under RequestDispatcher.forward() calls .
REQUEST - Filter will be applied under ordinary client calls to the path or servlet.
INCLUDE - Filter will be applied under RequestDispatcher.include() calls.
ERROR - Filter will be applied under the error page mechanism. Hence choice D is incorrect. The names and values of these filter initialization parameters are available to the filter at runtime via the getInitParameter and getInitParameterNames methods on the filter's FilterConfig object and not via ServletConfig object. Hence choice E is incorrect.
But if the developer define the same name for two filters of the same class in DD, the application cannot be deployed.
I believe the correct answers are F and G.
For g, filters can be mapped using servlet-name and url
pattern.