Servlet Filters


In the previous tutorial, we have discussed “Understanding Servlet Lifecycle,” servlet life cycle has the three methods in its life cycle init, service and destroy. In this tutorial, we will discuss filters in Servlets. Filters also have three methods in its life cycle but they are a little bit different from servlet life cycle methods, we also discuss the advantages of the servlet filters. Our table of contents will be

  • Introduction to the Servlet Filters
  • Servlet Filter Architecture
  • Advantages of the servlet filter
  • Filter methods
  • Requirements for filters
  • Order of the elements in deployment descriptor

Introduction to Filters in Servlet:

Java Version 2.3 Introduces a new feature in Java servlets, and that is ServletFiilters. Servlet filters are the objects host responsibility is to interpret HTTP request coming from user’s browser. By using filters, we can modify the request before it arrives at the servlet. In the same way, we can manipulate response coming from the server before sending it to the client. These filters are configured in the Web.xml, so these are plug-able by simply removing the entries from there. We can map a filter on many servlets, or we can map many filters on one Servlet. Servlet follows a decorator pattern

Servlet Filter Architecture:

When client request arrives on the server it is then forwarded to the container, Container then Reads The URL and check the Filter’s URL mapping in web.xml if it finds filter mapping it will execute the filter code, servlet then sends back the request to the filter , filter pass it to the container and finally container sends the response back to the client. In this way, one request is handled and intercepted by the filter.

Servlet FIlters
Servlet Filters

Advantages of Servlet Filters:

We can manipulate input coming from the users without changing any implementation in the Servlet.

Following are the list of useful features that can be achieved using Servlet Filters.

  • Data Compression:  it is used to decrease the bit size to reduce the size of data in technical terms if we try to remove statistical redundancy it is called compression. There are encryption functions available within Java to perform these operations.
  • Encryption
  • Authentication
  • many others

We can manage sessions with the help of filters

Security features can be added with the help of filters

Filter Methods:

Filter interface has three methods

  • doFilter(ServletRequest, ServletResponce, FilterChain)
  • init(FilterConfig filter config)
  • destroy().

Method doFilter has three parameters Servlet request, Servlet response and Filter chain. Here we know about the Request, and the response objects they are the same as we have discussed them in our earlier tutorials, filter chain object is new here, If we have mapped many filters on one Servlet then we have a chain of filters before a servlet as shown below.

Filter within a filter
Filter within a filter

Identification of the next filter in the chain of filters will be made by FilterChain object. There is a function that passes the control to its next filter and that function is doFilter(). It can be used as follows

Method init() has one parameter FilterConfig that will be created by the container to get filter configuration from the web.xml file.

Finally, the Method destroy() it will have all the clean up code. It is used to remove the filter from the service, general form of the destroy method can be shown below.

Requirements for filters:

To write a filter, we have to implement a filter interface. Example is shown below

Then it should also be configured in deployment descriptor that is web.xml

How to Register Filter in Deployment Descriptor(Web.XML)?

Filter-Class defines the class name of the filter; URL-pattern tells the web URL pattern of the filter and filter-name is for finding the filter class name through URL pattern.

In the example below if the requested URL has the pattern /filter in it then the filter class named “” will be executed.

Register Multiple Filter in Deployment Descriptor:

We can define multiple filters in web.xml. As shown below

Order of the Elements in Deployment Descriptor

As the filters are designed to use with servlets so we should also pay attention to the ordering of the elements in the deployment descriptor, the XML elements must appear in the following order.

  1. <filter>
  2. <filter-mapping>
  3. <servlet>
  4. <servlet-maping>

Download Complete Servlets Project Here


In this tutorial, we have accomplished following things.

  • Learned about the Servlet Filters
  • Learned init(),doFilter() and destroy() Methods

In the next tutorial, you will learn about Servlet Listeners so stay tuned and stay blessed.