Front controller

The front controller software design pattern is listed in several pattern catalogs and related to the design of Web applications. It is "a controller that handles all requests for a Web site",[1] which is a useful structure for Web application developers to achieve the 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 make the individual pages responsible for navigation.

The front controller may be implemented as a Java object, or as a script in a script language like PHP, Python or Ruby that is called on every request of a Web session. This script, for example an index.php, 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 particular task(s) required.

The alternative to a front controller would be individual scripts like login.php and order.php that would each then satisfy the type of request. Each script would have to duplicate code or objects that are common to all tasks. However, each script might also have more flexibility to implement the particular task required.

Examples

Several Web-tier application frameworks implement the front controller pattern, among them:

Implementation

To better understand front controller pattern, there is an example to implement front controller in Java.[3] It can be defined in 3 components:

  1. XML Mapping: files that map requests to the class that will handle the request processing.
  2. Request Processor: used for dealing with the request processing (and modifying or retrieving the appropriate model).
  3. Flow Manager: first get the request and the output of the processing, then determine what will show 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 initiate contact retrieval. Dispatchers can be used for navigation and managing the view output. Users will receive next view that is determined by the dispatcher. Dispatchers are also flexible: they can be encapsulated within the controller directly or separated to another component. The dispatcher provides a static view along with the dynamic mechanism.

It also uses the RequestDispatcher object (supported in the servlet specification) and encapsulates some additional processing.

A helper helps view or controller to process. Thus helper can achieve various goals.

At view side, the helper collects data and sometimes stores data as an intermediate station. Before view's process, helpers serve to adapt the data model for it. Helpers do certain pre-processes such as formatting the data to Web content or providing direct access to the raw data. Multiple helpers can collaborate with one view for most conditions. They are implemented as JavaBeans components in JSP 1.0+ and custom tags in JSP 1.1+. Additionally, a helper also works as a transformer that is used to adapt and convert the model into the suitable format.

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

Demo implementation in Java

Here is part of a demo code to implement front controller.[4]

 1 private void doProcess(HttpServletRequest request,
 2                        HttpServletResponse response)
 3    throws IOException, ServletException {
 4     ...
 5    try {
 6       getRequestProcessor().processRequest(request);
 7 
 8         getScreenFlowManager().forwardToNextScreen(request, response);
 9     } catch (Throwable ex) {
10       String className = ex.getClass().getName();
11       nextScreen = getScreenFlowManager().getExceptionScreen(ex);
12       // put the exception in the request
13       request.setAttribute("javax.servlet.jsp.jspException", ex);
14       if (nextScreen == null) {
15          // send to general error screen
16          ex.printStackTrace();
17          throw new ServletException("MainServlet: unknown exception: " +
18             className);
19       }
20    }

Benefits and liabilities

There are three benefits for using front controller pattern.[5]

In terms of liability, front controllers that determine the following activities by searching the database or XML documents, performance might be decreased. And implementation of front controller to existed systems always involving replacing the current ones, which makes it harder for beginners to start with.

Relationship with MVC pattern

  1. In order to improve system reliability and maintainability, duplicated codes should be avoided and centralized when they are of the same common logic through the whole system.
  2. The data for the application is better to be handled in one location, thus there will be no need to duplicate database retrieval code.
  3. Different roles in the MVC pattern should be separated to increase testability, which is also true for controller part in the MVC pattern.

Comparison

Page controller is an alternative to front controller in MVC model.

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

See also

References

Notes

External links

This article is issued from Wikipedia - version of the 12/1/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.