JSTL List Count Using varStatus

JSTL List Count Using varStatus Example explains about finding JSTL List Count Using varStatus

Consider a JSP page, which contains an ArrayList and we need to find the exact count of that particular ArrayList

For this by declaring variable varStatus="status" in JSTL forEach, you can access all the available methods of status object inside JSP page

You can use JSTL 'status.count' for finding the number of elements available on that particular ArrayList.

This JSTL 'status.count' is available for all the List / Collection implementations in java

you can see the below example demonstrating JSTL 'status.count'

Note

Please check the table, You can iterate following table items using c:forEach tag

JSTL foreach Collections

Required Libraries

You need to download

  1. Tomcat 9
  2. JSTL 1.2

Following jar must be in classpath

  1. jstl-1.2.jar

Project Structure

JSTL List Count Using varStatus

JSTL List Count Using varStatus

// Getting The List Size In JSP

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.ArrayList"%>
<%
    ArrayList<String> numList = new ArrayList<String>();
    numList.add("one");
    numList.add("two");
    numList.add("three");
    request.setAttribute("numList", numList);
%>
<html>
<body>
    <c:forEach items="${numList}" var="item" varStatus="status">
        ${status.count}) ${item}<br/>
    </c:forEach>
</body>
</html>
Output
1) one
2) two
3) three










Your email address will not be published. Required fields are marked *