Reading:
If - Else Condition in JSP

If - Else Condition in JSP

Metamug
If - Else Condition in JSP

c:if, c:when and c:otherwise tags in JSP

All of the tags are used as Conditional Statement, to control the flow of the program. When we need to run some code based upon some condition we use these tags to control the flow of the program. It’s same as ‘if’ and ‘if-else’ statement in other programming languages.

** To use any of the c tags, this library must be included

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

c:if

The c:if tag can be used like this. It is similar to the ‘if’ statement

<c:set var = "salary" value = "${2500}"/>
<c:if test = "${salary > 1000}">
<c:out value = "${salary}"/>
</c:if>

Anything inside the tag will execute only when the condition is True. The condition is written in the ‘test’ attribute, here it is ‘salary > 1000’.

The c:set tag is used to set the value of a variable by var and value attribute and the c:out tag is used to display the value of a variable.

c:choose , c:when and c:otherwise

It can be used like this. It is similar to the ‘if-else’ statement.

<c:set var = "salary" value = "${2500}"/>
<c:choose>
<c:when test="${salary < 1000}">
<c:out value = "${salary ‘is less than 1000’}"/>
</c:when>
<c:otherwise>
<c:out value = "${salary ‘is greater than or equals to 1000’}"/>
</c:otherwise>
</c:choose>

Anything inside the c:when tag will be executed only when the condition is True. If the condition is false then c:otherwise tag will execute. And both of these are written inside the c:choose tag.



Icon For Arrow-up
Comments

Post a comment