Show TOC

Procedure documentationDeveloping Filters Locate this document in the navigation structure

 

Filters enable you to intercept a request before it reaches a resource. In other words, a filter gives you access to the HttpServletRequest and the HttpServletResponse objects before they are passed on to a servlet by the Web Container.

Filters can be very useful. For example, you can write a filter that records all incoming requests and logs the IP addresses of the computers from which the requests originate. You can also use a filter as an encryption and decryption device. Other uses include user authentication, data compression, user input validation, and so on.

You can also put a set of filters in a chain. The first filter in the chain is called first by the Web Container, and then it passes control to the second filter, and so on. Filter chaining ensures that you can write a filter that does a specific task but adds some functionality in another filter.

When writing a filter, you deal with the following interfaces in the javax.servlet package (some of them are optional):

  • javax.servlet.Filter

    Represents the life cycle of the filter.

  • javax.servlet.FilterConfig

    Represents the configuration of the filter.

  • javax.servlet.FilterChain

    An object provided by the servlet container to the developer giving access to the invocation chain of a filtered request for a resource.

SAP NetWeaver Developer Studio allows you to easily create listeners and filters implementing the corresponding interfaces in the javax.servlet package.

Prerequisites

A Dynamic Web Project exists.

Procedure

  1. In the Project Explorer, select the Dynamic Web Project.

  2. In the context menu, choose   New   Filter  .

  3. In the CreateFilter wizard, enter the settings as required. Choose Next.

    If you want the filter to intercept requests before they reach a servlet, you need to specify filter mappings. This is done in the following way:

    1. Choose the Add button next to the Filter M appingspane.

    2. Choose the Servlet or URL Pattern option regarding the following:

      • If you want the filter to work for a single servlet only, choose the Servlet option.

        Note Note

        This option is available only if you have at least one servlet created.

        End of the note.
      • If you want the filter to work for a list of servlets, choose the URL Pattern option. In that case, all requests coming to a whole URL pattern will be filtered.

    3. Depending on the option you have chosen, select the servlet or enter the URL pattern to be filtered, respectively.

    4. Choose one or more of the available dispatchers. The filter will be active for those dispatchers.

    5. Choose OK.

  4. When you are ready with the filter settings, choose Finish.

Result

The Developer Studio generates the skeleton source code of the filter. You can now implement the class body as necessary. It is open in the Java editor, and has the following methods:

  • The init method is called by the Web Container only once when it finishes instantiating the filter.

  • The doFilter 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 Web Container to tell the filter that it will be taken out of service.

The filter, along with its initialization parameters and mappings, is automatically added to the web.xml of the Dynamic Web Project.