" "> " " />
Easy Tutorial
❮ Jsp Cookies Jsp Custom Tags ❯

<x:forEach> Tag

JSP Standard Tag Library

The <x:forEach> tag is used to loop through nodes in an XML document.

Syntax

&lt;x:forEach
   var="<string>"
   select="<string>"
   begin="<int>"
   end="<int>"
   step="<int>"
   varStatus="<string>">

Attributes

The <x:forEach> tag has the following attributes:

Attribute Description Required Default Value
select XPath expression to be evaluated Yes None
var Variable to store the current item No None
begin Starting index of the iterator No None
end Ending index of the iterator No None
step Step size of the iteration No None
varStatus Variable representing the status of the iterator No None

Example

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<html>
<head>
  <title>JSTL x:forEach Tag</title>
</head>
<body>
<h3>Books Info:</h3>

&lt;c:set var="xmltext">
  <books>
    <book>
      <name>Padam History</name>
      <author>ZARA</author>
      <price>100</price>
    </book>
    <book>
      <name>Great Mistry</name>
      <author>NUHA</author>
      <price>2000</price>
    </book>
  </books>
</c:set>

&lt;x:parse xml="${xmltext}" var="output"/>
<ul class="list">
&lt;x:forEach select="$output/books/book/name" var="item">
   <li>Book Name: <x:out select="$item" /></li>
</x:forEach>
</ul>

</body>
</html>

The output will be as follows:

BOOKS INFO:
Book Name: Padam History

Book Name: Great Mistry

JSP Standard Tag Library

❮ Jsp Cookies Jsp Custom Tags ❯