[go: up one dir, main page]

0% found this document useful (0 votes)
64 views4 pages

Work Smarter, and Harder Too

This document provides an overview of the Model 1 and Model 2 architectural patterns for building Java web applications. It describes Model 1 as a page-centric approach where JSP pages call each other directly, while Model 2 follows the MVC pattern with separation of concerns between the model, view, and controller. The document recommends Model 2 for complex applications as it is more flexible, maintainable, and scalable. It then provides details on implementing MVC using Struts as the framework.

Uploaded by

Snow White
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views4 pages

Work Smarter, and Harder Too

This document provides an overview of the Model 1 and Model 2 architectural patterns for building Java web applications. It describes Model 1 as a page-centric approach where JSP pages call each other directly, while Model 2 follows the MVC pattern with separation of concerns between the model, view, and controller. The document recommends Model 2 for complex applications as it is more flexible, maintainable, and scalable. It then provides details on implementing MVC using Struts as the framework.

Uploaded by

Snow White
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Work smarter, and harder too…

Building JSP/Servlets based web application using MVC (Struts) framework.

Java developers generally use two design models in Web applications, simply called Model 1
and Model 2.

Model-1 (page-centric)
Model-2 (MVC – Model View Controller)

Model-1

Model 1, is a page-centric model in which an application consists of a series of JSP


pages. In this model, a JSP page calls another JSP page. (This model presents maintenance
nightmares and does not suit complex web applications)

You can use this model when you write simple web applications or if you want things done
quickly.

This model allows JSPs or Servlets direct access to some resource like a database or legacy
application to service a client’s request: the early JSP specifications termed this a “Model 1”
programming approach. The JSP page is where the incoming request is intercepted processed,
and the response sent back to the client; JSPs only differed from Servlets in this scenario by
providing cleaner code, and separating code from the content by placing data access in beans.

The advantage of such an approach is that it is simple to program, and allows the page author
to generate dynamic content easily, based upon the request and the state of the resources.

However this architecture does not scale up well for a large number of simultaneous clients
since there would be a significant amount of request processing to be performed, and each
request must establish or share a potentially scarce/expensive connection to the resource in
question. (A good example would be JDBC connections in servlets or JSPs and the need for
connection pools.)

Indiscriminate usage of this architecture usually leads to a significant amount of Java code
embedded within the JSP page. This may not seem to be much of a problem for Java
developers but it is certainly an issue if the JSP pages are maintained by designers: the code
tends to get in the designer’s way, and you run the risk of your code becoming corrupted when
others are tweaking the look and feel.

Model-2 (MVC)

Model 2, is a Model-View-Controller (MVC) architecture that separates content


generation and content presentation. “Model 2” architecture is indicated by the presence of a
controller servlet between the client browser and the JSP pages or servlet content that
presents the content. The controller servlet dispatches HTTP requests to the corresponding
presentation JSP pages based on the request URL, input parameters, and application state. In
this model, presentation parts (JSP pages or servlets) are isolated from each other.

Model View Controller" is a standard way of splitting presentation, application logic and domain
objects. It has its roots in early Smalltalk systems and has been used successfully in a wide
variety of systems. Model-View-Controller is the paradigm behind the traditional Smalltalk-
80 user interface.

MVC is not limited to web applications alone. It can be implemented in any project.

Examples
- C#, VB, VC++ Application development can be based on MVC.
- Most of Java Swing is implemented using MVC
(http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node15.html)
- Microsoft Word is developed based on MVC architecture.

MVC is further extended to HMVC (Hierarchical MVC), which is not discussed here.
(Please check - http://www.javaworld.com/javaworld/jw-07-2000/jw-0721-hmvc_p.html)
Model 2 applications are more flexible and easier to maintain and extend because views do not
reference each other directly. The Model 2 controller servlet provides a single point of control
for security and logging, and often encapsulates incoming data into a form usable by the back-
end MVC model.

“Model 2” architecture is recommended for complex applications. Building an application using


this model is not as simple as using Model 1; however, this model offers many advantages over
the first model

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.

MVC implementation understanding

Model – application object / data (Java Bean which encapsulates Business Logic)
View - display of model (JSP which formats and displays output to the user)
Controller - user interface interaction (Servlet which process get/post/put requests)

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.

Quotes – (After reading this take a refreshment bio-break)

All our knowledge has its origins in our perceptions.

Beauty can't amuse you, but brainwork -- reading, writing, and thinking -- can.

Dollars and guns are no substitutes for brains and will power.

-------------------------------------------------- B R E A K ------------------------------------------------------
Struts is a framework for implementing MVC (Discussion restricted to Tomcat
http://jakarta.apache.org/tomcat)

1) Download struts (after you have installed java and tomcat successfully)
2) Unzip in a folder
3) You will find a lib folder containing many jar files, which are required by your web
application. If your web application does not have lib folder inside
webapps/webApplicationName/WEB-INF/classes or the jar files, place those files here
(webapps/webApplicationName/WEB-INF/classes)
4) Read Install file (read install file for tomcat installation only)
5) Install struts-example.war and struts-documentation.war by placing them inside the
webapps folder.
6) Start tomcat (when you start automatic deployment happens for struts-example.war
and struts-documentation.war)
7) Check the application http://localhost:8080/struts-example/tour.do
8) http://localhost:8080/struts-example/tour.do - Read this article on your machine
9) Test, read the code and understand.

Its better you visit


http://www.fawcette.com/javapro/2002_09/online/servletsjsp_bkurniawan_09_13_02/
to know basic knowledge of how to build your first web application using struts.

This Example can be downloaded at http://www.fawcette.com/javapro/codepage.asp?


loccode=jpep020913sj

END

You might also like