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
- 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 |
|
|
Display the products list page |
|
|
Add one to the quantity for product |
|
|
Display the checkout page |
|
|
Add one to the quantity for product |
|
|
Remove one to the quantity for product |
|
|
Set the quantity to |
|
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
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.