Easy Tutorial
❮ Jsp Life Cycle Jsp Directives ❯

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

JSP Standard Tag Library

The <c:choose> tag functions similarly to the Java switch statement, used for making a choice among multiple options.

The switch statement has cases, and the <c:choose> tag corresponds with <c:when>. The switch statement has a default case, and the <c:choose> tag has <c:otherwise>.

Syntax

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

Attributes

Attribute Description Required Default Value
test Condition Yes None

Example

<%@ 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>
&lt;c:set var="salary" scope="session" value="${2000*2}"/>
<p>Your salary is : <c:out value="${salary}"/></p>
<c:choose>
    &lt;c:when test="${salary <= 0}">
       Too bad.
    </c:when>
    &lt;c:when test="${salary > 1000}">
       Good salary, can live.
    </c:when>
    <c:otherwise>
       Nothing.
    </c:otherwise>
</c:choose>
</body>
</html>

Output:

Your salary is : 4000

Good salary, can live.

JSP Standard Tag Library

❮ Jsp Life Cycle Jsp Directives ❯