4.5 Front Controller
The Front Controller pattern gives the whole application a single request entry point. All requests funnel into this one controller, which centrally performs common logic — authentication, logging, internationalization, exception handling — then dispatches to specific handlers.
Without it, this common logic gets copied into every page/handler — repetitive and easy to miss (e.g., one page forgets to add auth). The lab below lets you pick a request path and watch the front controller do common handling first, then dispatch to the matching view.
Controller + Dispatcher
A front controller usually pairs with a Dispatcher:
- Front Controller: the single entry, doing common pre-processing.
- Dispatcher: routes control to the right view or handler based on the request.
class FrontController {
private Dispatcher dispatcher = new Dispatcher();
public void handle(Request req) {
authenticate(req); // common logic written once
log(req);
dispatcher.dispatch(req); // then route to the specific handler
}
}The lab below shows a centralized route table: all "path → handler" mappings are maintained in one place, and a new route changes only this table.
You use it every day
The front controller is a cornerstone of modern web frameworks, just hidden behind them:
- Spring MVC's
DispatcherServlet— the name spells out "dispatcher."
- The central router / entry file of nearly every web framework (Express's app, Django's URL dispatch, PHP's single
index.phpentry).
Precisely because there's a unified entry, a framework can insert auth middleware, global exception handling, and request logging in one place. Understand the front controller and you understand "why all requests pass through that one framework gate first."