Home Jsp Simple-jsp-example JSTL for Loop
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JSTL for Loop

Advertisement
Consider a case where we want to do something with a collection like an array, retrieving one element at a time, and prints that element in a dynamically generated row.

JSTL for Loop <c: forEach> 

        

Consider a case where we want to do something with a collection like an array, retrieving one element at a time, and prints that element in a dynamically generated row. For this  <c: forEach> tag is perfect, this tag gives you the options to iterate over arrays and collections. 

In the tag <c:forEach var="i" begin="1" end="20" step="1" varStatus ="status">. Here this tag is working exactly as, for loop works in a jsp or in java. In this example i is the name of the variable, which begins from 1 and continues up to 20, we are incrementing the variable by 1, to increment the variable we have used step attribute. To print the value of the variable on the browser use <c:out> tag.

 

The code of the program is given below:

 

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
<title>JSTL Simple Conditional Execution Example</title>
</head>
<body bgcolor="#FFFFcc">
<h1>For Loop in JSTL</h1>
<c:forEach var="i" begin="1" end="20" step="1" varStatus ="status">
<c:out value="${i}" /> 
</c:forEach>
</body>
</html>

The output of the program is given below:

Download this example.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 12, 2008

Ask Questions?    Discuss: JSTL for Loop   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Stephanie
April 18, 2011
jstl foreach loop

I am a student working on a jsp project and am having trouble getting values from a java class to at jsp page using a jstl forEach loop. This is the loop: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:forEach items="${Products}" var="product" > <tr> <td>${product.code}</td> <td>${product.description}</td> <td>${product.price}</td> </tr> </c:forEach> And this is the array it is calling from a java class called ProductsServelet HttpSession session = request.getSession(); ArrayList<Product> products = ProductIO.getProducts( session.getServletContext().getRealPath("WEB-INF/products.txt")); session.setAttribute("products", products); Can anybody help?
Kyaw Myint Thu
April 18, 2011
JSTL

trying
Michael
June 24, 2011
<c:forEach> Question

What is varStatus ?
basit
March 15, 2012
JSTL for each loop

to know about JSL for each loop but plz use ArrayList of object type of class
SATABDI DHAR
April 18, 2012
JSTL

How should I create nested loop using JSTL....? Please guide me.
Mekbib
July 19, 2012
It is helpful

Thanks it is helpful
Mitesh
September 18, 2012
question

I copy pasted the code from above, as is, but I get the following output on my browser: ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} Iwas expecting numbers 1-20. Any idea what could be wrong?
Mitesh
September 18, 2012
question

I copy pasted the same code but when I open the url from a browser i get the following output: ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} ${i} I was expecting numbers - any idea what could be wrong? I am deploying this on Websphere app server
DMCA.com