BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

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

By Chaitanya Singh | Filed Under: JSTL

In this article we are discussing <c:choose>, <c:when> and <c:otherwise> core tags of JSTL. These tags are used together like switch-case and default statements in java. <c:choose> is the one which acts like switch, <c:when> like case which can be used multiple times inside <c:choose> for evaluating different-2 conditions. <c:otherwise> is similar to default statement which works when all the <c:when> statements holds false.

Syntax:

The basic structure looks like this –

<c:choose>
    <c:when test="${condition1}">
       //do something if condition1 is true
    </c:when>
    <c:when test="${condition2}">
        //do something if condition2 is true
    </c:when>
    <c:otherwise>
        //Statements which gets executed when all <c:when> tests are false.
    </c:otherwise>
</c:choose>

Example

In this example we have three numbers and we are comparing them using these three core tags. Example is pretty simple to understand. Screenshot of output is provided after the example’s code.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>c:choose, c:when and c:otherwise Tag Example</title>
</head>
<body>
<c:set var="number1" value="${222}"/>
<c:set var="number2" value="${12}"/>
<c:set var="number3" value="${10}"/>
<c:choose>
 <c:when test="${number1 < number2}">
     ${"number1 is less than number2"}
 </c:when>
 <c:when test="${number1 <= number3}">
     ${"number1 is less than equal to number2"}
 </c:when>
 <c:otherwise>
     ${"number1 is largest number!"}
 </c:otherwise>
</c:choose>
</body>
</html>

Output

c-choose-example

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSTL Core Tags

  • c:out
  • c:set
  • c:remove
  • c:if
  • c:choose
  • c:when
  • c:otherwise
  • c:catch
  • c:import
  • c:forEach
  • c:forTokens
  • c:param
  • c:url
  • c:redirect
JSTL Functions
  • fn:contains()
  • fn:containsIgnoreCase()
  • fn:indexOf()
  • fn:escapeXML()
  • fn:join() & fn:split()
  • fn:length()
  • fn:startsWith()
  • fn:endsWith()
  • fn:substring()
  • fn:substringAfter()
  • fn:substringBefore()
  • fn:trim()
  • fn:toUpperCase()
  • fn:toLowerCase()
  • fn:replace()

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap