Servlet Filter


A filter is an item that is invoked on the preprocessing and postprocessing of a request.

It is particularly used to carry out filtering responsibilities including conversion, logging, compression, encryption and decryption, input validation etc.

The servlet filter is pluggable, i.e. its access is described withinside the web.xml file, if we eliminate the access of clear out out from the web.xml file, clear out out might be eliminated routinely and we do not want to extrade the servlet.

So the upkeep cost might be less.

Note-Contrary Servlet, One filter doesn’t have dependency on any other filter.

Usage of Filter

  • recording all incoming requests
  • logs the IP addresses of the computers from which the requests initiated
  • conversion
  • data compression
  • encryption and decryption
  • input validation etc.

Advantage of Filter

  • Filter is pluggable.
  • One filter does not have dependency onto another resource.
  • Less Maintenance

Filter API

Like servlet filters have their own API. There are three interfaces of Filter API contained by the javax.servlet package.

  1. Filter
  2. FilterChain
  3. FilterConfig

1) Filter interface

For creating any filter, you want to implement the Filter interface. The life cycle methods for a filter are provided by Filter interface.



public void init(FilterConfig config)

init() way is supplicate only once. It is used to initialize the filter.

public void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain)

doFilter() method is supplicate every time when a user requests any resource, to which the filter is mapped. It is used to execute filtering tasks.

public void destroy()

This is supplicate only once when filter is taken out of the service.

2) FilterChain interface

The object of FilterChain is responsible to invoke subsequent filter or resource within the chain.This object is passed within the doFilter method of Filter interface.The FilterChain interface contains just one method:

  1. public void doFilter(HttpServletRequest request, HttpServletResponse response): it passes the control to subsequent filters or resources.

How to define Filter

We can define filters the same as servlets. Let's examine the weather of filter and filter-mapping.





Either Url-Pattern or Servlet-name both can be used for mapping filters. The Url-Pattern component has a benefit over Servlet-name components, that is it can be applied on servlet, JSP or HTML.

Easy Example of Filter

We are easily showing information that filter is supplicate automatically once after the post processing of the request, in the below given example:


<a href="servlet1">click here</a> 


import java.io.IOException;  
import java.io.PrintWriter; 
import javax.servlet.*;    

public class MyFilter implements Filter{    

public void init(FilterConfig arg0) throws ServletException {}        

public void doFilter(ServletRequest req, ServletResponse resp,  
    FilterChain chain) throws IOException, ServletException {            

    PrintWriter out=resp.getWriter();  
    out.print("filter is invoked before");           

    chain.doFilter(req, resp);//sends request to next resource           

    out.print("filter is invoked after");  
    public void destroy() {}  


import java.io.IOException;  
import java.io.PrintWriter;    

import javax.servlet.ServletException;  
import javax.servlet.http.*;    

public class HelloServlet extends HttpServlet {  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        PrintWriter out = response.getWriter();        

welcome to servlet
); } }


Filter element of the web-app must be defined just like a servlet, For defining the filter.






download this example (developed using Myeclipse IDE)

download this example (developed using Eclipse IDE)

download this example (developed using Netbeans IDE)