[Demo] Simple Servlet Filter and Servlet FilterChain

Filters are pluggable classes that stand between the client and a target component (like a servlet or JSP), within a web application. We can do pre or post processing of request/response data while it is coming from client to a servlet or from the servlet back to a client. 

Create a Servlet TargetServlet with URL Pattern as “/TargetServlet” and print some content to console from its doGet method. Code for TargetServlet is given in the end.

Now create a filter that will have the same url pattern for the servlet.

package com.javajee.filters;

 

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.annotation.WebFilter;

 

@WebFilter("/TargetServlet")

public class FirstFilter implements Filter {

 

  public FirstFilter() {

  }

 

  public void init(FilterConfig fConfig) throws ServletException {

  }

 

  public void doFilter(ServletRequest request, ServletResponse response,

      FilterChain chain) throws IOException, ServletException {

 

    System.out.println("Inside FirstFilter. Before FilterChain.doFilter");

    chain.doFilter(request, response);

    System.out.println("Inside FirstFilter. After FilterChain.doFilter");

  }

 

  public void destroy() {

  }

}

Execute the original servlet using the specified url pattern and you will get the below output in console:

Inside FirstFilter. Before FilterChain.doFilter

TargetServlet.doGet

Inside FirstFilter. After FilterChain.doFilter

 

FilterChain Demo

Create another filter exactly as FirstServlet with the same url pattern "/TargetServlet" and name it as SecondServlet.

Modify the SecondServlet's doFilter method print statements to reflect the servlet name as SecondServlet:

public void doFilter(ServletRequest request, ServletResponse response,

      FilterChain chain) throws IOException, ServletException {

 

    System.out.println("Inside SecondServlet. Before FilterChain.doFilter");

    chain.doFilter(request, response);

    System.out.println("Inside SecondServlet. After FilterChain.doFilter");

  }

Execute the original servlet using the specified url pattern and you will get the below output in console:

Inside FirstFilter. Before FilterChain.doFilter

Inside SecondFilter. Before FilterChain.doFilter

TargetServlet.doGet

Inside SecondFilter. After FilterChain.doFilter

Inside FirstFilter. After FilterChain.doFilter

 

Complete Code

TargetServlet Code

package com.javajee.servlets;

 

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet("/TargetServlet")

public class TargetServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

 

  public TargetServlet() {

    super();

  }

 

  protected void doGet(HttpServletRequest request, HttpServletResponse response)

      throws ServletException, IOException {

    System.out.println("TargetServlet.doGet");

  }

}

 

SecondServlet.java

package com.javajee.filters;

 

import java.io.IOException;import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.annotation.WebFilter;

@WebFilter("/TargetServlet")

public class SecondFilter implements Filter {

  public SecondFilter() {

  }

  public void init(FilterConfig fConfig) throws ServletException {

  }

  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain) throws IOException, ServletException {

    System.out.println("Inside SecondFilter. Before FilterChain.doFilter");

    chain.doFilter(request, response);

    System.out.println("Inside SecondFilter. After FilterChain.doFilter");

  }

  public void destroy() {

  }

}

 

Development and Execution Environment

Eclipse Luna Java EE IDE for Web Developers and Apache Tomcat 8.0.18, using Servlet spec 3.1.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!