JSTL Core tag - choose() : Function

The <c:choose> works like a Java switch statement in that it lets you choose between a number of alternatives. Where the switch statement has case statements, the <c:choose> tag has <c:when> tags. A a switch statement has default clause to specify a default action and similar way <c:choose> has <c:otherwise> as default clause.

Attribute:

  • The <c:choose> tag does not have any attribute.

  • The <c:when> tag has one attributes which is listed below.

  • The <c:otherwise> tag does not have any attribute.

The <c:when> tag has following attributes:

AttributeDescriptionRequiredDefault
testCondition to evaluateYesNone

JSTL Example :

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Tag Example</title>
</head>
<body>
<c:set var="marks" scope="session" value="${30*3}"/>
<p>Marks obtained by Raj : <c:out value="${marks}"/></p>
<c:choose>
    <c:when test="${marks < 40}">
       You are failed
    </c:when>
    <c:when test="${marks > 80}">
       You got First class
    </c:when>
    <c:otherwise>
        You are average student.
    </c:otherwise>
</c:choose>
</body>
</html>

Output :

Marks obtained by Raj : 90
You got First class