Enterprise web programmingModules ENTWA (Level 6) and APSW (Level 7) |
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:
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)
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 |
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.
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:
from http://www.javaworld.com/javaworld/jw-01-2006/jw-0130-pojo.html
Each tier should be independent and should not expose dependencies related to the implementation Unconnected tiers should not communicate.
So how does a Java web application map into these structures? Typically, a Java web application will have 5 sets of classes:
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.
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. |