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

SHARE & COMMENT :

JSTL Core <c:choose> Tag

The <c:choose> tag of JSP core tag library is used for conditional execution of statement. The choose tag is used to construct an <c:if> statement. The <c:choose> tag acts like a java switch statement. The <c:choose> tag executes the conditional block statements which is embedded with the sub-tags <c:when> and <c:otherwise>.

The syntax of <c:choose> Tag

<c:choose> body content </c:choose>

The <c:choose> tag has no attribute.

JSTL Core <c:when> Tag.

It is a subtag of <c:choose> tag .<c:when> tag is like the block of if control statements which executes when the condition is true. The <c:when> tag encloses a single case within the <c:choose> tag.

The syntax of <c:when> Tag

<c:when attribute> body content </c:when>

Attribute used in <c:when> Tag

There is only one attribute used in the <c:when> tag that is test. test attribute is used to provide conditional statement for evaluation.

JSTL Core <c:otherwise> Tag

<c:otherwise> tag is similar to default statement which works when all the statements of <c:otherwise> holds false. This tag is like else of if control statement of java program.This is also a subtag of <choose> tag .
The <c:otherwise> tag is evaluated if <c:when> or nested <c:when> tag attribute test condition is not evaluated to true.

syntax of <c:otherwise> Tag

<c:otherwise> body content </c:otherwise>

The body content of the <c:otherwise> tag is evaluated if none of the <c:when> conditions in the <c:choose> tag are resolved to true. There is a no attribute for <c:otherwise> tag.

Example using <c:choose>, <c:when> and <c:otherwise> Tags

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<span style="font-size: 12px; line-height: 18px;">    pageEncoding="ISO-8859-1"%></span>
<span style="font-size: 12px; line-height: 18px;"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" </span>
<span style="font-size: 12px; line-height: 18px;">"http://www.w3.org/TR/html4/loose.dtd"></span>
<span style="font-size: 12px; line-height: 18px;"><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %></span>
<span style="font-size: 12px; line-height: 18px;"><html></span>
<span style="font-size: 12px; line-height: 18px;"><head></span>
<span style="font-size: 12px; line-height: 18px;"><title>c:choose, c:when and c:otherwise Tag Example</title></span>
<span style="font-size: 12px; line-height: 18px;"></head></span>
<span style="font-size: 12px; line-height: 18px;"><body></span>
<span style="font-size: 12px; line-height: 18px;"><c:set var="number1" value="${6546}"/></span>
<span style="font-size: 12px; line-height: 18px;"><c:set var="number2" value="${12}"/></span>
<span style="font-size: 12px; line-height: 18px;"><c:set var="number3" value="${10}"/></span>
<span style="font-size: 12px; line-height: 18px;"><c:choose></span>
<span style="font-size: 12px; line-height: 18px;"> <c:when test="${number1 < number2}"></span>
<span style="font-size: 12px; line-height: 18px;">     ${"number1 is less than number2"}</span>
<span style="font-size: 12px; line-height: 18px;"> </c:when></span>
 <c:when test="${number1 <= number3}">
<span style="font-size: 12px; line-height: 18px;">     ${"number1 is less than equal to number2"}</span>
<span style="font-size: 12px; line-height: 18px;"> </c:when></span>
<span style="font-size: 12px; line-height: 18px;"> <c:otherwise></span>
<span style="font-size: 12px; line-height: 18px;">     ${"number1 is largest number!"}</span>
<span style="font-size: 12px; line-height: 18px;"> </c:otherwise></span>
<span style="font-size: 12px; line-height: 18px;"></c:choose></span>
<span style="font-size: 12px; line-height: 18px;"></body></span>
<span style="font-size: 12px; line-height: 18px;"></html></span>

How To Run?

  • Save this file as example.jsp in your eclipse IDE.
  • Now select this jsp file, right mouse click and select Run as ->Run on server

Output

When the execution process is completed successfully we will get the following output :
jstl_core c choose tag_demo

Above example demonstrate the use of <c:choose>, <c:when> and <c:otherwise> tags. In the above example we used the <c:set> tag first for assigning the value for variables and the we use the <c:when> and <c:otherwise> tags. These tags are sub tags of <c:choose>. These tags execute the the conditional statements specified by the <c:when test=””>.
Above example demonstrate the comparisons between three numbers and displays the greater number.

 

Previous Tutorial : JSTL Core c:if Tag :: Next Tutorial : JSTL Core c:import Tag

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better