View RSS Feed

My Java Tips

The Intercepting Filter

Rate this Entry
by , 11-19-2011 at 05:50 PM (1675 Views)
In a client-server environment, a system must be able to handle client requests with different processing needs before sending them to server.


- The system receives requests from a client with different protocols such as HTTP, FTP, SMTP etc.
- The system must authorize and authenticate some requests, where as for some requests it is not necessary to authorize and authenticate requests and system can handle such requests directly.
- From certain requests and responses a system must add or remove some information.

For an efficient system, these requests and responses must be pre-processed and post-processed in a proper way. In addition to that this processing must be independent from the rest of the system. The design pattern Intercepting Filter helps in efficiently handle this all. A filter is an object that intercepts communication between client and server and then filters the data being passed between them.

The Intercepting Filter separates request/response processing from the rest of the system by creating filters. The design pattern Intercepting Filter comprises of several javax.servlet. Filter objects that process requests and response. There can be a single interface for transforming requests and responses when a Intercepting Filter is used. Intercepting Filter provides a standard way to modify requests and responses when needed. It also provides standardized and centralized pre-processing and post processing of requests and responses.



Intercepting Filter facilitates to insert or remove filters runtime during deployment. Also to be noted that Intercepting Filter deals with requests/response and not with applications appearance. The design pattern Intercepting Filter perform with minimum effect on external code processing. It provides authentication, authorization, transformation and conversion of requests and responses as needed.

The example below demonstrates use of filter. It is very basic example which shows minimal use of Filter. SimpleFilterDemo class implements Filter class. doFilter() is the important method in this class which will consists of logic for filtering request/response. The output Simple Filter Demo will be shown in the browser.

Java Code:
import java.io.*;
import javax.servlet.*;

public class SimpleFilterDemo implements Filter
{
   private FilterConfig filterConfig;

   public void init(FilterConfig filterConfig)
   {
	  this.filterConfig = filterConfig;
 	  System.out.println("Simple Filter Demo");
   }            

   public void doFilter(
				  ServletRequest request,
				  ServletResponse response,
				  FilterChain filterChain
				 ) throws ServletException, IOException
   {
	  PrintWriter pw = response.getWriter();
	  pw.println("");
	  pw.println("");
	  pw.println("");
	  pw.println("");
	  pw.println("
Simple Filter Demo


");
	  pw.println("");
	  pw.println("");
   }         

   public void destroy()
   {
   }      

}

Submit "The Intercepting Filter" to Facebook Submit "The Intercepting Filter" to Digg Submit "The Intercepting Filter" to del.icio.us Submit "The Intercepting Filter" to StumbleUpon Submit "The Intercepting Filter" to Google

Comments