English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

<c:choose>, <c:when>, <c:otherwise> tags

JSP Standard Tag Library

The <c:choose> tag has the same function as the Java switch statement and is used to make a choice among many options.

In a switch statement, there is a case, and in the <c:choose> tag, there is a corresponding <c:when>. In a switch statement, there is a default, and in the <c:choose> tag, there is a <c:otherwise>.

Syntax format

<c:choose>
    <c:when test="<boolean>">
        ...
    </c:when>
    <c:when test="<boolean>">
        ...
    </c:when>
    ...
    ...
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>

Attribute

  • <c:choose> tag has no attributes.
  • <c:when> tag has only one attribute, which is listed in the table below.
  • <c:otherwise> tag has no attributes.

The attributes of the <c:when> tag are as follows:

Attribute Description Is Necessary Default Value
test Condition Yes None

Example Demonstration

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>c:choose Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2"/>
<p>Your salary is : <c:out value="${salary}"/></p>
<c:choose>
    <c:when test="${salary} <= 0">
       Too bad.
    </c:when>
    <c:when test="${salary}> 1000}>
       Good salary, can live well.
    </c:when>
    <c:otherwise>
        Nothing.
    </c:otherwise>
</c:choose>
</body>
</html>

The running result is as follows:

Your salary is : 4000
Good salary, can live well.

JSP Standard Tag Library