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
Follow the project wizard prompts.
- 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.
- 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
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.
- 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
- The destroy method is called by the servlet container to tell the
filter that it will be taken out of service.
- 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:
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.
- declare the filter with a <filter> element in the deployment
- map the filter to the servlet using the <filter-mapping>
- Method stubs
- Because each filter must implement the javax.servlet.Filter interface,
the Inherited abstract methods option is always checked and cannot