Enterprise web programming

Modules ENTWA (Level 6) and APSW (Level 7)

Structuring web applications

What is MVC?

The Model View Controller (MVC) pattern is a way of organising an application (not necessarily a web application) so that different aspects of it are kept separate. This is a good thing because:

  1. It is good software engineering practice to maintain separation of concerns.
  2. An application might have more than one user interface (for example users might be able to access either as a windows application and a web application) and the elements of the user interface need to be kept separate from the parts that are common to each.
  3. Different developers (with different skills) may be responsible for different aspects of the application. Frequently, user-interface designers will be responsible for the view while conventional programmers may be responsible for the controller and model. In some circumstances, the controller might be the responsibility of an interaction engineer (or team).
Model

The model represents enterprise data and the business rules that govern access to and updates of this data. Often the model serves as a software approximation to a real-world process, so simple real-world modeling techniques apply when defining the model.

View The view renders the contents of a model. It accesses enterprise data through the model and specifies how that data should be presented. It is the view's responsibility to maintain consistency in its presentation when the model changes. This can be achieved by using a push model, where the view registers itself with the model for change notifications, or a pull model, where the view is responsible for calling the model when it needs to retrieve the most current data.
Controller The controller translates interactions with the view into actions to be performed by the model. In a stand-alone GUI client, user interactions could be button clicks or menu selections, whereas in a Web application, they appear as GET and POST HTTP requests. The actions performed by the model include activating business processes or changing the state of the model. Based on the user interactions and the outcome of the model actions, the controller responds by selecting an appropriate view.

(From Java BluePrints http://java.sun.com/blueprints/patterns/MVC-detailed.html)

Implementing MVC in Java web applications

There are generally held to be three ways of implementing Java web applications. See:

Type 1 All code is in JSP pages
Type 1.5 Most business logic code is in a JavaBean (or several JavaBeans) that your JSP page uses
Type 2 Very little code in JSP pages. Servlet used as a controller that access classes (including JavaBeans) that provide the business logic.

The following diagrams (from http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html and Mahmoud) show models 1 and 2.

Type 1.5 Type 2
JSP Model 1 JSP Model 2

In this course, we will concentrate on Type 2, which is an example of the MVC pattern.

Two examples of this are available. The first (in webapp1) describes a simple application "fruit". The second illustrates a simple project ideas system.

An article Scriptless JSP Pages: The Front Man by Bear Bibeault explores this topic in more depth.

Application layers

Another way of looking at an application is as a set of tiers or layers. The most common partition is to regard the application as made up of 3 tiers:

tiersfrom http://www.javaworld.com/javaworld/jw-01-2006/jw-0130-pojo.html

  1. the presentation tier - this could be regarded as the view and controller elements of MVC
  2. the business (or application or service) tier - provides the implementation of the business tasks to be performed by the application
  3. the persistence tier - responsible for fetching/storing data to/from a database or other storage facility

Each tier should be independent and should not expose dependencies related to the implementation Unconnected tiers should not communicate.

Architecture of layers

So how does a Java web application map into these structures? Typically, a Java web application will have 5 sets of classes:

  1. the JSP files that constitute the view
  2. the servlets that constitute the controller
  3. the classes that implement the business tasks
  4. the classes that represent the business objects
  5. the classes responsible for persisting data

Figure 1 below shows how they would combine to make a web server application that interacts with the user (HTTP from their browser) and a database (via JDBC typically). Figure 2 shows a variant of this where the model is separated off into a separate application. The controller would access the business layer operations through Java RMI or a similar protocol. This means that the presentation and business layers could be physically located on separate servers.

Single server Separate servers

(Aside) What is a JavaBean?

A JavaBean is a Java class that adheres to the following pattern:

There are a number of tutorials on JavaBeans at http://java.sun.com/products/javabeans/learning/tutorial/index.html. Although many of these concentrate on the user-interface uses of Beans, in web application programming we tend to concentrate on invisible Beans.

 

Last updated by Prof Jim Briggs of the School of Computing at the University of Portsmouth

 
The enterprise web programming modules include some material that was formerly part of the WEB1P and WEB2P units.