3

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.

  • 2
    It's not JSTL who finds them. It's EL (those `${}` things) who finds them. Start at http://stackoverflow.com/tags/el/info – BalusC Feb 07 '13 at 16:56
  • BalusC, thanks for the link. It gave a clear explanation of where it looks for the objects. –  Feb 07 '13 at 17:02
  • 1
    You're welcome. As to JSTL, look at http://stackoverflow.com/tags/jstl/info (you can find those tag wiki pages by hovering the tag until a black box shows up and clicking therein the *info* link). As to the concrete question, you should actually be using a preprocessing servlet (you should avoid using `<% %>` in JSP). See the 2nd hello world example in http://stackoverflow.com/tags/servlets/info – BalusC Feb 07 '13 at 17:04

3 Answers3

3

add pageContext.setAttribute("fruits", fruits);

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ 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");
pageContext.setAttribute("fruits", fruits);
%>
<html>
<head>
<title>JSTL</title>
</head>
<body>
    <c:forEach var="fruit" items="${fruits}">
     <c:out value="${fruit}" />
    </c:forEach>
</body>
</html>
SaK
  • 410
  • 3
  • 10
2

You need to put the array in the request. Do this right after the last fruits.add() call.

<%= request.setAttribute( "fruits", fruits ); %>
mightyrick
  • 910
  • 4
  • 6
  • Well it worked, thanks! Does that mean, only those objects I set using `request.setAttribute()` can be used in JSTL tags? In what other way can I define/set objects so that they are available for use in JSTL tags? –  Feb 07 '13 at 16:55
  • What about the `page` scope? – Paul Vargas Feb 07 '13 at 16:57
  • 3
    While this work, it's not suited as a best practice. Related: [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/q/3177733/1065197) – Luiggi Mendoza Feb 07 '13 at 17:02
  • Luiggi, I agree, it is not a best practice. All I was trying to do was answer the OP's direct question without getting into how to implement web applications using one of the many Java web stacks available. – mightyrick Feb 07 '13 at 20:51
  • Paul, `page` scope will definitely work. The only caveat there is that if the JSP does a forward or redirect, the variable will not be available to the subsequent JSP. So this is an important distinction for OP to understand before he uses `page` scope. – mightyrick Feb 07 '13 at 20:53
0

easiest one would be to define a variable using and using that.

<c:set var="fruits">
   <%= fruits %>
<c:set>

<c:forEach var="fruit" items="${fruits}">
    <c:out value="${fruit}" />
</c:forEach

The <%= fruits %> is the arraylist you created in scriptlet.

Abhishek
  • 91
  • 1
  • 5