Show TOC Start of Content Area

Background documentation Developing Filters  Locate the document in its SAP Library structure

Filters are a new type of Web component defined by the Java Servlet 2.3 Specification. To a certain extent they resemble servlet components. However, they do not generate their own content (as servlets do), rather they plug into the request-response cycle and modify the request or response objects. Filters can be applied to dynamic Web application components, as well as to static resources (such as HTML pages, images, and so on). They can easily be plugged into any application without the application knowing anything about the filters, simply by mapping them to certain application components (or a URL) using the application’s deployment descriptor. Later on, the Web Container determines when the filter is invoked based on the configuration information in the deployment descriptor.

Strictly speaking, a filter is a class that implements the javax.servet.Filter interface. The filter class contains at least the following three methods that shape the basic phases of the filter components life-cycle: init(), doFilter() and destroy() methods.

Filters Life-cycle

The life cycle of the filter components is similar to that of servlets. It consists of the following phases:

...

       1.      Loading the filter class – this is done when the Web application that the filter is part of starts.

       2.      Creating an instance of the filter class.

       3.      Initializing the filter - the Web Container calls the init() method of the filter once per filter declaration.

       4.      Performing filtering actions according to the logic programmed in the doFilter() method.

       5.      Destroying the filter instance – the Web Container calls the destroy() method of the filter if the application is stopped.

Common Programming Tasks for Filters Development

When developing filters you have to take care of:

·        initializing the filters using the init() method of the filter class.

·        implementing logic in the doFilter() method.

Note

Do not forget that after you develop the filters classes, you have to configure the filters using the deployment descriptor of the Web application. There you also define the filter chain, that is, the order of invocation of multiple filters for a particular resource of the application.

The filter classes must be also properly packaged together with the other files of the Web application, so that they are deployed on the J2EE Engine’s Web Container.

You can use the SAP NetWeaver Developer Studio throughout the whole filter development and deployment process.

For more information about filters and related development issues, refer to the JavaÔ Servlet 2.3 Specification.

 

 

End of Content Area