MVC Architecture (Model 2 Architecture)

By: Baski Emailed: 1608 times Printed: 2078 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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. 

  1. The Controller Servlet handles the user’s request. (This means the hyperlink in the JSP should point to the controller servlet). 
  2. The Controller Servlet then instantiates appropriate JavaBeans based on the request parameters (and optionally also based on session attributes). 
  3. 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. 
  4. The Controller sets the resultant JavaBeans (either same or a new one) in one of the following contexts – request, session or application. 
  5. The controller then dispatches the request to the next view based on the request URL. 
  6. 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.


Struts Home | All Struts Tutorials | Latest Struts Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(8)


1. View Comment

In 6th point .....there is a spelling mistake...mistyped sole insted of role...make sure u correct it.... ("The sole function of the JSP in Model 2 architecture")

View Tutorial          By: Gnani at 2009-09-22 12:25:59
2. View Comment

Thanks for the clear illustration of the MVC2 architecture. Could you please help me with this scenario:
1. I have a controller (servlet1) that passes some data(model) to the view (jsp1) for display.
2. On jsp1, i want to have a hyperlink to display some other view (jsp2), which a different set of data.

What is the proper approach?
a. Do i point the hyperlink (href) to another controller (servlet2) which gets the data(model) for display on jsp2
OR
b. Do i just point the hyperlink (href) directly to jsp2 and then use JSTL sql to access my data correctly? Is it proper to do this with MVC2 mindset?

If i take the approach in a., does it mean that i'll need a controller servlet for each view?

Please note that i'm trying to learn the MVC2 approach without using any framework yet.


Kindly help. Thanks


View Tutorial          By: Tayo at 2010-01-03 12:53:46
3. View Comment

The tutorial is simple and clear.
@Gnani, i don't think there is any problem with the 6th point.


View Tutorial          By: Tayo at 2010-01-03 13:10:24
4. View Comment

Clear explanation and clear architecture diagram..You said that a single servlet is easy to maintain the code, but in the situation of large project how can come up with a single servlet?.. But my thought is too same with u..Always we need to maintain a single servlet for the clear code purpose..

View Tutorial          By: safiya begam at 2011-05-09 06:28:51
5. View Comment

The concept is clear.

View Tutorial          By: sam at 2012-08-23 03:52:15
6. View Comment

Good Explanation.

View Tutorial          By: Ashishkumar Haldar at 2013-01-03 11:47:25
7. View Comment

i didnot get the answer

View Tutorial          By: ramana at 2013-02-04 06:36:37
8. View Comment

thnks a lot...

View Tutorial          By: iti at 2015-06-03 07:05:24

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Baski
Compiling multiple source files and specifying classpath using javac
The equals() Method example in Java
Default Values for Data Types in Java
JSP Example to connect to MS SQL database using Tomcat Connection Pool
<convertNumber> and <convertDateTime> in JSF
Enable/Disable Scripting Elements in JSP
Using calloc() Function in C
lseek() sample program in C
Arrays of Structures example program in C
The Basic Syntax Expression Language in JSP
Initialization of Pointer Arrays in C
Functions returning non-integer values in C
Increment and Decrement Operators in C
Getting Started with C
What is JSF (JavaServer Faces)?

More Tutorials in Struts
Configuring JDBC DataSources in Struts
Struts Classes
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
Using JavaScript to submit a form in Struts
How to prepopulate a form in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
When is the best time to validate input in Struts
What is a Plug-in and how to use Java plug-ins with Struts?
7 Best Practices of Struts
Origin and Architecture of Struts
ActionErrors and ActionError in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts

More Latest News
Most Viewed Articles (in Struts )
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
ForwardAction in Struts
Handling multiple buttons in HTML Form in Struts
Struts and Tiles - Steps to use Struts and Tiles
What is Struts? Which Version of Struts to use?
What is a Model View Controller (MVC) Model?
Configuring JDBC DataSources in Struts
MVC Architecture (Model 2 Architecture)
Use of {0} is required and Accessing resource bundles programmatically in Struts
DispatchAction in Struts
Handling Duplicate Form Submissions in Struts
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
How to prepopulate a form in Struts
Is Struts the most popular web application framework for Java?
Most Emailed Articles (in Struts)
WebWork 2 is now Apache Struts 2
What is Struts? Which Version of Struts to use?
Is Struts the most popular web application framework for Java?
Using Multibox in Struts
Use ImageButtonBean for Image based Form Submits in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
FAQ: Why was reload removed from Struts (since 1.1)?
Simple example of using the requiredif Validator rule in Struts
What is a Model View Controller (MVC) Model?
Struts 1 vs Struts 2
Using Checkbox & Radio Tags, html:select, html:options in Struts Forms
IncludeAction in Struts
The directories and files of a Struts application
Creating the first application using Struts 2
Using Multiple Message Resource Bundles in Struts