Front Controller Design Pattern in Java
Learn Front Controller Design Pattern in Java with Spring DispatcherServlet, centralized request handling, authentication, logging, routing, enterprise architecture, UML diagrams, code examples, and interview questions.
What You Will Learn
- What is Front Controller Pattern?
- Why Front Controller is Needed
- Request Processing Flow
- Java Implementation
- Spring DispatcherServlet
- Authentication and Logging
- Enterprise Use Cases
- Benefits and Limitations
- Interview Questions
Introduction
Every enterprise application receives thousands of requests.
Examples:
GET /customers
GET /accounts
POST /orders
POST /payments
Without a Front Controller:
CustomerServlet
OrderServlet
PaymentServlet
AccountServlet
Each servlet handles:
Authentication
Authorization
Logging
Validation
Error Handling
Result:
Duplicate Code
Maintenance Issues
Inconsistent Processing
Front Controller solves this problem.
What is Front Controller Pattern?
Front Controller is an Architectural Pattern that provides a centralized entry point for all incoming requests.
Instead of:
Client → Many Controllers
Use:
Client
↓
Front Controller
↓
Controllers
Purpose of Front Controller
Primary Goal:
Centralize Request Handling
Common responsibilities:
Authentication
Authorization
Logging
Routing
Validation
Exception Handling
Real World Analogy
Airport Security.
Passengers:
Passenger A
Passenger B
Passenger C
All must pass through:
Security Checkpoint
before reaching gates.
Security checkpoint acts as Front Controller.
Problem Without Front Controller
flowchart LR
A[Client]
B[Customer Controller]
C[Order Controller]
D[Payment Controller]
A --> B
A --> C
A --> D
Every controller duplicates common logic.
Solution With Front Controller
flowchart LR
A[Client]
B[Front Controller]
C[Customer Controller]
D[Order Controller]
E[Payment Controller]
A --> B
B --> C
B --> D
B --> E
All requests pass through one place.
Front Controller Architecture
flowchart LR
A[Client Request]
B[Front Controller]
C[Authentication]
D[Routing]
E[Controller]
A --> B
B --> C
C --> D
D --> E
Responsibilities
Authentication
Verify user identity.
Authorization
Verify permissions.
Logging
Track request details.
Routing
Forward request to correct controller.
Error Handling
Handle exceptions consistently.
UML Diagram
classDiagram
class FrontController {
+handleRequest()
}
class Dispatcher
class Controller
FrontController --> Dispatcher
Dispatcher --> Controller
Request Lifecycle
flowchart LR
A[Browser]
B[Front Controller]
C[Authentication]
D[Authorization]
E[Controller]
F[Response]
A --> B
B --> C
C --> D
D --> E
E --> F
Java Example
Step 1: Controller Interface
public interface Controller {
void process();
}
Step 2: Customer Controller
public class CustomerController
implements Controller {
@Override
public void process() {
System.out.println(
"Customer Request Processed");
}
}
Step 3: Order Controller
public class OrderController
implements Controller {
@Override
public void process() {
System.out.println(
"Order Request Processed");
}
}
Step 4: Dispatcher
public class Dispatcher {
public void dispatch(
String request) {
if("CUSTOMER".equals(request)) {
new CustomerController()
.process();
}
else if("ORDER".equals(request)) {
new OrderController()
.process();
}
}
}
Step 5: Front Controller
public class FrontController {
private Dispatcher dispatcher =
new Dispatcher();
public void handleRequest(
String request) {
authenticate();
logRequest(request);
dispatcher.dispatch(request);
}
private void authenticate() {
System.out.println(
"Authentication Success");
}
private void logRequest(
String request) {
System.out.println(
"Request: " + request);
}
}
Step 6: Client
public class FrontControllerDemo {
public static void main(
String[] args) {
FrontController controller =
new FrontController();
controller.handleRequest(
"CUSTOMER");
controller.handleRequest(
"ORDER");
}
}
Output
Authentication Success
Request: CUSTOMER
Customer Request Processed
Authentication Success
Request: ORDER
Order Request Processed
Execution Flow
sequenceDiagram
Client->>FrontController: Request
FrontController->>Authentication: Validate
Authentication-->>FrontController: Success
FrontController->>Dispatcher: Route
Dispatcher->>Controller: Execute
Controller-->>Client: Response
Banking Example
Requests:
/account
/transfer
/statement
/loan
All requests pass through:
BankFrontController
Banking Architecture
flowchart LR
A[Customer]
B[Bank Front Controller]
C[Account Controller]
D[Transfer Controller]
E[Loan Controller]
A --> B
B --> C
B --> D
B --> E
Insurance Example
Requests:
/create-policy
/claim
/renew-policy
Common processing:
Authentication
Validation
Audit Logging
Insurance Flow
flowchart LR
A[User]
B[Insurance Front Controller]
C[Policy Controller]
D[Claim Controller]
A --> B
B --> C
B --> D
Spring MVC Example
The most famous Front Controller implementation:
DispatcherServlet
Spring MVC Architecture
flowchart LR
A[Browser]
B[DispatcherServlet]
C[Controller]
D[Service]
E[Repository]
F[Database]
A --> B
B --> C
C --> D
D --> E
E --> F
DispatcherServlet Responsibilities
Receive Request
Find Controller
Invoke Controller
Resolve View
Return Response
Spring Request Flow
flowchart LR
A[Client]
B[DispatcherServlet]
C[HandlerMapping]
D[Controller]
E[ViewResolver]
A --> B
B --> C
C --> D
D --> E
API Gateway Relation
In Microservices:
API Gateway
acts like a Front Controller.
Responsibilities:
Authentication
Routing
Rate Limiting
Monitoring
API Gateway Flow
flowchart LR
A[Client]
B[API Gateway]
C[User Service]
D[Order Service]
E[Payment Service]
A --> B
B --> C
B --> D
B --> E
Enterprise Examples
Banking
Internet Banking
Fund Transfer
Account Management
Insurance
Claims Processing
Policy Management
Retail
Order Processing
Inventory Management
Healthcare
Patient Management
Appointment Booking
Framework Examples
Common Front Controller implementations:
Spring DispatcherServlet
Struts ActionServlet
JSF FacesServlet
API Gateway
Benefits
✅ Centralized Request Handling
✅ Reduced Code Duplication
✅ Consistent Security
✅ Easier Logging
✅ Better Monitoring
✅ Improved Maintainability
Limitations
❌ Single Point Of Failure
❌ Additional Processing Layer
❌ Can Become Complex
When To Use
Use Front Controller when:
- Building Web Applications
- Multiple Controllers Exist
- Common Processing Is Required
- Security Must Be Centralized
When Not To Use
Avoid when:
- Application Is Extremely Small
- No Shared Request Processing Exists
Front Controller vs MVC
| Feature | Front Controller | MVC |
|---|---|---|
| Focus | Request Handling | Application Structure |
| Entry Point | Single | Multiple Controllers |
| Example | DispatcherServlet | Spring MVC |
Front Controller vs API Gateway
| Feature | Front Controller | API Gateway |
|---|---|---|
| Scope | Single Application | Multiple Services |
| Usage | Monolith | Microservices |
| Example | DispatcherServlet | Kong, Gloo, Apigee |
Real Enterprise Architecture
flowchart LR
A[Client]
B[Front Controller]
C[Security]
D[Routing]
E[Business Controller]
A --> B
B --> C
C --> D
D --> E
Interview Questions
What is Front Controller Pattern?
A centralized entry point that handles all incoming requests.
Why Use Front Controller?
To centralize authentication, logging, routing, and validation.
Spring Example?
DispatcherServlet
API Gateway Similarity?
API Gateway acts as Front Controller for microservices.
Main Benefit?
Reduced duplication and centralized request processing.
Key Takeaways
- Front Controller centralizes request handling.
- Commonly used in enterprise web applications.
- Spring DispatcherServlet is the best example.
- API Gateway is the microservices equivalent.
- Handles authentication, routing, logging, and validation.
- Improves maintainability and consistency.
- One of the most important enterprise architecture patterns.