JSP Standard Tag Library(JSTL) is a standard library of readymade tags. The JSTL contains several tags that can remove scriplet code from a JSP page by providing some ready to use, already implemented common functionalities.
JSTL is divided into 5 groups:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
The JSTL core library contains several tags that can be used to eliminate the basic scripting overhead such as for
loop, if...else
conditions etc from a JSP Page. Let's study some important tags of JSTL Core library.
if
tag, the body is evaluated only when the expression is true. For Example :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:if test="${param.name == 'studytonight'}">
<p>Welcome to ${param.name} </p>
</c:if>
</body>
</html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:out value="${param.name}" default="StudyTonight" />
</body>
</html>
The value
attribute specifies the expression to be written to the JspWriter. The default
attribute specifies the value to be written if the expression evaluates null.
forEach
tag works similarly to enhanced for loop of Java Technology. You can use this tag to iterate over an existing collection of items. For Example :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:forEach var="message" items="${errorMsgs}" >
<li>${message}</li>
</c:forEach>
</body>
</html>
Here the attribute items has its value as an EL expression which is a collection of error messages. Each item in the iteration will be stored in a variable called message which will be available in the body of the forEach tag.
when
tag evaluates to true, then the content within when
tag is evaluated, otherwise the content within the otherwise
tag is evaluated.
We can also implement if-else-if
construct by using multiple when tag. The when tags are mutually exclusive, that means the first when tag which evaluates to true is evaluated and then, the control exits the choose
block. If none of the when condition evaluates to true, then otherwise
condition is evaluated. For Example
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:forEach var="tutorial" items="${MyTutorialMap}" begin="0" end="5" varStatus="status">
<c:choose>
<c:when test="${status.count %2 == 0 }">
<p> Divisible by 2 : ${tutorial.key} </p>
<br/>
</c:when>
<c:when test="${status.count %5 == 0 }">
<p > Divisible by 5 : ${tutorial.key} </p>
<br/>
</c:when>
<c:otherwise>
<p> Neither divisible by 2 nor 5 : ${tutorial.key} </p><br/>
</c:otherwise>
</c:choose>
</c:forEach>
</body>
</html>
< c:import>
tag is used to dynamically add the contents from the provided URL to the current page, at request time. The URL resource used in the < c:import>
url attribute can be from outside the web Container. For Example :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:import url="http://www.example.com/hello.html">>
<c:param name="showproducts" value="true"/>
</c:import>
</body>
</html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<a href='<c:url value="/home.jsp"/>' > Go Home </a>
</body>
</html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:set target="student" property="name" value="${param.name}" />
</body>
</html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:catch>
<% int a = 0;
int b = 10;
int c = b/a;
%>
</c:catch>
</body>
</html>