Java Omnibus
OOAD & Architecture / J2EE / Jakarta EE Patterns / Front Controller Pattern
J2EE Presentation Java Order Management
Presentation

Front Controller Pattern

Front Controller centralizes request handling so authentication, routing, localization, error handling, and shared web concerns do not get duplicated across many entry points.

Why This Topic Matters

Why it matters in enterprise Java

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.

Pattern Essentials

Intent, problem, and solution

Intent

What the pattern is trying to achieve

Provide a single handler for incoming requests that applies shared web concerns before delegating to specific handlers.

Problem

What goes wrong without it

Without a central entry point, routing and cross-cutting web behavior get scattered across many controllers or servlets.

Solution

How the pattern answers the problem

Introduce one front controller that receives requests, applies shared logic, and dispatches to the right command or handler.

Modern Relevance

Why it still matters now

In modern Java, the Front Controller idea lives on in DispatcherServlet-like infrastructure, API gateways, and centralized route dispatch layers.

Structure

How the collaboration works

  • Client sends all web requests to one central endpoint.
  • The front controller resolves handler selection and shared policies.
  • Specialized handlers or controllers perform the specific use case work.
  • Rendering or API serialization happens after delegation.
Java Example

Java example in the Order Management domain

Java example: central request dispatch for order operations
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);
    }
}
Code Walkthrough

Step by step

  1. All requests arrive through one servlet entry point.
  2. Handler resolution happens centrally instead of being repeated in multiple places.
  3. Shared metadata such as correlation IDs can be attached once.
  4. Concrete handlers stay focused on the specific use case.
Real-World Usage

Where this pattern shows up

  • Spring MVC dispatcher flows
  • Central API gateways inside monoliths
  • Server-rendered applications with shared request rules
When Not To Use

Cases where another shape is better

  • Tiny applications where one or two endpoints do not justify extra routing indirection
  • Systems where a framework already provides a well-structured central dispatcher
Trade-Offs

Trade-offs and design pressure

  • A front controller can become a giant switch statement if handler dispatch stays too manual.
  • Over-centralization can make debugging harder if the entry layer owns too much behavior.
Comparison Note

How this differs from nearby patterns

Front Controller is about one central entry point. Intercepting Filter is about a reusable processing pipeline before or around that entry point.

Common Misuse

Mistakes to avoid

  • Putting business orchestration directly into the front controller
  • Letting the front controller grow into a god object for the whole web layer
  • Using it as a place for every special case instead of delegating cleanly
Related Pages

Related concepts


What To Read Next