Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp Simple-jsp-example JSTL If- Else

Related Tutorials


 
 

Share on Google+Share on Google+

JSTL If- Else

Advertisement
The problem with is that, this tag will not work if the value entered by the user doesn't match any of the condition given in the program. In this tag there is no way to specify the default value.

JSTL If- Else

        

The problem with <c:if> is that, this tag will not work if the value entered by the user doesn't match any of the condition given in the program. In this tag there is no way to specify the default value. The <c:if> tag is not for applicable if we want to do one thing if the condition is true, and another thing if the condition is false. In this situation this tag will become obsolete.  Here comes the use of  the <c:choose> tag. This tag works like a if- else statement in a java

To make a program on this, it is important to use the following tld's in the lib folder. i.e. /WEB-INF/lib folder, first is jstl.jar and the second one is standard.jar. We have made a program on if- Else statement using jstl library which will exactly work like the way this statement works in java. 

The code of the program is given below:

 

 

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
<title>How to Use Choose,Otherwise and When</title>
</head>

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

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

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

</c:when>

<c:otherwise>bananas.

</c:otherwise>
</c:choose>
</c:if>
</body>
</html>

The output of the program is given below:

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: March 12, 2008

Related Tutorials

Discuss: JSTL If- Else   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
myur
January 18, 2012
thanks

thanks!
Ben
February 28, 2012
Thanks

Cool. Brief concise and easy to follow. Thanks!
Chris
June 16, 2012
comment box

Your comment box's (this box, lol) has very little contrast between the background-color of the parent box and the input fields. I can barely tell where an in put field starts. Try changing a color :-) cheers.
DMCA.com