- 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
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
- /TR/html4/loose.dtd">
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>JSTL if condition</title>
- </head>
- <body>
- <c:set var="age" scope="session" value="${20}"/>
- <c:if test="${age > 18}">
- <p>My age is: <c:out value="${age}"/><p>
- </c:if>
- </body>
- </html>
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
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
- /TR/html4/loose.dtd">
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>JSTL if else condition</title>
- </head>
- <body>
- <h1>c:when, c:otherwise, c:choose</h1>
- <c:set var="year" value="2016" ></c:set>
- <c:choose>
- <c:when test="${year%4==0}">
- <c:out value="leap year"></c:out>
- </c:when>
- <c:otherwise>
- <c:out value="Not Leap year"></c:out>
- </c:otherwise>
- </c:choose>
- </body>
- </html>
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
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
- /TR/html4/loose.dtd">
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>JSTL multiple if else condition/ if else ladder</title>
- </head>
- <body>
- <c:set var="Day" value="Friday" ></c:set>
- <c:choose>
- <c:when test="${Day=='Sunday'}">
- <c:out value="Its SunDay"></c:out>
- </c:when>
- <c:when test="${Day=='Monday'}">
- <c:out value="Its MonDay"></c:out>
- </c:when>
- <c:when test="${Day=='Tuesday'}">
- <c:out value="Its TuesDay"></c:out>
- </c:when>
- <c:when test="${Day=='Wednesday'}">
- <c:out value="Its Wednesday"></c:out>
- </c:when>
- <c:when test="${Day=='Thursday'}">
- <c:out value="Its ThursDay"></c:out>
- </c:when>
- <c:when test="${Day=='Friday'}">
- <c:out value="Its FriDay"></c:out>
- </c:when>
- <c:otherwise>
- <c:out value="Its Saturday"></c:out>
- </c:otherwise>
- </c:choose>
- </body>
- </html>
No comments