Are you searching for information on MVC Architecture in JSP? Then, this java tutorial is the correct choice for you. Here, we have shared the complete details about MVC Architecture in JSP with Example like definition, layers of MVC, advantages, and an example program.
- What is MVC?
- Layers of MVC Architecture
- Model Layer
- View Layer
- Controller Layer
- Advantage of MVC (Model 2) Architecture
- Example of MVC Architecture in JSP
MVC stands for Model View Controller. It is a design pattern that is used to separate the business logic, presentation logic, and data. It can be used to design a web application in a standard manner ie: It will provide a pattern to design a web application. It is able to define which element is used for which purpose.
As per MVC, our application will be divided into three layers.
- Model Layer is the data layer.
- It consists of all the data of our web application.
- It represents a state of an application.
- The Model Layer 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 that have a connection to the database.
- It represents the presentation layer
- It normally represents the User Interface (UI) of the application.
- We can use HTML, CSS, JS, etc to create a presentation layer.
- It is an interface between the view layer and the model layer.
- It receives the request from View Layer.
- It read the data which is coming from the presentation layer.
- To read data from the presentation layer, we can use Servlet, JSP, Filter, etc.
The MVC Architecture is given below:
The advantage of MVC architecture are:
- It is easy to maintain a large application.
- It is easy to test.
- It is easy to extend.
- The navigation control is centralized.
In this example, we are going to show you how to use MVC architecture in JSP. In this example, we are going to create an example in which servlet as a controller, JSP as a view component, Java Bean class as a model.
In this example, we have created 6 pages.
- index.jsp: It gets the input from the user.
- ControllerServlet.java: It acts as a controller.
- loginSuccess and loginError.jsp files acts as a view component.
- LoginMvcBean.java: It acts as a Model layer.
- web.xml file is used to mapping the servlet.
Output: When we enter the right credential.
Output: When we enter the wrong credential.