I am trying to creating a JSP home page that contains a table of varying sizes. I am wanting to use the forEach JSTL tag. The servlet is working correctly and is producing a list of two products. However this information does not appear on the final HTML.
If I call:
http://localhost:8080/JavaIntoJSP/products
the output is good.
If I call:
http://localhost:8080/JavaIntoJSP/default.jsp
the output is missing
Code as follows (default.jsp):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix= "c" %>
<!DOCTYPE html>
<html>
<head>
<title>default</title>
</head>
<body>
<form action="products" method="get">
<table>
<c:forEach items="${products}" var="product" >
<tr>
<td><c:out value="${product.name}" /></td>
<td><c:out value="${product.price}" /></td>
<td><c:out value="${product.category}" /></td>
<td><c:out value="${product.units}" /></td>
</tr>
</c:forEach>
</table>
</form>
<h1>try again</h1>
</body>
</html>
The Servlet has the following
@WebServlet("/products")
public class ProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ProductServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Product> products = new ArrayList<Product>();
Product prod1 = new Product();
prod1 = prod1.returnProduct();
products.add(prod1);
Product prod2 = new Product();
prod2 = prod2.returnProduct();
prod2 = prod2.returnProduct();
products.add(prod2);
request.setAttribute("products", products);
request.getRequestDispatcher("default.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Can anyone plese let me know why the products are not being found on the JSP?