Skip to main content

JSP Model 1, Model 2 Architectures and MVC Pattern


JSP Model 1, Model 2 Architectures and MVC Pattern
The early JSP specifications presented two approaches for building web applications using JSP technology. These two approaches were the JSP Model 1 and Model 2 architectures. The two JSP architectures differ in several key areas.
The major difference is in how and by which component the processing of a request is handled. With the Model 1 architecture, the JSP page handles all of the processing of the request and is responsible for displaying the output to the client. There is no extra servlet involved in the process. The client request is sent directly to a JSP page, which may communicate with JavaBeans or other services, but ultimately the JSP page selects the next page for the client. The next view is determined based on either the JSP selected or parameters within the client's request. In contrast, in the Model 2 architecture, the client request is first intercepted by a servlet, referred to as a controller servlet. This servlet handles the initial processing of the request and determines which JSP page to display next.A client never sends a request directly to a JSP page in the Model 2 architecture. This allows the servlet to perform front-end processing, including authentication and authorization, centralized logging, and help with internationalization. Once request processing has completed, the servlet directs the request to the appropriate JSP page. How the next page is determined varies widely across different applications. The main difference between the two approaches is that the Model 2 architecture introduces a controller servlet that provides a single point of entry and encourages more reuse and extensibility than the Model 1 approach. With the Model 2 architecture, there is a clear separation of the business logic, presentation output, and request processing. This separation often is referred to as a Model-View-Controller (MVC) pattern.

While the Model 2 architecture might seem overly complicated, it actually can simplify an application greatly. Web applications built using the Model 2 approach generally are easier to maintain and can be more extensible than comparable applicationsbuilt around the Model 1 architecture.

Importance of Model-View-Controller

The MVC architectural pattern is not directly related to web applications. In fact, it's quite common in Smalltalk applications, which generally have nothing to do with the Web.
The Model 2 approach is concerned with separating responsibilities in web applications. Allowing a JSP page to handle the responsibilities of receiving the request, executing some business logic, and then determining the next view to display can make for an unattractive JSP page, not to mention the maintenance and extensibility problems this entanglement causes. Application development and maintenance are much easier if the different components of a web application have clear and distinct responsibilities.

The MVC Model
Depending on the type of architecture your application uses, the model portion of the MVC pattern can take many different forms. In a two-tier application, where the web tier interacts directly with a data store such as a database, the model classes may be a set of regular Java objects. These objects may be populated manually from a ResultSet returned by a database query, or they may be instantiated and populated automatically by an object-to-relational mapping (ORM) framework such as TopLink or CocoBase.

The MVC View
The views within the web tier MVC pattern typically consist of HTML and JSP pages. HTML pages are used to serve static content, while JSP pages can be used to serve both static and dynamic content.
Most dynamic content is generated in the web tier. However, some applications may require clientside JavaScript. This does not interfere with or infringe upon the MVC concept.
HTML and JSP are not the only choice for the view. You easily can support WML, for example,
instead of HTML. Because the view is decoupled from the model, you can support multiple views, each for a different client type, using the same model components.

The MVC Controller
The controller portion of the web tier MVC design generally is a Java servlet. The controller in a web tier application performs the following duties:

  • Intercepts HTTP requests from a client
  • Translates each request into a specific business operation to perform
  • Either invokes the business operation itself or delegates it to a handler
  • Helps to select the next view to display to the client
  • Returns the view to the client

    The Front Controller pattern describes how a web tier controller should be implemented. Because all client requests and responses go through the controller, there is a centralized point of control for the web application. This helps when adding new functionality. Code that would normally need to be put in every JSP page can be put in the controller servlet, which processes all the requests. The controller also helps to decouple the presentation components (views) from the business operations, further aiding development.

    Useful Links : Sun's Patterns Catalog Front Controller Pattern

  • Here is a graphical repersentation of a typical struts based application.

    Click to view larger image


    Comments

    Anonymous said…
    Thanks for clarifying things with your post. You make it very obvious why Model 2/MVC is preferred to Model 1 and its dependence on monolithic JSPs.

    I do have one question, however. In your illustration, what is the difference between the "Business Logic" module and the "Model" module?

    Regards.

    Popular posts from this blog

    Installing Bugzilla on Windows

    Bugzilla is the Bugs/Issues Tracking Tool from The Mozilla Organization. Version 2.18 is the latest stable release. There are couple of resources which guide a User installing Bugzilla on a Unix/linux machine. However, this entry describes a way to install Bugzilla on a Windows machine (W2K to be precise). This document guides you step by step through the installation process. First, get Administrative access to the machine on which you want to install Bugzilla. It should be a simple step. Usually, Users are given Administrative rights on Windows machine. However, if you dont have, contact your Administrator. Get Bugzilla Then download the Bugzilla from http://bugzilla.org/download.html . There are two ways of gettng it - through CVS or direct downloading the tar file. Remember there are no Zip files. However, any zip utility should be able to untar the Bugzilla. I download the tar file and untarred it using WinZip. I placed the untarred 'bugzilla' directory in my c: drive. So...

    JSP Performance Tips

    Here are a few tips to improve JSP performance that I have been using. If you have your own listing of the tips, plz share. Disable JSP auto reloading feature. Use thread pool for your JSP engine and define the size of thread pool as per application requirement. Use jspInit() method to cache static data Initialize the 'out' object (implicit object) with proper size in the page directive. Set the content length Give 'false' value to the session in the page directive to avoid session object creation. Flush the data partly Use StringBuffer rather than using + operator when you concatenate multiple strings Use include directive instead of include action when you want to include the child page content in the translation phase. Avoid giving unnecessary scope in the 'useBean' action. Use print() method rather than println() method Use ServletOutputStream instead of JSPWriter to send binary data Minimize code in the synchronized block Do not use custom tags if you do no...

    How old are the daughters?

    Two MIT math graduates bump into each other at Fairway on the upper west side. They hadn't seen each other in over 20 years. The first grad says to the second: "how have you been?" Second: "Great! I got married and I have three daughters now" First: "Really? how old are they?" Second: "Well, the product of their ages is 72, and the sum of their ages is the same as the number on that building over there.." First: "Right, ok.. oh wait.. hmmmm.., I still don't know" second: "Oh sorry, the oldest one just started to play the piano" First: "Wonderful! my oldest is the same age!" Problem: How old are the daughters?