Front controller

Short description: Controller that handles all requests for a website

This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Front controller" – news · newspapers · books · scholar · JSTOR (November 2022) (Learn how and when to remove this template message)

|

The front controller software design pattern is listed in several pattern catalogs and is related to the design of web applications. It is "a controller that handles all requests for a website,"[1] which is a useful structure for web application developers to achieve flexibility and reuse without code redundancy.

## Contents

* 1 Instruction
* 2 Examples
* 3 Implementation
* 3.1 Participants and responsibilities
* 3.2 Java implementation example
* 4 Benefits and liabilities
* 5 Relationship with MVC
* 6 Comparison
* 7 See also
* 8 References
* 9 Notes
* 10 External links

## Instruction

A typical front controller structure.

Front controllers are often used in web applications to implement workflows. While not strictly required, it is much easier to control navigation across a set of related pages (for instance, multiple pages used in an online purchase) from a front controller than it is to assign individual pages responsibility for navigation.

The front controller may be implemented as a Java object, or as a script in a scripting language such as PHP, Raku, Python or Ruby that is called for every request of a web session. This script would handle all tasks that are common to the application or the framework, such as session handling, caching and input filtering. Based on the specific request, it would then instantiate further objects and call methods to handle the required tasks.

The alternative to a front controller is the usage of page controllers mapped to each site page or path. Although this may cause each individual controller to contain duplicate code, the page-controller approach delivers a high degree of specialization.

## Examples

Several web-tier application frameworks implement the front controller pattern:

* Apache Struts
* ASP.NET MVC
* Cairngorm framework in Adobe Flex
* Cro or Bailador frameworks in Raku
* Drupal
* MVC frameworks written in PHP, such as Yii, CakePHP, Laravel, Symfony, CodeIgniter and Laminas
* Spring Framework[2]
* Yesod, written in Haskell

## Implementation

Front controllers may divided into three components:

1. XML mapping: files that map requests to the class that will handle the request processing.
2. Request processor: used for request processing and modifying or retrieving the appropriate model.
3. Flow manager: determines what will be shown on the next page.

### Participants and responsibilities

Controller  | Dispatcher  | Helper  | View

|
|
|

The controller is an entrance for users to handle requests in the system. It realizes authentication by playing the role of delegating helper or initiating contact retrieval.  | Dispatchers can be used for navigation and managing the view output. Users will receive the next view that is determined by the dispatcher. Dispatchers are also flexible; they can be encapsulated within the controller directly or separated into another component. The dispatcher provides a static view along with the dynamic mechanism.  | Helpers assist in the processing of views or controllers.

On the view side, the helper collects data and sometimes stores data as an intermediate station. Helpers do certain preprocesses such as formatting of the data to web content or providing direct access to the raw data. Multiple helpers can collaborate with one view for most conditions. Additionally, a helper works as a transformer that adapts and converts the model into a suitable format.

| With the collaboration of helpers, views display information to the client by processing data from a model. The view will display if the processing succeeds, and vice versa.

### Java implementation example

The front controller implemented in Java code:[3]

import java.io.IOException;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

private void doProcess(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// ...
try {
getRequestProcessor().processRequest(request);
getScreenFlowManager().forwardToNextScreen(request, response);
} catch (Throwable t) {
String className = ex.getClass().getName();
nextScreen = getScreenFlowManager().getExceptionScreen(t);
// Put the exception in the request
request.setAttribute("jakarta.servlet.jsp.JspException", t);
if (nextScreen == null) {
// Send to general error screen
ex.printStackTrace();
throw new ServletException("MainServlet: unknown exception: " + className);

## Benefits and liabilities

There are three primary benefits associated with the front controller pattern.[4]

* Centralized control. The front controller handles all the requests to the web application. This implementation of centralized control that avoids using multiple controllers is desirable for enforcing application-wide policies such as user tracking and security.
* Thread safety. A new command object arises when receiving a new request, and the command objects are not meant to be thread-safe. Thus, it will be safe in the command classes. Though safety is not guaranteed when threading issues are gathered, code that interacts with commands is still thread-safe.
* Configurability. As only one front controller is employed in a web application, the application configuration may be greatly simplified. Because the handler shares the responsibility of dispatching, new commands may be added without changes needed to the code.

The front controller pattern may incur performance issues because the single controller is performing a great deal of work, and handlers may introduce bottlenecks if they involve database or document queries. The front controller approach is also more complex than that of page controllers.

## Relationship with MVC

1. In order to improve system reliability and maintainability, duplicate code should be avoided and centralized when it involves common logic used throughout the system.
2. The data for the application is best handled in a single location, obviating the need for duplicate data-retrieval code.
3. Different roles in the model–view–controller (MVC) pattern should be separated to increase testability, which is also true for the controller part in the MVC pattern.

## Comparison

The page-controller pattern is an alternative to the front controller approach in the MVC model.

| Page Controller  | Front Controller

|
|

Base class  | A base class is needed and will grow simultaneously with the development of the application.  | The centralization of requests is easier to modify than is a base class.
Security  | Low security because various objects react differently without consistency.  | High, because the controller is implemented in a coordinated fashion.
Logical Page  | Single object on each logical page.  | Only one controller handles all requests.
Complexity  | Low  | High

## See also

* Software design pattern
* Mediator pattern (note: the front controller pattern is a specialized kind of mediator pattern)

## References

1. ↑ Fowler, Martin. "Front Controller". https://www.martinfowler.com/eaaCatalog/frontController.html.
2. ↑ "Web MVC framework". Pivotal Software. https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-servlet.
3. ↑ "Demo code in Java". http://java.sun.com/blueprints/patterns/FrontController.html.
4. ↑ "Benefits for using front controller". 17 March 2014. https://msdn.microsoft.com/en-us/library/ff648617.aspx.

## Notes

* Alur, Deepak; John Crup; Dan Malks (2003). Core J2EE Patterns, Best Practices and Design Strategies, 2nd Ed.. Sun Microsystems Press. pp. 650pp. ISBN 0-13-142246-4.
* Fowler, Martin (2003). Patterns of Enterprise Application Architecture. Addison-Wesley Professional. pp. 560pp. ISBN 978-0-321-12742-6. http://www.martinfowler.com/books.html#eaa.

## External links

* Bear Bibeault's Front Man™, a lightweight Java implementation.

* v
* t
* e

Software design patterns

Creational|

* Abstract factory
* Builder
* Dependency injection
* Factory method
* Lazy initialization
* Multiton
* Object pool
* Prototype
* RAII
* Singleton

Structural|

* Adapter
* Bridge
* Composite
* Decorator
* Delegation
* Facade
* Flyweight
* Front controller
* Marker interface
* Module
* Proxy
* Twin

Behavioral|

* Blackboard
* Chain of responsibility
* Command
* Interpreter
* Iterator
* Mediator
* Memento
* Null object
* Observer
* Servant
* Specification
* State
* Strategy
* Template method
* Visitor

Functional|

* Closure
* Currying
* Function composition
* Functor
* Monad
* Generator

Concurrency|

* Active object
* Actor
* Balking
* Barrier
* Binding properties
* Coroutine
* Compute kernel
* Double-checked locking
* Event-based asynchronous
* Fiber
* Futex
* Futures and promises
* Guarded suspension
* Immutable object
* Join
* Lock
* Messaging
* Monitor
* Nuclear
* Proactor
* Reactor
* Read write lock
* Scheduler
* Thread pool
* Thread-local storage

Architectural|

* ADR
* Active record
* Broker
* Client–server
* CBD
* DAO
* DTO
* DDD
* ECB
* ECS
* EDA
* Front controller
* Identity map
* Interceptor
* Implicit invocation
* Inversion of control
* Model 2
* MOM
* Microservices
* MVA
* MVC
* MVP
* MVVM
* Monolithic
* Multitier
* Naked objects
* ORB
* P2P
* Publish–subscribe
* PAC
* REST
* SOA
* Service locator
* SN
* SBA
* Specification

Cloud
Distributed|

* Ambassador
* Anti-Corruption Layer
* Bulkhead
* Cache-Aside
* Circuit Breaker
* CQRS
* Compensating Transaction
* Competing Consumers
* Compute Resource Consolidation
* Event Sourcing
* External Configuration Store
* Federated Identity
* Gatekeeper
* Index Table
* Leader Election
* MapReduce
* Materialized View
* Pipes
* Filters
* Priority Queue
* Publisher-Subscriber
* Queue-Based Load Leveling
* Retry
* Scheduler Agent Supervisor
* Sharding
* Sidecar
* Strangler
* Throttling
* Valet Key

Other|

* Business delegate
* Composite entity
* Intercepting filter
* Lazy loading
* Mangler
* Mock object
* Type tunnel
* Method chaining

Books|

* Design Patterns
* Enterprise Integration Patterns

People|

* Christopher Alexander
* Erich Gamma
* Ralph Johnson
* John Vlissides
* Grady Booch
* Kent Beck
* Ward Cunningham
* Martin Fowler
* Robert Martin
* Jim Coplien
* Douglas Schmidt
* Linda Rising

Communities|

* The Hillside Group
* The Portland Pattern Repository

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Front controller. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Front_controller&oldid=4227938"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template