Front_controller

Front controller

Front controller

Add article description


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.

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:

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

More information Controller, Dispatcher ...

Java implementation example

The front controller implemented in Java code:[3]

private void doProcess(HttpServletRequest request,
                       HttpServletResponse response)
    throws IOException, ServletException {
    ...
    try {
        getRequestProcessor().processRequest(request);
        getScreenFlowManager().forwardToNextScreen(request, response);
    } catch (Throwable ex) {
        String className = ex.getClass().getName();
        nextScreen = getScreenFlowManager().getExceptionScreen(ex);
        // Put the exception in the request
        request.setAttribute("javax.servlet.jsp.jspException", ex);
        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.

More information Page Controller ...

See also


References

  1. Fowler, Martin. "Front Controller". Retrieved September 26, 2017.
  2. "Web MVC framework". Spring Framework Reference Documentation. Pivotal Software. Retrieved September 26, 2017.
  3. "Demo code in Java". Archived from the original on 2012-04-19.{{cite web}}: CS1 maint: bot: original URL status unknown (link)

Notes


Share this article:

This article uses material from the Wikipedia article Front_controller, and is written by contributors. Text is available under a CC BY-SA 4.0 International License; additional terms may apply. Images, videos and audio are available under their respective licenses.