20.3.8 <c:choose> , <c:when> , <c:otherwise>
These tags are similar to switch statement with default case in java.
Consider <c:choose> as a switch , multiple <c:when> as case and <c:otherwise> as default which means we can have a multiple <c:when> and single <c:when> in a <c:choose> tag.
Syntax is like below –
<c:choose> <c:when test=”” /> </c:when> <c:when test=”” /> </c:when> <c:otherwise> </c:otherwise> </c:choose>
<choose> and <c:otherwise> do not have any attributes but <c:when> has a mandatory test attribute
Lets create cchoose.jsp to demonstrate the <c:choose> tag like below
<html> <head> <title> c:choose tag example </title> </head> <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %> <body> <c:setvar="bonus" value="2000" /> <c:choose> <c:when test="${bonus < 1000}"> <c:out value="Bonus is less than $ 1000!!!" /> </c:when> <c:when test="${bonus > 1000}"> <c:out value="Bonus is greater than $ 1000!!!" /> </c:when> <c:otherwise> <c:out value="No conditions match" /> </c:otherwise> </c:choose> </body> </html>
Access cchoose.jsp by hitting http://localhost:8080/jsp-tutorial/cchoose.jsp
20.3.9 <c:forEach>
This tags is similar to loops in java and used for iteration over a collection.
Following attributes can be used with this tag
a)var- counter variable name
b)items-scoped collections on which iteration has to perform
c)begin – is the initial counter value.
d)end- last value or limit
e)step- increment to be done in counter value after each run
f)varStatus- variable to expose the loop status
Lets create cforEach.jsp to demonstrate the <c:forEach> tag like below
<html> <head> <title> c:forEach tag example </title> </head> <%@page import="java.util.*" %> <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %> <body> <c:forEachvar="i" begin="1" end="10"> <c:out value="Value of counter is ${i} "></c:out> <br/> </c:forEach> <hr/> <c:forEachvar="i" begin="1" end="10" step="2"> <c:out value="Value of counter is ${i} with step 2"></c:out> <br/> </c:forEach> <hr/> <% List<String> list = newArrayList<String>(); for(inti=0;i<10;i++) { list.add("Item" + i); } request.setAttribute("list", list); %> <c:forEach items="${list}"var="item" begin="0" end="9"varStatus="i"> Value at ${varStatus.index} is : ${item} <br/> </c:forEach> </body> </html>
Access cforEach.jsp by hitting http://localhost:8080/jsp-tutorial/cforEach.jsp
20.3.10 <c:forTokens>
This tags is similar to <c:forEach> and is used break a string into tokens and iterate through each of the tokens.
Following attributes can be used with this tag
a)var- counter variable name
b)items- scoped collections on which iteration has to perform
c)begin – is the initial counter value.
d)end- last value or limit
e)step- increment to be done in counter value after each run
f)varStatus- variable to expose the loop status
g)delims- characters to use as delimeter.
Lets create cforTokens.jsp to demonstrate the <c:forTokens> tag like below
<html> <head> <title> c:forTokens tag example </title> </head> <%@page import="java.util.*" %> <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %> <body> <c:forTokens items="Apple,Orange,Banana"var="i"delims=","> <c:out value="Value is ${i} "></c:out> <br/> </c:forTokens> <hr/> </body> </html>
Access cforTokens.jsp by hitting http://localhost:8080/jsp-tutorial/cforTokens.jsp