Filter life cycle and the role of FilterConfig

In this post, you will learn:

  • What is filter life cycle?
  • What is the role of FilterConfig?

Filter life cycle:

Filter life cycle describes how and when a filter object is created and initialzed, how it performs pre & post processing and how and when it is destroyed by the server. Filter life cycle is defined by javax.servlet.Filter interface with the help of following methods:

  1. init(): This method is invoked by the web server only once, just after the filter object is created. It is used by the server to provide the reference of a FilterConfig object to the filter and it can be used by the application programmer to initialize the filter. It has following syntax:

public void init(FilterConfig config) throws ServletException;

FilterConfig is an interface of servlet API, implemenation of it, is provided by server vendors. An object of the class which implements this interface is provided by the server to the filter as parameter of init() method. Role of this object is covered later in the post.

  1. doFilter(): This method is invoked by the web server each time a request is received for the component to which the filter is associated. It is used by the filter to apply pre processing, to get the next component invoked, and to apply post processing. It has following syntax:

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

In addition to ServletRequest & ServletResponse obects, an object of type FilterChain is provided as argument in this method. Role of this object is discussed in the next post.

  1. destroy(): This method is invoked by the web server only once, just before the filter is unloaded. It is used by the server to give a chance to the filter to free the resources used by it. It has following syntax:

public void destroy();

Role of FilterConfig:

For each Filter object an object of type FilterConfig is created by the server. This object has following use:

1. It is used by the server to provide the reference of the ServletContext object to the Filter.

2. It is used by the server to provide the initialization parameters to the Filter.

3. It is used by the server to provide the filter name to the Filter.

Each component of the application whether it is servlet, listener or filter, is provided the reference of the ServletContext object of the application by the server. Following diagram shows how a filter is connected to the ServletContext.

servletconfig_context

Description of the diagram:

1.0 – An application containing filter in addition to servlets and HTML pages is deployed on the server.

1.1 – ServletContext object is created for the application by the server.

1.2 – Filter object is created by the server.

1.3 – A FilterConfig object is created for the filter and

   a) – Reference of the ServletContext object is provided to it.

   b) – <init-param> elements if defined for the filter in web.xml are read and their name and values are stored in a map in it.

   c) – Name given to the filter in web.xml is stored in it.

1.4 – init() method is invoked on the filter and reference of the FilterConfig object is provided as argument.

FilterConfig interface provides following methods:

  1. getServletContext(): It is used by a filter to obtain the reference of the ServletContext object from the FilterConfig. It has following syntax:

public ServletContext getServletContext();

  1. getInitParameter(): This method is used by a filter to obtain the value of an initialization parameter. It has following syntax:

public String getInitParameter(String paramName);

  1. getInitParameterNames(): This method is used by a filter to obtain the name of all initialization parameters which are defined for it in web.xml file. It has following syntax:

public Enumeration<String> getInitParameterNames();

getFilterName(): This method is used by a filter to obtain its name. It has following syntax:

public String getFilterName();


Its time to recap the main concepts:

  1. Filter interface defines life cycle of a filter in the form of init(), doFilter() and destroy() methods.
  2. Filter objects are created by the server at the time of deployment.
  3. For each filter, a FilterConfig object is created by the server.
  4. Through FilterConfig object, server provides the reference of the ServletContext object, filter name and initialization parameters to the filter.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *