The filters wizard helps you to create Java™ servlet filters by walking you through
the creation process and by providing you with output files that you can use
or that you can modify for use with your Web application. The filters can
run on Java EE-compliant Web servers.
To create a filter, complete the following steps:
- In the Java EE perspective, expand your dynamic project in the Project Explorer view.
- Right click on the Filter icon, and select from
the pop-up menu. The Create Filter wizard
appears.
- Follow the project wizard prompts.
General Information
- Modifiers
- The Sun Microsystems Java Servlet 2.3 Specification states
that a Servlet class must be public and not abstract. Therefore,
you cannot change these modifiers. The only one available for change is the final modifier.
- Interfaces
- There is one obligatory interface that filter classes must implement: javax.servlet.Filter.
This interface is provided by javax.servlet package and is used to represent
the life-cycle of the filter. This interface has three methods: init, doFilter
and destroy.
- The init method is called by the servlet container only once, when
it finishes instantiating the filter.
- ThedoFilter method is where the filtering is performed and is called
every time a user requests a resource, such as a servlet, to which the filter
is mapped.
- The destroy method is called by the servlet container to tell the
filter that it will be taken out of service.
Although, that the javax.servlet.Filter interface is provided in the Interfaces field
by default, you can add additional interfaces to implement by using the Add button.
- Interface selection dialog
- This dialog appears if you select to add an interface to your filter.
As you type the name of the interface that you are adding, a list of available
interfaces listed in the Matching items list box updates dynamically
to display only the interfaces that match the pattern. Choose an interface
to see the qualifier, and then click OK when finished.
- Filter Mappings
- For a filter to intercept a request to a servlet, you must:
- declare the filter with a <filter> element in the deployment
descriptor
- map the filter to the servlet using the <filter-mapping>
element.
Sometimes you want a filter to work on multiple servlets. You can do
this by mapping a filter to a URL pattern so that any request that matches
that URL pattern will be filtered. All this is made automatically if you use
the Filter Mappings field.
- Method stubs
- Because each filter must implement the javax.servlet.Filter interface,
the Inherited abstract methods option is always checked and cannot
be changed.