JSTL Core Tag choose, when and otherwise

JSTL Core Tag choose, when and otherwise


Posted in : Java Posted on : April 1, 2012 at 6:55 PM Comments : [ 0 ]

In this tutorial your will learn about the JSTL Core flow control tag choose, when and otherwise.

JSTL Core Tag choose, when and otherwise

In this tutorial your will learn about the JSTL Core flow control tag choose, when and otherwise.

In the above tags when and otherwise are the sub-tags of choose tag. The <c:choose> tag executes the conditional block statements which is embedded with the sub-tags <c:when> and/or <c:otherwise>. An otherwise tag (if present) is evaluated if when or nested when tag attribute 'test' condition is not evaluated to true. The choose, when, and otherwise tag is look like the switch statement where you have the multiple alternative cases to select one and if no one the alternative case is selected the default clause is executed. These tags can also be used to create an if-then-else statements.

Attributes of the following tags :

  • <c:choose>    : This tag has no attribute.
  • <c:when>       :This tag has one attribute.
    • test       :This attribute is used for providing a conditional statement to evaluate.
  • <c:otherwise>: This tag has no attribute.

Example

An example that I am going to give here, is very simple which will demonstrate you how to use the above mentioned JSTL core flow control tags. In the example given below I have first set the value of variables using <c:set> tag and then used the JSTL core tag choose, when, and otherwise. From these tags 'when' and 'otherwise' are the subtags and these tags executes the conditional statements specified by the <c:when test=""> tag. In this example in <c:when> tag I am trying to compare the two strings so as an output if the specified condition satisfies then the statement of <c:when> tag will be executed and if the condition is not satisfied then the statement of <c:otherwise> tag will be executed.

JstlChooseWhenOtherwiseExample.jsp

<%@ 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">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Example</title>
</head>
<body>
<h3>JSTL Core Tag choose, when, otherwise Example.</h3>
<c:set var="str" value="Dev"/>
<c:set var="str1" value="manuals"/>
<c:choose>
<c:when test="${str eq 'Dev'}">
<%-- <c:set var="concat" value="${str}${str1}" /> --%>
<%-- <c:out value="${concat}"></c:out> --%>
<b>"${str}${str1}"</b>
</c:when>
<c:otherwise>
<b>"${str}"</b>
</c:otherwise>
</c:choose>

</body>
</html>

How to Run this example

Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :

  • jstl.jar
  • standard.jar

After adding of these jar files you may execute your program in the following ways :

  • Select JstlChooseWhenOtherwiseExample.jsp page of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlChooseWhenOtherwiseExample.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
  • A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Choose your server -> Finish

NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.

Output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics