JSP model 2 architecture
![]() | This article includes a list of references, related reading, or external links, but its sources remain unclear because it lacks inline citations. (September 2008) |
In the design of Java Web applications, there are two commonly-used design models, referred to as Model 1 and Model 2.
Model 1 is simpler and only recommended for small applications. Model 2 is recommended for medium- and large-sized applications.
Model 2 uses the Model-View-Controller (MVC) design pattern to separate presentation from content.
In a Model 2 application, requests from the client browser are passed to the controller, which is a servlet. The controller decides which view (JSP) it will pass the request to. The view then invokes methods in a JavaBean (which may access a database) and returns the Response object to the Web container, which is then passed on to the client browser.
History
In 1998, Sun Microsystems published a pre-release of the JavaServer Pages specification, version 0.92. In this specification, Sun laid out two methods by which JSP pages could be used. The first model (referred to as "model 1" due to its ordering in the document) was a simplistic model whereby JSP pages were standalone, disjointed entities. Logic could be contained within the page itself, and navigation between pages was typically achieved by way of hyperlinks. This fit with the then-common usage of template technology.
Examples of similar systems that implemented the first model were ColdFusion and Active Server Pages.
The second model referred to by the document ("model 2" in the ordering) was an improved method that combined servlet technology with JSP technology. The specific difference listed was that a servlet would intercept the request, place the content to render into a request attribute (typically represented by a JavaBean), then call a JSP to render the content in the desired output format. This model differed from the previous model in the fact that JSP technology was used as a pure template engine. All of the logic was separated out into a servlet, leaving the JSP with the sole responsibility of rendering the output for the content provided.
In December of 1999, JavaWorld published an article by Govind Seshadri entitled Understanding JavaServer Pages Model 2 architecture. In this article, Govind accomplished two major milestones in the use of the term "Model 2". The first milestone was to formalize the term "Model 2" as an architectural pattern rather than one of two possible options. The second milestone was the claim that Model 2 provided an MVC architecture for web-based software.
Govind believed that because "Model 2" architecture separated the logic out of the JSP and placed it in a servlet, the two pieces could be seen as the "View" and the "Controller" (respectively) in an MVC architecture. The "Model" part of the MVC architecture was left open by Govind, with a suggestion that nearly any data-structure could meet the requirements. The specific example used in the article was a Vector list stored in the user's session.
In March of 2000, the Apache Struts project was released. This project formalized the division between View and Controller and claimed implementation the "Model 2" pattern.
See also
- Apache Struts is an open-source framework for implementing web-applications based on a Model 2 architecture.
External links
- JSP 0.92 Specification
- How Struts Implements Model 2 (The Origins of Model 1/Model 2)
- Understanding JavaServer Pages Model 2 architecture by Govind Seshadri (JavaWorld)
- Brian's waste of time - a history of MVC, including Model 2.
- ASP.NET Presentation Patterns - In this article Dino Esposito discusses how Model2 is also used in ASP.NET MVC.