This J2EE tip describes a method of writing a servlet filter. A servlet filter is an object that can transform the header and content (or both) of a request or response. Applications of filters include authentication, logging, image conversion, data compression, encryption etc. Further, Filter,FilterChain and FilterConfig are defined as part of javax.servlet package. Here an implementation of doFilter is given which receives request,response and FilterChain object as inputs. Moreover, init and destroy methods will be called when filter is initialized and destroyed.

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import java.util.Enumeration ;

public final class Filter1 implements Filter { 
  private FilterConfig fcon = null;
  PrintWriter out;
  public void init(FilterConfig filterConfig)
    throws ServletException  {
    fcon = filterConfig; 
  public void destroy() {
    fcon = null; // destroy method called
  public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
      HttpServletResponse httpres;
        if (fcon == null) 
      httpres = (HttpServletResponse ) response;
      /* Perform typecast operation as HttpServletResponse
      is sub class of ServletResponse
      out = response.getWriter();
      out.println("Website is temporarily closed for maintainance");
      out.println("Thanks for visiting");