JSTL c:choose , c:when and c:otherwise Core Tags

The JSTL <> Core Tag is used when a number of alternatives are available for a particular condition. It works same as of switch statement in java. The <> is like switch, <> is like case and <> is like default statement.

Syntax:

<c:choose>
 
    <c:when test="${testCondition1}">
       //block of statements
    </c:when>
 
    <c:when test="${testCondition2}">
        //block of statements
    </c:when>
 
      //Other <c:when>
 
    <c:otherwise>
        //block of statements
    </c:otherwise>
 
</c:choose>

c:when tag attributes:

Attribute Description Required
test It specify the condition to evaluate. No

 
Note: c:choose and c:otherwise tags does not have any attribute.

Example:

test.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 
<html>
	<head>
	 <title>
          c:choose, c:when and c:otherwise JSTL core tag example
         </title>
	</head>
	<body>
		<c:set var="num" value="100"/>
		<c:choose>
			<c:when test="${num > 10}">
				Number is greater than 10.
			</c:when>
			<c:otherwise>
				Number is less than or equal to 10.
			</c:otherwise>
		</c:choose>
	</body>
</html>

web.xml

<web-app>
 
  <welcome-file-list>
          <welcome-file>test.jsp</welcome-file>
  </welcome-file-list>	
 
</web-app>

Output:

jsp example 36
 
Download this example.
 
Next Topic: JSTL c:import Core Tag with example.
Previous Topic: JSTL c:if Core Tag with example.

Related Topics:

JSTL c:out Core Tag with example.
JSTL c:set Core Tag with example.
JSTL c:remove Core Tag with example.
JSTL c:catch Core Tag with example.
JSTL c:if Core Tag with example.
JSTL c:choose , c:when and c:otherwise Core Tags with example.
JSTL c:import Core Tag with example.
JSTL c:forEach Core Tag with example.
JSTL c:forTokens Core Tag with example.
JSTL c:url Core Tag with example.
JSTL c:param Core Tag with example.
JSTL c:redirect Core Tag with example.