I am using MVC design pattern to fetch data from a database to print it out on a JSP using JSTL For Each tag, whenever I run the Servlet that calls the JSP I get a blank JSP page. I am wondering where the problem is?
1- The server version is Tomcat 9.0.55 2- The full URL on my web browser address bar is http://localhost:8080/foodcart/FoodCart 3- The generated HTML output is as follows:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>menu</title>
</head>
<body>
</body>
</html>
Here is my Controller (the Servlet):
package com.sami.foodcart;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/FoodCart")
public class FoodCart extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// gets the data from the model
try {
List<Food> foodItems = FoodCartDBUtil.getFoodList();
request.setAttribute("foodItems", foodItems);
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// sends data to the view
RequestDispatcher rd = request.getRequestDispatcher("show-food.jsp");
rd.forward(request, response);
}
}
Here is my View (the JSP)
<%@ taglib prefix ="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>menu</title>
</head>
<body>
<c:forEach var="items" items="${foodItems}">
${items.id}
${items.item}
${items.price}
</c:forEach>
</body>
</html>