Introduction
In Servlet applications, a filter can handle security breaches. A filter is a servlet components that provides some useful service to the request and response. The filter lies between the client and the web application. This intercepts the requests to web applications and responses from them. The intercepted requests or responses are manipulated as per the requirements. The request or response manipulation can be checking for the authentication, logging in user information or compressing the data. There can be more than one filter between a client and a web application. This set of filters forms a filter chain. The request and response are processed in each filter of the filter chain sequentially.
How Do Filters Work?
The filter works as an interface between the client and the web application in the following ways,
Intercepts request
The request sent to the web application wrapped in the request object is captured by a wrapper object created by the filters. This wrapped request is then manipulated by the filter.
Processes the wrapped request
The filter then processes the request as per the requirement. The filter contains the method, which actually act over the captured request and modifies the same.
Send request to servlet
Then the modified request is sent to the appropriate servlet wrapped inside a wrapper object.
Processes the wrapped response
The request sent from the servlet, which is already wrapped inside a wrapper object is captured by the filter. The filter then processes the request as per the requirement and generates a response. The processing of the response can dynamically change the format of text sent to the client or compress the amount of data sent to the client over the network.
Sends response
The processed response is then freed by the capture object and captured by the ServletResponse object. Then the response is sent to the client browser.
Filter API
The filter API brings in all the required interfaces for creating a filter class into the javax.servlet package. It contains three interfaces which create and handle the functionalities of a filter. These are Filter interface.FilterConfig interface and FilterChain interface.
Filter Interface
The filter interface is mandatory for creating the filter component in the Web container. Create a class that implements this interface so that it can gain access over the methods in the interface.. Some of the methods in the Filter interface are,
init()
This is called by the servlet container to initialize the filter. It is called once.
public void init(FilterConfig filterConfig)throws ServletException
doFilter()
The doFilter() method of the Filter interface is called by the coordinator each time a request or response is processed.
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)throws IOException , ServletException
destroy()
This method is called by the servlet container to inform the filter that its service is no longer required.
public void destroy()
- import java.io.IOException;
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- inport javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- public class ProcessTimeFilter implements Filter {
- private String processingTime = null;
- private FilterConfig filterConfig = null;
- public void destroy() //destroy() method is called to release the filter from the operation.
- {
- this.processingTime = null;
- this.filterConfig = null;
- }
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
- if (processingTime != null) {
- request.setAttribute(processingTime, this);
- }
- //Assign the current time in millisecond to begin.
- long begin = System.currentTimrMillis();
- chain.doFilter(request, response);
- /* The current time in milli seconds after the control passes from one to the next filter is assigned to the variable end.*/
- long end = System.currentTimeMillis();
- //The processing time is calculated and assigned to the FilterConfig object.
- filterConfig.getServletContext().log(this.toString() + ":" + (end - begin) + "milliseconds");
- }
- //The Filter is initialized
- public void init(FilterConfig filterConfig) throws ServletException {
- //The filterConfig object is assigned the passed value.
- this.filterConfig = filterConfig;
- this.processingTime = filterConfig.getInitParameter("processingTime");
- }
- public String toString() {
- if (filterConfig == null) return ("InvokerFilter()");
- StringBuffer sb = new StringBuffer("InvokeFilter(");
- sb.append(filterConfig);
- sb.append(")");
- return (sb.toString());
- }
- }

Join the conversation! Your thoughts help the community grow.