0% found this document useful (0 votes)
9 views18 pages

Slot 7 - MVC

sdfdsfdsfsd

Uploaded by

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

Slot 7 - MVC

sdfdsfdsfsd

Uploaded by

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

ĐẠI HỌC FPT CẦN THƠ

Introduction to MVC Architecture


Objective

Describe the use of JSP models in Web applications

Explain JSP Model 1

Explain JSP Model 2

Explain the Model-View-Controller architecture

Explain the relationship between the components of MVC

MVC_Lương Hoàng Hướng 2


Objective

Explain Controller and its purpose in MVC

Explain View and its purpose in MVC

Explain Model and its purpose in MVC

Develop a Web application based on MVC architecture

MVC_Lương Hoàng Hướng 3


Introduction

❖ Sun Microsystems has provided the JSP specification to


addresses the problem of tightly-coupled presentation
and business logic in Servlets.
❖ Based on the popularity and benefits of JSP in Web
development, the commonly used approaches using
JSP were identified.
❖ These approaches are also referred to as JSP models.

MVC_Lương Hoàng Hướng 4


JSP Models

There are two types of programming models for


developing Web application.
JSP Model 1

• The Model 1 architecture is very simple.


• The HTML or JSP page sends request along with the data to Web container.
• The Web container invokes the mapped Servlet which handles all responsibilities for the
request.
• The responsibilities include processing the request, validating data, handling the business
logic, and generating a response back to browser.

JSP Model 2 (Model-View-Controller)

• It provides a clear separation of application responsibilities.


• A central servlet, known as the Controller, receives all requests for the application from JSP.
• The Controller works with the Model to prepare any data needed by the View and forwards
the data back to the JSP.
• The business and presentation logic are separated from each other, which
• help to reuse the logic.

MVC_Lương Hoàng Hướng 5


JSP Model 1 Architecture

Model 1 architecture enables the Web designers to develop Web


applications such that it separates business logic from presentation
logic.
• It deals with the method of modeling real world business objects
such as accounts, loans, travel, and so on in the application.
Business • It also deals with the storage mechanism for these objects,
object interactions, access, and update rights for them.
Logic

• It deals with methods of displaying these objects.


• For example, decisions related to displaying user accounts in a
Presentation list form.
Logic

MVC_Lương Hoàng Hướng 6


JSP Model 1 Architecture

MVC_Lương Hoàng Hướng 7


JSP Model 1 Architecture

JSP Model 1 architecture has a page-centric architecture.


Page-centric architecture:
The application is composed of a series of interrelated JSP pages and these JSP
pages handle all aspects of application.
The business process logic and control decisions are hard-coded inside JSP pages
in the form of JavaBeans, scriptlets, and expressions.
Figure shows an example of JSP Model 1 architecture for an online shopping
Web application with all JSP pages.

MVC_Lương Hoàng Hướng 8


JSP Model 1 Architecture

Advantages of JSP Model 1:


It makes development easier as there are no Servlets
involved in the application.
Disadvantages of JSP Model 1:
As business logic and presentation logic are tied together, it is
difficult for building and maintaining a complex enterprise
application.
Each of the JSP pages is individually responsible for control
logic, application logic, and also to present results to the
user. This makes the JSP Model 1 more dependent and less
extensible.

MVC_Lương Hoàng Hướng 9


JSP Model 2 Architecture

Model 2 architecture is an approach used for developing a Web application.


It separates the Business logic from the Presentation logic.
The Model 2 has an additional component - a Controller.
Figure shows the Model 2 architecture.

MVC_Lương Hoàng Hướng 10


JSP Model 2 Architecture

In Model 2 architecture, a Servlet acts as a Controller and therefore, it


has a Servlet-centric architecture.

Servlet - Controller:
Is responsible to process the incoming request and instantiate a Model - a
Java object or a bean to compute the business logic.
Is responsible for deciding to which JSP page the request should be
forwarded.

JSP - View:
Is responsible for handling the View component.
Retrieves the objects created by the Servlet.
Extracts dynamic content for insertion within a template for display.

MVC_Lương Hoàng Hướng 11


JSP Model 2 Architecture

Advantages of Model 2:

Web applications based on this model are easier to maintain and


extendable.

Testing is easy in model 2.

MVC_Lương Hoàng Hướng 12


Model-View-Controller (MVC)

MVC is a software architectural pattern.


This pattern divides the application logic from User Interface.
The division permits independent development, testing, and
maintenance of each component.
Figure shows a brief overview of the components under the MVC
architecture.

MVC_Lương Hoàng Hướng 13


Model-View-Controller (MVC)

Controller

• Manages the flow of data between the Model layer and the View layer.
• Calls methods in the Model to fulfil the requested action.
• After the action has been taken on the data in Model, the Controller is
responsible for redirecting the appropriate view to the user.

View

• Is used to generate the response to the browser, what the user sees.
• Are simple JSP or HTML pages.

Model

• Is a layer between the Controller and the database.


• Contains the business logics and functions that manipulate the business data.
• Can access the functionalities encapsulated in the Model.

MVC_Lương Hoàng Hướng 14


Relationships between Components

View and
• In this relationship, the Controller is responsiblefor creating
Controller and selecting views.
Relationship
Model and • In this relationship, the View depends on the Model.
View • If a change is made to the Model, then there might be a
requirement to make parallel changes in the View.
Relationship
Model and • In this relationship, the Controller is dependent on the Model.
Controller • If a change is made to the model interface,then there might
be a requirement to make parallel changes to the Controller.
Relationship
MVC_Lương Hoàng Hướng 15
MVC in Web Application

Model encapsulates data and business logic using JavaBean components or


Plain Old Java Object (POJO), a database API, or an XML file.

View shows the current state of the Model using an HTML or a JSP page.

Controller updates the state of the Model and generates one or more views
using Servlet.

MVC_Lương Hoàng Hướng 16


Implementation of MVC Pattern

DEMO

MVC_Lương Hoàng Hướng 17


Summary

The JSP specification presents two approaches for developing Web applications namely, JSP Model I and JSP Model
II.

JSP Model II is also known as MVC.

MVC is a software design pattern, which can be used to design medium and large sized applications.

MVC has three components as follows: Model, View, Controller.

In MVC Web application, servlet acts as Controller, which receives the request

from client.

The View handles presentation of the content on the Web page and could be an HTML file or a JSP file.

MVC_Lương Hoàng Hướng 18

You might also like