JSP in the MVC model with example

JSP in the MVC model with example

The Model-View-Controller (MVC) design pattern is a commonly used pattern for developing web applications. It separates the application into three interconnected components:

  • 1.  Model: Represents the application data and business logic. It handles data-related operations and provides the necessary data to the view.
  • 2.  View: Represents the presentation layer. It displays the data provided by the model to the user and sends user commands to the controller.
  • 3.  Controller: Handles user requests, processes them (possibly by interacting with the model), and determines the appropriate view to display the response.

MVC model

Using JSP in the MVC model typically involves:

  1. Servlets as Controllers
  2. JavaBeans or POJOs as Models
  3. JSPs as Views

Create the Model
Step 1: Create the Model
 User.java: 
java
package com.example.model;

public class User {
    private String name;
    private String email;

    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }
}


 UserDAO.java: 
java
package com.example.model;

import java.util.ArrayList;
import java.util.List;

public class UserDAO {
    public List<User> getAllUsers() {
        List<User> users = new ArrayList<>();
        users.add(new User("John Doe", "john.doe@example.com"));
        users.add(new User("Jane Smith", "jane.smith@example.com"));
        return users;
    }
}

Create the Controller
Step 2: Create the Controller
 UserController.java: 
java
package com.example.controller;

import com.example.model.User;
import com.example.model.UserDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/users")
public class UserController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserDAO userDAO = new UserDAO();
        List<User> users = userDAO.getAllUsers();
        request.setAttribute("users", users);
        request.getRequestDispatcher("userList.jsp").forward(request, response);
    }
}


Create the View
Step 3: Create the View
 userList.jsp: 
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>User List</title>
</head>
<body>
<h1>User List</h1>
<table border="1">
    <tr>
        <th>Name</th>
        <th>Email</th>
    </tr>
    <c:forEach var="user" items="${users}">
        <tr>
            <td>${user.name}</td>
            <td>${user.email}</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

Explanation MVC model

1.  Model (User.java and UserDAO.java):

  • User.java: Represents a User entity with name and email properties.
  • UserDAO.java: Provides a method to get a list of users. This is a simple data access object (DAO) for demonstration.

2.  Controller (UserController.java):

   UserController.java: A servlet that handles GET requests to the /users URL. It retrieves the list of users from the UserDAO and sets it as a request attribute. It then forwards the request to userList.jsp.

3.  View (userList.jsp):

   userList.jsp: A JSP page that displays the list of users in an HTML table. It uses JSP Standard Tag Library (JSTL) to iterate over the list of users and display their details.

Homepage

Readmore