Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using for loop inside of a JSP

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>  
like image 784
Jack Dalton Avatar asked Apr 05 '13 16:04

Jack Dalton


People also ask

Can we use while loop in JSP?

The while Loop Note that if the condition is not true, the body of the loop is not even executed once. Here's an example putting the while loop to work; in this case, the code will display a value, then subtract one from it the next time through the loop, as long as that value stays positive.

Can we use for loop inside method in Java?

It is possible prove that there is no way that the code after the loop (as written) can be reached. However, according to the JLS code reachability rules that the compiler is required to implement, it is possible to reach the end of the method. Therefore, there has to be a return statement after the loop.

What is <%@ in JSP?

The page directive is used to provide instructions to the container that pertain to the current JSP page. You may code the page directives anywhere in your JSP page. By convention, page directives are coded at the top of the JSP page. Following is the basic syntax of page directive − <%@ page attribute = "value" %>

What is <%= some Java code %> in JSP?

A scriptlet tag is used to execute java source code in JSP. Syntax is as follows: <% java source code %>


1 Answers

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> 
like image 200
BalusC Avatar answered Oct 20 '22 00:10

BalusC