MVC Architecture (Model 2 Architecture)
By: Baski
The Model 2 architecture for designing JSP pages is in reality, Model View Controller
(MVC) applied to web applications. Hence the two terms can be used interchangeably in the web world. MVC originated in SmallTalk and has since
made its way into Java community. Model 2 architecure and its derivatives are the cornerstones for all serious and industrial strength web applications designed
in the real world. Hence it is essential for you understand this paradigm thoroughly. Figure 1.2 shows the Model 2 (MVC) architecture.
The main difference between Model 1 and Model 2 is that in Model 2, a controller handles the user request instead of another JSP. The controller is
implemented as a Servlet. The following steps are executed when the user submits the request.
- The Controller Servlet handles the user’s request. (This means the hyperlink in the JSP should point to the controller servlet).
- The Controller Servlet then instantiates appropriate JavaBeans based on the request parameters (and optionally also based on session attributes).
- The Controller Servlet then by itself or through a controller helper communicates with the middle tier or directly to the database to fetch the required data.
- The Controller sets the resultant JavaBeans (either same or a new one) in one of the following contexts – request, session or application.
- The controller then dispatches the request to the next view based on the request URL.
- The View uses the resultant JavaBeans from Step 4 to display data. Note that there is no presentation logic in the JSP. The sole function of the
JSP in Model 2 architecture is to display the data from the JavaBeans set in the
request, session or application scopes.
Model 2 Architecture.
Advantages of Model 2 Architecture
Since there is no presentation logic in JSP, there are no scriptlets. This means
lesser nightmares. [Note that although Model 2 is directed towards elimination of
scriptlets, it does not architecturally prevent you from adding scriptlets. This has
led to widespread misuse of Model 2 architecture.]
With MVC you can have as many controller servlets in your web application. In fact you can have one Controller Servlet per module. However there are
several advantages of having a single controller servlet for the entire web application. In a typical web application, there are several tasks that you want to
do for every incoming request. For instance, you have to check if the user requesting an operation is authorized to do so. You also want to log the user’s
entry and exit from the web application for every request. You might like to centralize the logic for dispatching requests to other views. The list goes on. If
you have several controller servlets, chances are that you have to duplicate the
logic for all the above tasks in all those places. A single controller servlet for the
web application lets you centralize all the tasks in a single place. Elegant code
and easier to maintain.
Web applications based on Model 2 architecture are easier to maintain and extend since the views do not refer to each other and there is no presentation
logic in the views. It also allows you to clearly define the roles and responsibilities in large projects thus allowing better coordination among team
members.
Archived Comments
1. Programming is combination of intelligent and creative work. Programmers can do anything with code.
View Tutorial By: asad at 2016-08-05 15:44:42
2. thnks a lot...
View Tutorial By: iti at 2015-06-03 07:05:24
3. i didnot get the answer
View Tutorial By: ramana at 2013-02-04 06:36:37
4. Good Explanation.
View Tutorial By: Ashishkumar Haldar at 2013-01-03 11:47:25
5. To explain my ninth grader, I wrote a small explanation of MVC (http://architecture-soa-bpm-eai.blog
View Tutorial By: tushar at 2012-08-24 17:42:49
6. The concept is clear.
View Tutorial By: sam at 2012-08-23 03:52:15
7. Hi Tayo, approach 1 is recommended. In fact approach 2 is violation of the architecture. You need no
View Tutorial By: Ajay at 2012-02-17 16:47:17
8. basci sample using this MVC
http://www.daniweb.com/web-development/jsp/threads/141776
View Tutorial By: Marc Skoda at 2011-11-08 06:26:11
9. I have told before also....this person here is a complete shameless copycat and you are allowing a g
View Tutorial By: Debo at 2011-09-16 05:33:16
10. Neat and simple explanation. Thanks
View Tutorial By: Srinivasan at 2011-08-18 06:25:48
11. Clear explanation and clear architecture diagram..You said that a single servlet is easy to maintain
View Tutorial By: safiya begam at 2011-05-09 06:28:51
12. The tutorial is simple and clear.
@Gnani, i don't think there is any problem with the 6th poi
View Tutorial By: Tayo at 2010-01-03 13:10:24
13. Thanks for the clear illustration of the MVC2 architecture. Could you please help me with this scena
View Tutorial By: Tayo at 2010-01-03 12:53:46
14. In 6th point .....there is a spelling mistake...mistyped sole insted of role...make sure u correct i
View Tutorial By: Gnani at 2009-09-22 12:26:50
15. In 6th point .....there is a spelling mistake...mistyped sole insted of role...make sure u correct i
View Tutorial By: Gnani at 2009-09-22 12:25:59
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Configuring JDBC DataSources in Struts
When is the best time to validate input in Struts
Simple example of using the requiredif Validator rule in Struts
How to prepopulate a form in Struts
Using JavaScript to submit a form in Struts
FAQ: Why are my checkboxes not being set from ON to OFF?
FAQ: Why was reload removed from Struts (since 1.1)?
What is a Plug-in and how to use Java plug-ins with Struts?
Origin and Architecture of Struts
Handling multiple buttons in HTML Form in Struts