What the pattern is trying to achieve
Provide a single handler for incoming requests that applies shared web concerns before delegating to specific handlers.
Front Controller centralizes request handling so authentication, routing, localization, error handling, and shared web concerns do not get duplicated across many entry points.
In an e-commerce system, checkout, account, order history, and admin routes all need shared request concerns. A central entry point keeps that policy consistent before individual handlers take over.
Provide a single handler for incoming requests that applies shared web concerns before delegating to specific handlers.
Without a central entry point, routing and cross-cutting web behavior get scattered across many controllers or servlets.
Introduce one front controller that receives requests, applies shared logic, and dispatches to the right command or handler.
In modern Java, the Front Controller idea lives on in DispatcherServlet-like infrastructure, API gateways, and centralized route dispatch layers.
package org.javaomnibus.ecommerce.web;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
@WebServlet("/*")
public final class CommerceFrontController extends HttpServlet {
private final Map<String, RequestHandler> handlers = Map.of(
"/orders", new OrderHistoryHandler(),
"/checkout", new CheckoutHandler()
);
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getRequestURI();
RequestHandler handler = handlers.get(path);
if (handler == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
request.setAttribute("correlationId", java.util.UUID.randomUUID().toString());
handler.handle(request, response);
}
}
Front Controller is about one central entry point. Intercepting Filter is about a reusable processing pipeline before or around that entry point.