Servlet Filters Tutorial


Servlet Filters are the latest components that are added in Servlet 2.3 specifications. These filters are used basically for intercepting and modifying requests and response from server.  Consider a scenario where you want to check session from the every users request and if it is valid then only you want to let the user access the page. You can acheive this by checking sessions on all the servlet pages (or JSP pages) which users queries or you can do this by using Filter.
Let us create a Servlet Filter that just prints the clients ip address and date time. This is just to log users who are accessing the application.

There are three interfaces defined in javax.servlet package.
  • FilterConfig – This object acts as configuration object for filter which pass information to filter during initialization. For eg, you may set initialization parameters in web.xml file for filter which could be accessed from this object. It also allow us to get a reference to ServletContext object. Every servlet container has its own implementation of FilterConfig interface.
  • FilterChain – This object is again created by servlet container which provides a convenient method to invoke next filter in the chain or final resource. The method is doFilter (ServletRequest request, ServletResponse response). Suppose one servlet is attached with two filters. In first filter calling doFilter() method of FilterChain object invokes second filter and calling the method in second filter invokes servlet itself.
  •  Filter – This is the filter object which actually performs filtering tasks. For creating Filter you have to declare a class which implements this interface. Like servlet, this also provides life cycle methods such as init() and destroy(). You have to implement doFilter(ServletRequest request, ServletResponse response, FilterChain chain) method which actually does filtering task. This method is called by servlet container for every request/response.
 In doFilter() method, the following steps are executed.
  • When a client makes HTTP request to a resource, the servlet container checks whether it is associated with filter.
  • If yes, the filter’s doFilter method is called.
  • Inside doFilter() method we may intercept request object and may modify the same. After that we call doFilter() method of FilterChain object. Here it invokes next filter in chain. If no more filters are available, final resource (Servlet, JSP etc) is called.
  • The resource like Servlet or JSP process the request and send response back to client. This again is passed through doFilter method of filter object. All statements written after doFilter() method of chain object is executed again.
  • Finally the response is sent back to client
The skeleton of doFilter() method is given below.
public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain)
                    throws IOException, ServletException{
      //preprocess statements for request/response objects
      ..................................................................
      ..................................................................     
      //call doFilter() of chain object, invoke next filter or resource     
      chain.doFilter (request, response);     
      //post-processing of request/response objects     
      ..................................................................
      ..................................................................
}


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email