October 22, 2018
Hot Topics:

Implementing the Intercepting Filter Pattern in Your Enterprise Java Applications

  • July 13, 2006
  • By Vlad Kofman
  • Send Email »
  • More Articles »

Servlet Implementation

Because this pattern is very useful, the creators of the Servlet API felt they need to provide its implementation. Therefore, the Servlet specification 2.3 included a filter and filter chain API. It also takes care of filter configuration via a main web.xml file. Moreover, intercepting filters can be set up transparently from the target Servlet or JSP and can wrap the request object. This implementation eliminates the need to write a lot of custom code, but on the other hand, it has limitations due to its predefined interfaces, and a requirement for a developer to learn its APIs.

Here is a part of the web.xml file that shows two filters that will intercept all requests to any URL pattern. However, the URL pattern can be set to be as specific as one Servlet only.




This configuration tells the container to invoke the filter first on any request before handling the request object to the main Servlet. For example, ScanFilter can check whether the request type is "multipart form data" (a special type for file upload) and scan all attached files before the request is passed forward. This filter can be reused in different applications because it is not tied with any specific application code.

Here is a sample of ScanFilter that uses the O'Reilly package to work with multipart requests.

import com.oreilly.servlet.multipart.*;

public class ScanFilter extends HttpServlet implements Filter { private FilterConfig filterConfig; public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } //Process the request/response pair public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) { try { String contentType = request.getContentType(); // Only filter this request if it is multipart encoding if (contentType.startsWith("multipart/form-data")) { MultipartParser mp = new MultipartParser(request, 10*1024*1024); Part part; while ((part = mp.readNextPart()) != null) { String name = part.getName(); if (part.isFile()) { // it's a file part FilePart filePart = (FilePart) part; if (filePart != null) { // scan it for viruses } } } } // end if filterChain.doFilter(request, response); } catch (ServletException sx) { filterConfig.getServletContext().log(sx.getMessage()); } catch (IOException iox) { filterConfig.getServletContext().log(iox.getMessage()); } }


In this article, I focused on the Intercepting Filter design pattern. It is one of the externally useful patterns in the category of enterprise Web development and should not be overlooked. It can simplify Web application development, maintenance, and reusability, as well as promote logic separation. As Web development technologies evolve, this pattern will probably change as well, but the main concepts should remain. Whether you will use a Servlet API or a custom solution to implement Intercepting Filter, you will benefit from its concepts in the long run.

Download Source


About the Author

Vlad Kofman is a Senior System Architect. He has implemented enterprise-scale projects for the major Wall Street firms, defense contracts, and the U.S. government. His main interests are object-oriented programming methodologies and design patterns.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


Thanks for your registration, follow us on our social networks to keep up-to-date