A creative use of Filters

I’ve just finished grading the assignments of my students for the semester. I change the assignment every now and then. The current one is a very simplified e-commerce shop.

The main feature revolves around a couple of screens, and cart management:

  • The product detail page is mainly composed of the product detail component. One can add one such product to the cart by clicking the Cart button

    product detail

  • The products list page aggregates a couple of product detail components. One can add one for each of the displayed product by clicking the Cart button
  • On the checkout page, for each product in the cart:
    • One can add one by clicking the + button
    • One can remove one by clicking the - button
    • One can set the quantity through an input field


Expected design

Because I mainly teach servlets, I expect students to design the application in a very simple way e.g.:

Mapping Description Forward to


Display the product detail page



Add one to the quantity for product x



Display the products list page



Add one to the quantity for product x



Display the checkout page



Add one to the quantity for product x



Remove one to the quantity for product x



Set the quantity to y for product x


This is more or less what students designed. Most of them also implemented both display and action via servlets.

It’s also what I probably would have done in such a simple project. I’d have coped with redundancy through delegated classes injected in the relevant servlets.

Another implementation with Filters

Filters are part of the Java EE API since Servlet 2.3:

A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.

Examples that have been identified for this design are:

  • Authentication Filters
  • Logging and Auditing Filters
  • Image conversion Filters
  • Data compression Filters
  • Encryption Filters
  • Tokenizing Filters
  • Filters that trigger resource access events
  • XSL/T filters
  • Mime-type chain Filter
— Javadoc

The gist of filters is that they are used for pre- or post-processing. While there’s nothing fundamentally hard about filter, only a few programmers use them (right). Thus, I was surprised at first when one group of students used filters for actions, and servlets for display i.e:

public class ProductsPage extends HttpServlet { ... }

public class ProductPage extends HttpServlet { ... }

public class CheckoutPage extends HttpServlet { ... }

@WebFilter(urlPatterns = {"/products/add", "/product/add", "/checkout/add"})
public class AddProduct implements Filter { ... }

@WebFilter(urlPatterns = {"/products/remove", "/product/remove", "/checkout/remove"})
public class RemoveProduct implements Filter { ... }

@WebFilter(urlPatterns = {"/products/set", "/product/set", "/checkout/set"})
public class SetProductQuantity implements Filter { ... }


At first, I was a bit surprised, or even questioning this design. However, now, I think this is brilliant, a creative use of filters: assign servlets to display, and filters to actions.

Of course, this is not technology specific: it applies to other frameworks, or tech stacks, the only requirement is that it allows for Filters.

And all of this, thanks to a creative group of students. Teaching is its own reward.

Nicolas Fränkel

Nicolas Fränkel

Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Also double as a trainer and triples as a book author.

Read More
A creative use of Filters
Share this