trainingtrains Logo

91-9990449935

 0120-4256464

MVC in JSP

MVC stands for Model View and Controller. It is a design pattern that separates the business logic, presentation logic and data.

Controller acts as an interface between View and Model. Controller intercepts all the incoming requests.

Model represents the state of the application i.e. data. It can also have business logic.

View represents the presentaion i.e. UI(User Interface).

Advantage of MVC (Model 2) Architecture

  1. Navigation Control is centralized
  2. Easy to maintain the large application
mvc architecture

If you new to MVC, please visit Model1 vs Model2 first.

Example of following MVC in JSP

In this example, we are using servlet as a controller, jsp as a view component, Java Bean class as a model.

In this example, we have created 5 pages:

  • index.jsp a page that gets input from the user.
  • ControllerServlet.java a servlet that acts as a controller.
  • login-success.jsp and login-error.jsp files acts as view components.
  • web.xml file for mapping the servlet.
File: index.jsp
File: ControllerServlet
File: LoginBean.java
File: login-success.jsp
File: login-error.jsp
File: web.xml

mvc output 2 mvc output 3
Next TopicJstl