0% found this document useful (0 votes)
12 views31 pages

11.0 MVC

MVC, or Model View Controller, is a design pattern used to separate business logic, presentation logic, and data in web applications. It consists of three layers: the Model Layer for data management, the View Layer for user interface presentation, and the Controller Layer as an intermediary that handles user input and requests. The document outlines a user management project, detailing the creation of the Model, View, and Controller components, along with steps for implementation in a Java environment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views31 pages

11.0 MVC

MVC, or Model View Controller, is a design pattern used to separate business logic, presentation logic, and data in web applications. It consists of three layers: the Model Layer for data management, the View Layer for user interface presentation, and the Controller Layer as an intermediary that handles user input and requests. The document outlines a user management project, detailing the creation of the Model, View, and Controller components, along with steps for implementation in a Java environment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

MVC

MVC
What is MVC ?
• MVC stands for Model View Controller.

• It is a design pattern which is used to separate the


business logic, presentation logic and data.

• MVC is used to design a web application in a standard


manner and provides a pattern to design web application.

• MVC is able to define which element is used for which


purpose.

• As per MVC, our application can be divided into three


layers.
 Model Layer
 View Layer
 Controller Layer
MVC
Model Layer

 Model Layer is also known as the Data layer.


 It consists of all the data of our web application.
 Model layer represents a state of an application.
 It is responsible to connect with the database as well as
stores the data into a database.
 It consists of all the classes in our application which have
the connection to the database.
MVC
View Layer

 It represents the Presentation layer


 It normally represents the User Interface (UI) of the
application.
 We can use HTML,CSS, JS etc. to create presentation layer.
MVC
Controller Layer

• Controller layer is an interface between the view layer


and the model layer.
• It receives the request from View Layer and reads the
data which is coming from the presentation layer.
• To read data from the presentation layer, we can use
Servlet, JSP, Filter, etc.
MVC Project :
User Management
Screenshot
Structure of the App
Model

• In our application, the User class will represent the


Model.
• The Model will be responsible for managing and
encapsulating the data of our application.
• The User class will have attributes like id and name to
represent user data.
• In our UserController servlet, we will maintain a list of
User objects (users) to store and manage user data.
View:

• The View is responsible for presenting the data to the


user and handling user interface elements.

• In our application, the JSP files (e.g., "user-list.jsp") will


serve as the View components.
• The JSP files will define the structure and layout of the
web pages that users will see.
• They will use embedded Java code to display dynamic
data from the Model and handle user interactions.
Controller:

The Controller acts as an intermediary between the Model


and the View. It handles user input, processes requests,
interacts with the Model to retrieve or modify data, and
determines which View should be presented to the user.

• In our application, the UserController servlet will serve


as the Controller.

• It will listen for incoming HTTP requests, determine the


action to be performed based on request parameters
(e.g., "action=delete"), interact with the Model (e.g.,
creating, listing, or deleting users), and will forward the
appropriate View (JSP) to display the results.
STEPS

1) To create Model, we will create a new Java project in NetBeans


“usermanagement”
2) We will add a “.jar” file reference for “servlet-api.jar” from the
path “C:\xampp\tomcat\lib”
3) After creating the required classes, we will compile the JAVA code
and export the .class file with package folder to the path “C:\
xampp\tomcat\webapps\ROOT\WEB-INF\classes”
4) After creating classes, we will create Views that will serve as
frontend.
5) Lastly, we will modify the “web.xml” file for mapping.

NOTE : To get more details about how to compile and copy the class
files with package to the “Tomcat” root, refere to “6.0_JSP_Filters.pptx”
Creating Model
(User.java)
We will create a new Java Project in NetBeans, create a new
Java class file “User.java” in it.
Add the following reference to the project as well
Creating Controller
(UserController.java)
After creating Java code, “clean-compile” to create the class files,
copy them from the “…build\classes\” folder and put them in the
“…tomcat\webapps\ROOT\Web-INF\classes\” folder as follows :
Creating View
(main.jsp and
user-list.jsp)
Modifying “web.xml”

You might also like