background image

Applying the Filter to Requests

<< Specifying Filter Mappings | Filter-to-Servlet Mapping >>
<< Specifying Filter Mappings | Filter-to-Servlet Mapping >>

Applying the Filter to Requests

6. Click Add Filter Element to map the filter to a web resource by name or by URL pattern.
7. In the Add Servlet Filter dialog, enter the name of the filter in the Filter Name field.
8. Click Browse to locate the servlet class to which the filter applies. You can include wildcard
characters so that you can apply the filter to more than one servlet.
9. Click OK.
To constrain how the filter is applied to requests, do the following:
1. Expand the Filter Mappings node in the Filters tab of the editor pane.
2. Select the filter from the list of filters.
3. Click Add.
4. In the Add Filter Mapping dialog, select one of the following dispatcher types:
REQUEST
: Only when the request comes directly from the client
FORWARD
: Only when the request has been forwarded to a component (see
"Transferring
Control to Another Web Component" on page 124
)
INCLUDE
: Only when the request is being processed by a component that has been
included (see
"Including Other Resources in the Response" on page 122
)
ERROR
: Only when the request is being processed with the error page mechanism (see
"Handling Servlet Errors" on page 105
)
You can direct the filter to be applied to any combination of the preceding situations by
selecting multiple dispatcher types. If no types are specified, the default option is
REQUEST
.
You can declare, map, and constrain the filter by editing the XML in the web application
deployment descriptor directly by following these steps:
1. While in the web.xml editor pane in NetBeans IDE, click XML at the top of the editor pane.
2. Declare the filter by adding a filter element right after the display-name element. The
filter
element creates a name for the filter and declares the filter's implementation class
and initialization parameters.
3. Map the filter to a web resource by name or by URL pattern using the filter-mapping
element:
a. Include a filter-name element that specifies the name of the filter as defined by the
filter
element.
b. Include a servlet-name element that specifies to which servlet the filter applies. The
servlet-name
element can include wildcard characters so that you can apply the filter to
more than one servlet.
4. Constrain how the filter will be applied to requests by specifying one of the enumerated
dispatcher options (described in step 4 of the preceding set of steps) with the dispatcher
element and adding the dispatcher element to the filter-mapping element.
Filtering Requests and Responses
The Java EE 5 Tutorial · September 2007
120