Creating a Filter

See Also

A filter is a piece of re-usable code that can transform the contents of HTTP requests, responses, and header information by modifying requests to and responses from a servlet.

You create a filter by implementing the javax.servlet.Filter interface and providing a public constructor taking no arguments. The IDE provides a wizard to help you create filters. The simple filter template helps you to create a basic filter class, while the advanced filter template helps you to create a more complex filter class with wrapper classes.

Filters are declared using the filter element in the web module's deployment descriptor (web.xml) and packaged in a WAR file along with the static content and servlets that make up a web module. A filter or collection of filters can be configured for invocation by defining the filter-mapping elements in the deployment descriptor.

To create a Filter:

  1. Choose File and choose New and choose JSP & Servlet and choose Filters.
  2. In the New Wizard, choose the appropriate filter template from JSP & Servlet and choose Filters. See the template description for more information about the AdvancedFilter and SimpleFilter templates.
  3. Follow the instructions in the New Wizard, then click Finish. The source code for the filter you created appears in the source editor.

You can also create a filter from within the Explorer. Right-click the location where the filter will be stored (typically in WEB-INF/Classes), then choose New and choose JSP & Servlet and choose Filters and the appropriate filter template from the contextual menu.

For more information about filters, see the Java Servlet 2.3 Specification available at http://java.sun.com/products/servlet .

See Also

Filters (defined)
File Location in a Web Module
Setting Filter Properties
Adding a Filter to the Deployment Descriptor


Legal Notices