Share on Google+Share on Google+

jstl if else

Here we have given a simple example to explain the implementation of if-else tag in JSTL.

In this section we have discussed about how to implement if-else tag in JSTL using a simple example.

<c:if> tag does not work if the value entered does not match with any of the condition given in the program, as their is not default value.

Hence, if-else tag is not implemented directly but is implemented with the help of some tags:

<c:choose> tag : works like if-else statement in a java programming. It performs conditional operations using <when> and <otherwise>.

<c:when> tag is a sub tag of <c:choose> and provides boolean expression to evaluate. If it is true rest of the body is evaluated.

<c:otherwise> tag is a sub tag of <c:choose> and is evaluated when condition provided to the <c:when> tag is false.

For this program to work, one must have jstl.jar and standard.jar in /WEB-INF/lib folder.


<title>How to Use Choose,Otherwise and When</title>

<body bgcolor = "#FFFFCC">
<form method="get">Enter a number from one onwards :
<input type="text" name="number" /><br>
<input type="submit" value="Submit" />

<c:if test="${pageContext.request.method=='GET'}">So, you want only
<c:out value="${param.number}" />

<c:when test="${param.number=='1'}">banana.







Posted on: October 10, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: jstl if else  

Post your Comment

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