Using for loop inside of a JSP

181,397

Solution 1

You concrete problem is caused because you're mixing discouraged and old school scriptlets <% %> with its successor EL ${}. They do not share the same variable scope. The allFestivals is not available in scriptlet scope and the i is not available in EL scope.

You should install JSTL (<-- click the link for instructions) and declare it in top of JSP as follows:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

and then iterate over the list as follows:

<c:forEach items="${allFestivals}" var="festival">
    <tr>      
        <td>${festival.festivalName}</td>
        <td>${festival.location}</td>
        <td>${festival.startDate}</td>
        <td>${festival.endDate}</td>
        <td>${festival.URL}</td>  
    </tr>
</c:forEach>

(beware of possible XSS attack holes, use <c:out> accordingly)

Don't forget to remove the <jsp:useBean> as it has no utter value here when you're using a servlet as model-and-view controller. It would only lead to confusion. See also our servlets wiki page. Further you would do yourself a favour to disable scriptlets by the following entry in web.xml so that you won't accidently use them:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <scripting-invalid>true</scripting-invalid>
    </jsp-property-group>
</jsp-config>

Solution 2

Do this

    <% for(int i = 0; i < allFestivals.size(); i+=1) { %>
        <tr>      
            <td><%=allFestivals.get(i).getFestivalName()%></td>
        </tr>
    <% } %>

Better way is to use c:foreach see link jstl for each

Share:
181,397
Jack Dalton
Author by

Jack Dalton

Zealot to the god of coffee. Computer Science graduate taking on his gap year travels around the world.

Updated on July 13, 2022

Comments

  • Jack Dalton
    Jack Dalton almost 2 years

    I want to loop through an ArrayList of "Festivals" and get their information with get methods, printing out all its values. For some reason when I use this code, it will always choose the "0"th value and not increment the loop.

    If I hard code the values as "get(1)" it will get the correct values so my issue is clearly with the syntax.

    <h1>All Festival Information</h1>
        <jsp:useBean id="allFestivals" type="java.util.ArrayList" scope="session" />
        <table border="1">
            <tr>
                <td>Festival Name:</td>
                <td>Location:</td>
                <td>Start Date:</td>
                <td>End Date:</td>
                <td>URL:</td>
            </tr>
            <% for(int i = 0; i < allFestivals.size(); i+=1) { %>
                <tr>      
                    <td>${allFestivals.get(i).getFestivalName()}</td>
                    <td>${allFestivals.get(i).getLocation()}</td>
                    <td>${allFestivals.get(i).getStartDate()}</td>
                    <td>${allFestivals.get(i).getEndDate()}</td>
                    <td>${allFestivals.get(i).getURL()}</td>  
                </tr>
            <% } %>
        </table>