Conditional loop example in JSTL

In this post we have seen how to loop through collection in JSTL. Here we will see how to use conditions while looping through JSTL. Consider we are printing a collection of book in JSP. Now let’s say that when we print the ?rst ten books, we want to highlight the ?rst ?ve. This is done by adding in a collaboration with the conditional action.

<table> 
 <c:forEach var="book" items="${books}" varStatus="status" begin="0" end="9"> 
  <tr> 
   <c:choose> 
	<c:when test="${status.count < 4}"> 
	  <td bgcolor="#FFFF00"> <c:out value="${book.title}"/> </td> 
	</c:when> 
	<c:otherwise> 
	  <td><c:out value="${book.title}"/></td> 
	</c:otherwise> 
   </c:choose> 
  </tr>
 </c:forEach> 
</table>

 

JSP-SERVLET JSTL