JSTL If Else Example

JSTL If Else Example explains about testing different conditions as per the requirements. It also provides a secondary path (else case) of execution, when an "if" conditions become false.

We have already seen the usage of JSTL If Example

How to use if-else option in JSTL?
 

There are 2 ways, we can achieve if-else statements using JSTL

1) You can use JSTL C:Choose C:When C:Otherwise Tag
2) You can use JSTL Mod (modulus) operator
 

You can see the below example, which is demonstrating JSTL If Else Example Using Mod operator

Required Libraries

You need to download

  1. Tomcat 9
  2. JSTL 1.2

Following jar must be in classpath

  1. jstl-1.2.jar

JSTL If Else Example

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

<head>
<title>JSTL If Else Example</title>
</head>

<body>

	<c:set value="18" var="age" />
	<c:out value="${num % 2 eq 0 ? 'even': 'odd'}" />

</body>
</html>

Output
 even 
  








Your email address will not be published. Required fields are marked *