Easy Tutorial
❮ Jsp Custom Tags Jstl Function Tolowercase ❯

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

JSP Standard Tag Library

The <x:choose> tag functions similarly to the Java switch statement. The switch statement has case statements, while the <x:choose> tag has <x:when> tags. The switch statement has a default statement, while the <x:choose> tag has an <x:otherwise> tag.

Syntax

<x:choose>
 &lt;x:when select="<string>">
     ...
 </x:when>
 &lt;x:when select="<string>">
     ...
 </x:when>
     ...
     ...
 <x:otherwise>
     ...
 </x:otherwise>
</x:choose>

Attributes

Attributes for <x:when> tag:

Attribute Description Required Default Value
select Condition Yes None

Example

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<html>
<head>
  <title>JSTL x:choose Tag</title>
</head>
<body>
<h3>Books Info:</h3>

&lt;c:set var="xmltext">
  <books>
    <book>
      <name>Padam History</name>
      <author>ZARA</author>
      <price>100</price>
    </book>
    <book>
      <name>Great Mistry</name>
      <author>NUHA</author>
      <price>2000</price>
    </book>
  </books>
</c:set>

&lt;x:parse xml="${xmltext}" var="output"/>
<x:choose>
   &lt;x:when select="$output//book/author = 'ZARA'">
      Book is written by ZARA
   </x:when>
   &lt;x:when select="$output//book/author = 'NUHA'">
      Book is written by NUHA
   </x:when>
   <x:otherwise>
      Unknown author.
   </x:otherwise>
</x:choose>

</body>
</html>

The output is as follows:

BOOKS INFO:
Book is written by ZARA

JSP Standard Tag Library

❮ Jsp Custom Tags Jstl Function Tolowercase ❯