It defines the following three interfaces.

  1. Filter
  2. FilterConfig
  3. FilterChain

Filter interface

  1. Every Filter in java has to implement this interface either directly or indirectly
  2. This interface defines the following three methods
    • init()
    • destroy()
    • doFilter()

init()

public void init(FilterConfig conf)throws ServletException

This method will be executed only once to perform initialization activities.
It takes FilterConfing object as the argument which provides configuration information to the filter

destroy()

public void destroy()

This method will be executed only once whenever web container feels filter object no longer required.This method is to perform clean-up a activities

doFilter()

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

This method will be executed for every request.Entire filtering logic we have to define in this method only.
This method takes FilterChain as argument by using that we can forward the request to the next level (it may be another Filter or Servlet)

FilterConfing

For every filter, web container will create one FilterConfig object which provides configuration information to the Filter.
This interface defines the following methods

  1. public String getFilterName()
  2. public String getInitParameter(String name)
  3. public Enumeration getInitParameterNames()
  4. public ServletContext getServletContext()

FilterChain

By using FilterChain object, we can forward the request and response to the nextlevel.(It can be another filter or servlet)
This interface defines only one method

public void doFilter(ServletRequest req,ServletResponse resp)