Add if else conditions in jsp using c:if c:choose jstl tags
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" %>
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.
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.