The JSTL <c:if> Core Tag is used in the control flow. It evaluates an expression and executes a specific block of code if the result returns true.
Syntax:
<c:if test ="testCondition"> //block of code </c:if>
c:if tag attributes:
Attribute | Description | Required |
test | It specifies the condition to evaluate. | Yes |
var | It specifies the name of the variable to store the condition’s result. | No |
scope | It specifies the scope of the variable to store the condition’s result. | No |
Example:
test.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>c:if JSTL core tag example</title> </head> <body> <c:set var="num" value="100"/> <c:if test="${num > 0}"> Num = <c:out value="${num}"/> </c:if> </body> </html>
web.xml
<web-app> <welcome-file-list> <welcome-file>test.jsp</welcome-file> </welcome-file-list> </web-app>