Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro

email article
printer friendly

Almost All Java Web Apps Need Model 2
The design model you first learned with JSP might not scale or support complexity.
by Budi Kurniawan

Posted June 11, 2002

Java developers generally use two design models in Web applications, simply called Model 1 and Model 2. Both are in use today, but Model 2 is recommended for medium- and large-sized applications or applications that will grow in complexity in the future. I'll discuss the Model 2 architecture and the Model-View-Controller design pattern on which Model 2 is based. I'll also explain why Model 2 is the recommended design and show you a sample application that implements this model.

You use Model 1, which is page-centric, for simple applications or if you want to get something done quickly. Applications implementing this model have a series of JSP pages where the user proceeds from one page to another. This is the model you always employ when you first learn JSP because it's simple and easy. The main problem with Model 1 applications is that they're hard to maintain and not flexible. In addition, this architecture does not promote the division of labor between the page designer and the Web developer because the developer is involved in both the page development and business objects coding.

Model 2 is the recommended architecture for large applications. It is based on the Model-View-Controller (MVC) pattern, which was the central concept behind the Smalltalk-80 user interface. At that time the term "pattern" was not yet used. Instead, it was called the MVC paradigm.

The MVC pattern consists of three kinds of main classes: Model, View, and Controller. The Model represents the application object or data. The View is the display of the model, and the Controller takes care of the user interface interaction with the user input. Prior to the MVC pattern, these three parts existed in one class, making the application inflexible and difficult to reuse.

The MVC pattern loosens the coupling between views and models. The model simply encapsulates the application object—it does not know anything about the view. The view, on the other hand, is the visual representation of the model and depends on the model. The view has a reference to the instance of the model, and so does the controller.


 
Figure 1. The Model 2 Architecture

To understand the MVC pattern better, consider a Microsoft Excel spreadsheet in which the data can be represented using a pie chart, a line diagram, and so on. The pie chart and the line diagram are the views visualizing the same piece of data. The controller gives commands to both the model and the view according to the user input.

Unlike the relationship between the model and the view, the view and the controller have a more intimate relationship. A view is normally passed as a reference to the controller object and can access some of the variables in the controller.

If you want to know more about this pattern, look at the paper written by Steve Burbeck, Ph.D., "Applications Programming in Smalltalk-80: How to use Model-View-Controller (MVC)."

Now, back to the Model 2 architecture, we have a servlet acting as the controller in the MVC pattern (see Figure 1). JSP pages are normally employed as views of the applications. As the model, we use a JavaBean that encapsulates the business logic.

Back to top

Printer-Friendly Version












Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home