10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Filtering Requests and Responses<br />

120<br />

7. In the Add Servlet Filter dialog, enter the name of the filter in the Filter Name field.<br />

8. Click Browse to locate the servlet class to which the filter applies. You can include wildcard<br />

characters so that you can apply the filter to more than one servlet.<br />

9. Click OK.<br />

To constrain how the filter is applied to requests, do the following:<br />

1. Expand the Filter Mappings node in the Filters tab of the editor pane.<br />

2. Select the filter from the list of filters.<br />

3. Click Add.<br />

4. In the Add Filter Mapping dialog, select one of the following dispatcher types:<br />

■ REQUEST: Only when the request comes directly from the client<br />

■ FORWARD: Only when the request has been forwarded to a component (see “Transferring<br />

Control to Another Web Component” on page 124)<br />

■ INCLUDE: Only when the request is being processed by a component that has been<br />

included (see “Including Other Resources in the Response” on page 122)<br />

■ ERROR: Only when the request is being processed with the error page mechanism (see<br />

“Handling Servlet Errors” on page 105)<br />

You can direct the filter to be applied to any combination of the preceding situations by<br />

selecting multiple dispatcher types. If no types are specified, the default option is<br />

REQUEST.<br />

You can declare, map, and constrain the filter by editing the XML in the web application<br />

deployment descriptor directly by following these steps:<br />

1. While in the web.xml editor pane in NetBeans IDE, click XML at the top of the editor pane.<br />

2. Declare the filter by adding a filter element right after the display-name element. <strong>The</strong><br />

filter element creates a name for the filter and declares the filter’s implementation class<br />

and initialization parameters.<br />

3. Map the filter to a web resource by name or by URL pattern using the filter-mapping<br />

element:<br />

a. Include a filter-name element that specifies the name of the filter as defined by the<br />

filter element.<br />

b. Include a servlet-name element that specifies to which servlet the filter applies. <strong>The</strong><br />

servlet-name element can include wildcard characters so that you can apply the filter to<br />

more than one servlet.<br />

4. Constrain how the filter will be applied to requests by specifying one of the enumerated<br />

dispatcher options (described in step 4 of the preceding set of steps) with the dispatcher<br />

element and adding the dispatcher element to the filter-mapping element.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!