Home Jsp Simple-jsp-example JSTL If- Else



JSTL If- Else
Posted on: March 12, 2008 at 12:00 AM
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.

Related Tags for JSTL If- Else:
javacweblibrarycomstlantimportjariomakeuserproblemjstldefaultportvaluetagstateusingolefolderthisooapplikejsifconditionforcabwithworkprogrammatchstatementtostandardramldcieilitlibnotlsliuseulpeobsoleteimfirstenterfaultinnorarstamnttrwanthintldoscaosletjndaadesspectrueweb-infemfalsemepropp.jardoctlxawhichuatsspsofoldatanyoldkisirhainfllivfollowandarstatactrtwantwingxactvassrdthcondavstabablatiapaluhatefaulticaicaplprndonomogrolo


More Tutorials from this section

Ask Questions?    Discuss: JSTL If- Else   View All Comments

Post your Comment


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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.