<c:forEach>

We can use this tag for implementing general for loop.

Form1:

<c:forEach begin = "0" end = "10" step ="2">
<h1>learning JSTL is very easy !!! </h1>
</c:forEach>

Output:

learning JSTL is very easy !!!
learning JSTL is very easy !!!
learning JSTL is very easy !!!
learning JSTL is very easy !!!
learning JSTL is very easy !!!
learning JSTL is very easy !!!

The begin attribute specifies the index when the loop has to start. end index specifies the index where the loop has to terminate.This loop internally maintains a Counter value Which is incremented by the value of step attribute
The default value of the step attribute is 1 and it is optional attribute.

Form2:<c:forEach> with var attribute:

<c:forEach> Internally maintains a counter variable which can be accessed by using var attribute.
This var is local variable and outside of for loop we can’t access.

<c:forEach begin = "0" end = "20"  step = "5" var = "x">
<h1> the counter value is : ${x} </h1>
</c:forEach>

output:

the counter value is :0
the counter value is :5
the counter value is :10
the counter value is :15
the counter value is :20

Form3:<c:forEach> for iterating through arrays and collections

<c:forEach items = "collection-obj" var = "current-obj">
Body
</c:forEach>

items attribute should contain either collection obj or array.
This action will iterate over every object in the collection to execute the body.We can capture obj using var attribute.

Type of items Attribute Type of var Attribute
primitive array
Example:int[]
doubnle[]
corresponding wrapper object
Integer
Double
object type Arrays
Example:Student[]
corresponding Declared type
Student
collection object
Map Map.Entry
List of strings seperated by, String

Example:

<%
   String [] s ={"A", "B", "C", "D"};
Page context . set attribute ("s",s);
%>

<c:forEach items = "${s}" var = "obj">
<h1> the current obj is = ${obj} </h1>
</c:forEach>

output:

The current obj is A
The current obj is B
The current obj is C
The current obj is D

Question:

write a jsp to display all request headers by using <c:forEach> tag:

<table border = "2">
<c:forEach items = "${header}" var = "hdr">
<tr> <td>${hdr.Key} <td><td> ${hdr.value} </td></tr>
</c:forEach>
</table>.

Question:Write a jsp to print all cookies associated with request

&lt;c:forEach items = &quot;${cookie}&quot; var = &quot;c&quot; &gt;
${c.value.name}----->${c.value.value}
&lt;/c:forEach&gt;

Form4:<c:forEach> with var status attribute

This attribute describes the status of iteration like current iteration number, is it first iteration or not etc…
This class contains several methods which are helpful while performing iteration.

  1. object getCurrent()
    Return the current item.
  2. int getIndex()
    Return the current index. (counter value)
  3. int getCount()
    Returns the no. of iterations that have already performed including current iteration.
  4. boolean isFirst()
    Returns true if the current iteration is the first iteration.
  5. boolean isLast()
    Returns true if the current iteration is the last iteration
  6. Integer getBegin()
    Returns the begin index of the iteration.
  7. Integer getEnd()
    Returns last index.
  8. Integer getStep()
    Returns incremented value.

Example:

		
<c:forEach begin = "0" end = "10" step = "2" var status = "x">
<h1> is it first iteration :${x .first} <br>
The current index : ${x.index} <br>
The step value is : ${x.step} </h1>
</c:forEach>

<c:forTokens>

It is a specialized version of <c:forEach> to perform string tokenization based on some delimiter. It behaves exactly same as StringTokenizer.

Example:

<c:forTokens items = "A,B,C,D" delims = ','>
This is tokenization
</c:forTokens>

We can hold current token by using var attribute

Example:

<c:forTokens items = "www.java2learn.com" delims = "." Var = "x">
    The current token is : ${x}
    </c:forTokens>
 

<c:forTokens can take the following extra attributes also.

  1. Begin:

    To specify the start index of the iteration.
    Index of the first token is 0.

  2. end:

    To specify the index where iteration should be terminated

  3. step:

    Incremented value blw iterations.

  4. varStatus:

    To specify the status of the iteration.

Example:

<c:forTokens items = "one, two ,there ,four ,five" delims = ","
 var = "x" begin = "0" end   = "0" end = "4"  step = "2">
<h1> the current token is : ${x} </h1>
</c:forTokens>

output
current token is :one
current token is :three
current token is : five

Note:

In the case of <c:forTokens> items attribute should be string only. But in the case of <c:forEach> items attributes can be String,Collection array and map. Hence <c: forTokens> tag is considered as specifited version of <c:forEach>

Summery of iteration tags:

Tag Description Attributes
<c:forEach> General purpose for loop items,begin,end,step,var,varStatus
<c:forTokens> Specialized for String tokenization items,begin,end,step.var,varStatus,delims