When working with JSP (JavaServer Pages), the <c:foreach> tag of JSTL (JavaServer Pages Standard Tag Library) is useful in displaying collections of data. It allows you to easily iterate over an array, a collection, or even a range of numbers. This tutorial explains how to use the <c:foreach> tag effectively, ensuring you can apply it efficiently in your JSP projects.



Understanding <c:foreach>

The <c:foreach> tag is a part of the JSTL core library and is used to iterate over a collection of items. It works like a loop in traditional programming languages but is explicitly designed for JSPs. This tag makes displaying elements from a collection simple, making your JSP code cleaner and more readable.

<c:foreach> Tag Syntax

The basic syntax of the <c:foreach> tag is:

<c:foreach items="${collection}" var="item">
    <!-- Your code here -->
</c:foreach>

Here, the items is the collection or array you want to iterate over, and the var is the variable name for the current item in the loop.

Practical Usage of <c:foreach> Tag

The JSTL <c:foreach> tag enables dynamic presentation of data in JSP. Let's explore some practical scenarios where this tag can be used effectively.

Looping Over Array or Collection

Suppose you have a collection of strings (like a list of names) that you want to display on your JSP page.

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<!DOCTYPE html>
<html>
<head>
    <title>Iterating Over a Collection Example</title>
</head>
<body>
    <% 
        java.util.List<String> names = new java.util.ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        request.setAttribute("names", names);
    %>
    <c:foreach items="${names}" var="name">
        <p>${name}</p>
    </c:foreach>
</body>
</html>

Iterating Through Number Ranges

You can also use <c:foreach> to loop through a range of numbers. It is useful for creating numbered lists or tables dynamically.

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<!DOCTYPE html>
<html>
<head>
    <title>Number Range Iteration Example</title>
</head>
<body>
    <c:foreach begin="1" end="10" var="num">
        <p>${num}</p>
    </c:foreach>
</body>
</html>

 Loop Status Details

The <c:foreach> tag provides a varStatus attribute for accessing loop status information like the current count, index, and whether it's the first or last iteration.

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<!DOCTYPE html>
<html>
<head>
    <title>Loop Status Details Example</title>
</head>
<body>
    <% 
        java.util.List<String> names = new java.util.ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        request.setAttribute("names", names);
    %>
    <c:foreach items="${names}" var="name" varStatus="status">
        <p>${status.index + 1}. ${name}</p>
    </c:foreach>
</body>
</html>

Iterating Over Map Entries

<c:foreach> can also be used to iterate over entries of a java.util.Map object. Each iteration gives you access to an entry of the map, allowing you to work with both the key and value.

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<!DOCTYPE html>
<html>
<head>
    <title>Iterating Over Map Entries Example</title>
</head>
<body>
    <% 
        java.util.Map<String, String> userRoles = new java.util.HashMap<>();
        userRoles.put("Alice", "Admin");
        userRoles.put("Bob", "User");
        userRoles.put("Charlie", "Guest");
        request.setAttribute("userRoles", userRoles);
    %>
    <c:foreach items="${userRoles}" var="entry">
        <p>User: ${entry.key}, Role: ${entry.value}</p>
    </c:foreach>
</body>
</html>

Conclusion

This tutorial has taught you how to use the <c:foreach> tag in JSTL to iterate over collections, arrays, number ranges, and map entries in JSP. Following the steps outlined in this tutorial, you can easily create dynamic web pages using JSP and JSTL.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram