JSTL If- Else

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

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.