Relational Operators in JSTL
JSTL provides comparison operators that can be used within EL expressions:
-lt— less than-le— less than or equal to-gt— greater than-ge— greater than or equal to-eq— equal to-ne— not equal to
Checking for null or empty values:
<c:if test="${empty items}">
<!-- Handle empty case -->
</c:if>
Pratcical Examples
Example 1: Comparing loop counter with request parameter
<c:forEach var="index" begin="1" end="${maxIterations}">
<c:if test="${selectedIndex eq index}">
<a href="product_detail.action?id=${index}" style="color: #000">[${index}]</a>
</c:if>
<c:if test="${selectedIndex ne index}">
<a href="product_detail.action?id=${index}" style="color: red">[${index}]</a>
</c:if>
</c:forEach>
Example 2: Using c:choose for if-else logic
<c:choose>
<c:when test="${selectedIndex eq currentIndex}">
Selected item: ${currentIndex}
</c:when>
<c:otherwise>
Unselected item: ${currentIndex}
</c:otherwise>
</c:choose>
Example 3: Iterating with status and conditional display
<c:forEach items="${productList}" var="product" varStatus="loopStatus">
<li>
<c:if test="${loopStatus.index < 4}">
<a href="product_details.action?id=${product.id}">
<img src="${product.imageUrl}">
</a>
<div class="product-name">${product.name}</div>
<div class="price">¥${product.cost}</div>
</c:if>
</li>
</c:forEach>
Using c:choose for Conditional Logic
When you need if-else functionality rather than just if, use <c:choose> with <c:when> and <c:otherwise>:
<c:choose>
<c:when test="${condition1}">
First condition is true
</c:when>
<c:when test="${condition2}">
Second condition is true
</c:when>
<c:otherwise>
No conditions matched
</c:otherwise>
</c:choose>
Key points:
<c:otherwise>must appear after all<c:when>tags within a<c:choose>- If no
<c:when>tests evaluate to true, the<c:otherwise>block executes - Each
<c:choose>can have only one<c:otherwise>