JSTL Core choose when otherwise Tag


JSTL Core choose when otherwise Tag

JSTL Core choose when otherwise Tag is similar to switch case statement of java and it is use to execute single statement from multiple cases.

Syntax

<c:choose>
 <c:when test="condition"> 
  ..
 </c:when>
 <c:when test="condition"> 
 ...
 </c:when>
 <c:when test="condition"> 
 ...
 </c:when>
 <c:otherwise> 
  default statement.
 </c:otherwise>
 </c:choose>

Here <c:choose> is similar switch of java and <c:when> is similar to case of java and <otherwise> is similar to default case of java.

JSTL Core choose when otherwise Tag Example

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL Examples</title>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
</head>
<body>
 <c:set var="age" scope="request" value="${20}" />

 <c:choose>
 <c:when test="${age <= 14}"> 
 You are under teen age. 
 </c:when>
 <c:when test="${age >= 18}"> 
 You are eligible for vote. 
 </c:when>
 <c:when test="${age >= 30}"> 
 You are eligible for marriage.
 </c:when>
 <c:otherwise> 
 Age not matched in switch case.
 </c:otherwise>
 </c:choose>
</body>
</html>

Output

 You are eligible for vote.






Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229