*** Welcome to piglix ***

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", which is a useful structure for Web application developers to achieve the flexibility and reuse without code redundancy.

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.

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

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

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

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.


...
Wikipedia

...