Work Smarter, and Harder Too
Work Smarter, and Harder Too
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
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 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.
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.
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.
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.
END