What is a filter?

In this post, you will learn:

  • What is a filter?
  • What are the uses of a filter in a web application?

What is a filter?

A filter is an optional component of a web application which is used to perform pre and post processing operations such as logging, validation, flow control etc. Filters are associated to the servlets and HTML pages of the applications. Whenever a request is received for these components, associated filter is invoked. The filter performs required pre processing and invokes the requested component. When the request is processed by the component, control is returned to the filter which performs the required post processing. Following diagram describes the flow of a request processing using filter.

filter

Description of the diagram:

1.0 – Client sends a request to a Servlet with which a filter is associated.

1.1 – The filter is invoked by the server.

1.2 – Filter performs pre processing and invokes the servlet.

1.3 – Request is processed by the servlet and control is returned to the filter.

1.4 – Post processing if any is performed by the filter and control is returned to the server.

1.5 – Response is returned to the client by the server.

What is pre and post processing?

The tasks which are performed before and after the actual processing are called pre and post processing respectively. In a web application, we can have request processing logic in the form of servlets; Event processing logic in the form of listeners and common pre and post processing logic in the form of filters. To understand all these, lets take the following real life analogy:

Assume a web application to be a metro train station in which passengers are requests, metro trains are servlets and journey of the train is the request processing. Each metro station has a two storey building. The first floor has the common area and the second floor has platforms. Each passenger goes to the common area where security check is performed and entry to the platform is given by marking the staring point of the journey with the help of token or smart card. At the platform, passengers board and unboard the metro train. After unboarding each passenger goes again to the common area and deposits the token or swaps the card to mark the ending point of the journey.

Performing the security check and marking the starting point of the journey are examples of pre processing. Marking the ending point of the journey is the example of post processing.

At the entry of each metro station you will find armed security personnel who seem to do nothing. They are stationed there to prevent terrorist attacks i.e. they represents event processing.

Uses of a filter in a web application:

1. Logging: Keeping the record of something for future use is called logging. Logging is a common requirement of web applications especially where financial transactions are involved. Logging can be done in the following two ways:

First, A logger class is defined having a method for logging and this method is invoked from each servlet. This approach has following problems:

   a) Each static page need to be converted to a servlet so that its log can be maintained.

   b) Invocation of the logging method from each servlet will create maintenance problem. If the logger method is changed in future, all the servlet need to be modified.

Second, A filter is defined and associated to each servlet and HTML page of the application. From this filter the logger method is invoked before or after the request processing. Advantages of this approach are:

   a) Log of static page can be maintained without converting them to servlet.

   b) If the logger method is changed in future, only a single filter need to be modified.

2. Validation: The process of checking whether the input or output is correct or not is called validation. Validation is also a common requirement of web applications. Being common requirement, it must be implemented with the help of filters.

3. Flow Control: The process of managing the flow of requests in a web application is called flow control. You must have noticed in web sites such as facebook, gmail etc, if you directly send a request for a bookmarked page, you are redirected to the login page. Such functionality can be easily implemented with the help of a filter which act a single entry point of the application. The filter is associated to each static as well as dynamic page of the application which can only be viewed after login. Whenever a request is submitted for such a page, the filter is invoked which checks for the user session. If user session is found, request is forwarded to the page otherwise it is forwarded to the login page.

Similar to logging, validation and flow control any operation which is common for some or all requests should be implemented with the help of filters.


Its time to recap the main concepts:

  1. A filter is an optional component which is used to perform common pre and post processing in a web application.
  2. Logging, validation and flow control are some examples of pre and post processing which are implemented using filters.
  3. A filter can be associated to static as well as dynamic components of a web application.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *