C:Choose C:When C:Otherwise Example

C:Choose C:When C:Otherwise Example explains about How to use c:choose,c:when,c:otherwise tags inside JSTL

c:choose c:when c:otherwise tags are available as a part of JSTL core tags, JSTL c:choose providing the same functionality as java's switch statement provides

Consider an example where you need to deal with different values, but only one value is correct at a time, In that case  you can use C:Choose C:When C:Otherwise tag

c:when tag is for testing different conditions, it may have more than one c:when statements to be tested & c:otherwise is for the default condition to be executed

you can see the below example, which is demonstrating C:Choose C:When C:Otherwise Tag

Required Libraries

You need to download

  1. Tomcat 7
  2. JSTL 1.2

Following jar must be in classpath

  1. jstl-1.2.jar

JSTL c:choose,c:when,c:otherwise Tag Example

Generic format for c:choose,c:when is following

 

 

 

<c:choose> <c:when test="${condition1}"> ... </c:when> <c:when test="${condition2}"> ... </c:when> <c:otherwise> ... </c:otherwise> </c:choose>

 

 

 

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
	<html>
		<head>
			<title>JSTL c:choose,c:when,c:otherwise</title>
		</head>
		<body>
			<c:set scope="request" value="18" var="age" />
			Your age is : <c:out value="${age}" />
			<c:choose> 
				<c:when test="${age < 18}"> You are a minor </c:when> 
				<c:when test="${age >= 18}"> You are an adult </c:when> 
				<c:otherwise> Not applicable </c:otherwise> 
			</c:choose>
		</body>
	</html>
Output
Your age is : 18 
You are an adult 
  

 






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