jstl if else string comparison

 In JavaServer Pages (JSP), the JSTL (Java Standard Tag Library) provides a set of tags that you can use to perform common tasks, such as iterating over a collection of data, performing conditional logic, and formatting text. The JSTL if tag is used to perform conditional logic in a JSP page. The if tag evaluates a boolean expression, and if the expression evaluates to true, the content inside the if tag is rendered to the response. If the expression evaluates to false, the content inside the if tag is not rendered.

JSTL (JavaServer Pages Standard Tag Library) is a collection of tags that provide common functionality when working with JSPs (JavaServer Pages). One of the tags provided by JSTL is the <c:if> tag, which can be used to conditionally include content in a JSP based on a Boolean expression.

Here's an example of how you might use the JSTL if tag to conditionally render content in a JSP page:

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

<c:if test="${someCondition}">

  <p>This content will be displayed if someCondition is true</p>

</c:if>

The JSTL if tag also provides an else branch which will be executed if the test conditions return false

jstl if else


Additionally, you can also use c:choose, c:when, c:otherwise tags for conditions where you have more than one test case.


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

<c:choose>
  <c:when test="${someCondition}">
    <p>This content will be displayed if someCondition is true</p>
  </c:when>
  <c:when test="${anotherCondition}">
    <p>This content will be displayed if anotherCondition is true</p>
  </c:when>
  <c:otherwise>
    <p>This content will be displayed if neither someCondition nor anotherCondition is true</p>
  </c:otherwise>
</c:choose>
Please note that to use the above-mentioned tags, you need to include JSTL core library in your project.

JSP Jstl if else statement with mutilple conditions

  • JSTL means Java Server pages standard Tag Library.
  • JSTL is a collection of useful JSP tags to simplify the JSP development.
  • Lets see how to write  if and if else statements  in java server pages using JSTL



JSTL If condition in JSP :

  • We can use JSTL tags by providing 
  • <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Program  #1: Write a program to how to use JSTL if condition in Java server pages


  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2.     pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
  4. /TR/html4/loose.dtd">
  5.  
  6. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  7. <html>
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  10. <title>JSTL if condition</title>
  11. </head>
  12. <body>
  13.  
  14.  <c:set var="age" scope="session" value="${20}"/>
  15. <c:if test="${age > 18}">
  16.    <p>My age is: <c:out value="${age}"/><p>
  17. </c:if>
  18. </body>
  19. </html>
Output:


jstl if condition
JSTL If  else condition in JSP :
  • We use  c:when and c:otherwise tags in JSTL like if else in java

Program  #2: Write a program to how to use JSTL if  else condition in Java server pages


  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2.     pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
  4. /TR/html4/loose.dtd">
  5.  
  6. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  7. <html>
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  10. <title>JSTL if else condition</title>
  11. </head>
  12. <body>
  13.  
  14.  <h1>c:when, c:otherwise, c:choose</h1>  
  15.  
  16. <c:set  var="year" value="2016" ></c:set>  
  17. <c:choose>  
  18. <c:when test="${year%4==0}">  
  19. <c:out value="leap year"></c:out>  
  20. </c:when>  
  21. <c:otherwise>  
  22. <c:out value="Not Leap year"></c:out>  
  23. </c:otherwise>  
  24. </c:choose>
  25.  
  26. </body>
  27. </html>
Output:


jstl if else statement





JSTL  multiple If  else conditions in JSP :
  • We use  c:when and c:otherwise tags in JSTL like if else if else in java server pages.
  • Lest wee how to use if else ladder and compare string in jstl  multiple if else conditions.
  • Jstl if else statement multiple conditions

Program  #3: Write a program to how to use  comparing string in JSTL multiple  if  else condition in Java server pages


  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2.     pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
  4. /TR/html4/loose.dtd">
  5.  
  6. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  7. <html>
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  10. <title>JSTL multiple if else condition/ if else ladder</title>
  11. </head>
  12. <body>
  13.  
  14. <c:set  var="Day" value="Friday" ></c:set>  
  15. <c:choose>  
  16. <c:when test="${Day=='Sunday'}">  
  17. <c:out value="Its SunDay"></c:out>  
  18. </c:when> 
  19. <c:when test="${Day=='Monday'}"> 
  20. <c:out value="Its MonDay"></c:out>  
  21. </c:when>  
  22. <c:when test="${Day=='Tuesday'}"> 
  23. <c:out value="Its TuesDay"></c:out>  
  24. </c:when>  
  25. <c:when test="${Day=='Wednesday'}"> 
  26. <c:out value="Its Wednesday"></c:out>  
  27. </c:when>  
  28. <c:when test="${Day=='Thursday'}"> 
  29. <c:out value="Its ThursDay"></c:out>  
  30. </c:when>  
  31. <c:when test="${Day=='Friday'}"> 
  32. <c:out value="Its FriDay"></c:out>  
  33. </c:when>  
  34. <c:otherwise>  
  35. <c:out value="Its Saturday"></c:out>  
  36. </c:otherwise>  
  37. </c:choose>  
  38.  
  39. </body>
  40. </html>
Output:


jstl if statement multiple conditions
Select Menu