About
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.

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.

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
1 2 3 4 5 6 7 8 9 10 11 |
//<chain object>.doFilter(<request object>,<responce object>) //Inside the filter class it would be as follows public class NewFilter implements Filter { public void doFilter(ServletRequest request,ServletResponce responce, FilterChain chainObj) { //... chainObj.doFilter( request,responce); //... } } |
Method init() has one parameter FilterConfig that will be created by the container to get filter configuration from the web.xml file.
1 2 3 4 5 6 7 8 9 10 |
public class NewFilter implements Filter\ { public void init(FilterConfig configObj) { //one of the function available in this FilterConfig object //is shown below configObj.getServletContext().getRealPath(yourPathHere); //... } } |
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.
1 2 3 |
public void destroy( ){ // ... } |
Requirements for filters:
To write a filter, we have to implement a filter interface. Example is shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class LogFilter implements Filter { public void init(FilterConfig configObj) throws ServletException{ String stringToTest = configObj.getInitParameter("A String To Test"); System.out.println("Test Parameter: " + stringToTest); } public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws java.io.IOException, ServletException { //your required code chain.doFilter(request,response); } public void destroy( ){ } } |
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 “myClassName.java” will be executed.
1 2 3 4 5 6 7 8 9 |
<strong><</strong>filter> <filter-name>myFistFilter</filter-name> <filter-class>myClassName</filter-class> </filter> <filter-mapping> <filter-name>myFistFilter</filter-name> <url-pattern>/filter</url-pattern> </filter-mapping> |
Register Multiple Filter in Deployment Descriptor:
We can define multiple filters in web.xml. As shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<filter> <filter-name>AnyFilter</filter-name> <filter-class>AnyFilter</filter-class> </filter> <filter> <filter-name>SomeOtherFilter</filter-name> <filter-class>SomeOtherFilter</filter-class> </filter> <filter-mapping> <filter-name>AnyFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>SomeOtherFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
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.
- <filter>
- <filter-mapping>
- <servlet>
- <servlet-maping>
Download Complete Servlets Project Here
Summary:
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.