Monday 22 September 2014

Servlets as First-Class Citizens - MVC Pattern Without a Web Framework

The MVC pattern is the idea of separating the implementation of a user interface into three parts. The Model, the View, and the Controller. This separation helps avoid spaghetti code that can arise from trying to write a single entity which represents all of these roles at once, or splitting the roles unthinkingly among various entities.


  • The Model is the data being held by the system. Data that the user might want to add to, remove, edit etc.
  • The View is essentially the part that the user sees and interacts with. The View controls how the application looks, and exposes the Model and functionality to the user. 
  • If the user interacts with the View, any interactions are delegated to the Controller. The Controller must update the Model with the changes that the user is requesting, and get the View to update itself based on the updated Model.

The Model: Pojos

In many cases there are two models: that which is used by the client-side code, and the data that is held in the application database. It is the Controller's job to get data from the database and reformat it to better match the particular View that is requested. This adaptation of the Model can be used to simplify the logic which generates the View. It also allows the design of User Interface to be more separate from the design of the back-end storage, since data from many tables can be fetched efficiently and combined into a single Model per interface.

The View: JSP 

The View can be generated using JSPs.  Using JSTL tags, data from the Model can be used to render an HTML page or fragment that exposes the data to the user and controls how it is displayed including executing logic that controls the display of role-restricted elements. Once generated, the View should be essentially dumb, and not contain JavaScript code or in-line event listeners.

The Controller: Servlets + JavaScript

Due to the nature of the web, the Controller must be split between the server and the client. Client-side controller code is written in JavaScript and is responsible for handling events generated by the browser as the user interacts with the client-side interface, and calling the server-side code. The server-side controller code will be split into various components responsible for performing updates to the database, and fetching data to be passed back to the View.

Many frameworks (sometimes called action-based frameworks) use a front-controller which is a Servlet or Filter that handles all incoming requests and directs them to Java classes which represent either actions or views. The mapping between the URL and the action is then controlled by the framework, which must read this information from somewhere such as configuration files, a database table or annotations on the action classes themselves.

With Servlet Annotations introduced in Servlet 3.0, this front-controller is no longer needed since annotations can be added to the Servlets. Therefore in a framework-free application, Servlets become first-class citizens once more, mapping themselves to URLs that can be called by the client-side controller code.

On the client-side, often JavaScript libraries such as jQuery are used to set up event listeners which bind the application controller code to the native browser components. In modern browsers, these libraries are becoming unnecessary since the Event API of HTML has been standardized to allow easy binding of event listeners, and native methods have been added to the HTML DOM API to allow fast searching of the DOM tree for the components on which to listen for events.



No comments:

Post a Comment