/ FILTERS

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

      checkout

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

/product?id=x

Display the product detail page

/product.jsp

/product/add?id=x

Add one to the quantity for product x

/product?id=x

/products

Display the products list page

/products.jsp

/products/add?id=x

Add one to the quantity for product x

/products

/checkout

Display the checkout page

/checkout.jsp

/checkout/add?id=x

Add one to the quantity for product x

/checkout

/checkout/remove?id=x

Remove one to the quantity for product x

/checkout

/checkout/set?id=x&qty=y

Set the quantity to y for product x

/checkout

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
https://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html

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:

@WebServlet("/products/*")
public class ProductsPage extends HttpServlet { ... }

@WebServlet("/product/*")
public class ProductPage extends HttpServlet { ... }

@WebServlet("/checkout/*")
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 { ... }

Conclusion

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

Nicolas Fränkel is a technologist focusing on cloud-native technologies, DevOps, CI/CD pipelines, and system observability. His focus revolves around creating technical content, delivering talks, and engaging with developer communities to promote the adoption of modern software practices. With a strong background in software, he has worked extensively with the JVM, applying his expertise across various industries. In addition to his technical work, he is the author of several books and regularly shares insights through his blog and open-source contributions.

Read More
A creative use of Filters
Share this