I have an ArrayList that is defined in a scriptlet in a JSP. In the body section, I want to display the items using a JSTL forEach loop.
After going through tutorials like this one, I have written the following code:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.ArrayList" %>
<%
ArrayList<String> fruits = new ArrayList<String>();
fruits.add("Orange");
fruits.add("Apple");
%>
<html>
<head>
<title>JSTL</title>
</head>
<body>
<c:forEach var="fruit" items="${fruits}">
<c:out value="${fruit}" />
</c:forEach>
</body>
</html>
But I am getting a blank page. Where am I going wrong in the above code?
All tutorials that I could find seem to define an ArrayList of beans in the servlet and pass them to the JSP through the request
. In the forEach loop, they use c:out
and ${bean.prop}
to print it. I haven't tried them as such. I wanted to do something much simpler, but can't seem to get this code to work.