How to develop a filter and configure in servlet web application

In this tutorial you will how to develop and configure a filter in servlet technology, you can assign one filter for more than one web resources, as well we write more than one filter of one web resource.

Steps require developing the filter

Write a public class by implement Filter interface.

Inside Filter interface there are three methods, you have to implement all three methods. These methods are:

init()

doFilter()

destroy()

Only throw chain.doFilter (request, response), request go to the destination web resource.

Below code show you the example of Filter.

package com.java.connect.filter;
 
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;
 
public class FilterExample implements Filter {
 
    public void init(FilterConfig filterConfig) throws ServletException {
        // Do some stuff
    }
 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
        throws IOException, ServletException {
        // Execute forward direction
        chain.doFilter(request, response);
        // Execute backward direction
    }
 
    public void destroy() {
        // Do some stuff
    }
}

After write this class, put this class into classes folder of your web application, we have to register this filter inside web.xml of your web application, and while registering inside web.xml we have to assign a web resource to this filter.

Configure filter in web.xml

Below is code of web.xml, which shows you the configuration of Filter created above.

    <filter>
        <filter-name>loginServelt</filter-name>
        <filter-class>com.java.connect.filter.FilterExample</filter-class>
    </filter>
    <!-- Login filter mappings -->
    <filter-mapping>
        <filter-name>loginServelt</filter-name>
        <url-pattern>/loginServelt</url-pattern>
    </filter-mapping>

Some URL which we have choose for web resource put that URL url-pattren tag, we can same URL which has been used in servlet url-pattern tag.

But we should use servlet-name instead of url-pattern, if you are assigning a filter for servlet, by using this we can assign more than one filter for same servlet and we can assign same filter for more than one web resource.

If you want to assign one filter to more than one resource go for more filter-mapping with same filter-name.

By default filter is having load-on-startup, we need not to be put explicitly, so whenever container started the class of filter loaded into memory, the object of filter is created and init method of filter will be called.

If you want to put filter specific parameters, you can put similar to servlet configuration. And the same init parameters you can get by using FilterConfig object which is available in your filter class, the example of getting filter init parameter in filter class as below.

To retrieve the init parameters in filter you can use config.getInitParameter(“paramName”) as shown in below example

private FilterConfig config;
 
public void init(FilterConfig filterConfig) throws ServletException {
    // Do some stuff
    this.config = filterConfig;
}
 
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
     throws IOException, ServletException {
    // get the init params
    String paramValue = config.getInitParameter("paramName");
    chain.doFilter(request, response);
}

At the time of calling the init method of filter, the container create a FilterConfig object and put inside a global attribute, than use that attributes to retrieve the filter init parameters which are configured along with filter configuration in web.xml.

The destroy () method of filter called when you stop the container.

So the life cycle of your web application and filter started at same time when container started and it maintained by same agent which is the container.

If you have put more than one filter for same resource, than they will be execute by their order from top to bottom.

In the 99% scenario filters are used for maintains of application.