JSTL
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:
- JSTL Core: JSTL Core provides several core tags such as if, forEach, import, out etc to support some basic scripting task. Url to include JSTL Core Tag inside JSP page is →
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- JSTL Formatting: JSTL Formatting library provides tags to format text, date, number for Internationalised web sites. Url to include JSTL Formatting Tags inside JSP page is →
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
- JSTL sql: JSTL SQL library provides support for Relational Database Connection and tags to perform operations like insert, delete, update, select etc on SQL databases. Url to include JSTL SQL Tag inside JSP page is →
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
- JSTL XML: JSTL XML library provides support for XML processing. It provides flow control, transformation features etc. Url to include JSTL XML Tag inside JSP page is →
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
- JSTL functions: JSTL functions library provides support for string manipulation. Url to include JSTL Function Tag inside JSP page is →
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
JSTL Core Library
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.- JSTL if tag: The if tag is a conditional tag used to evaluate conditional expressions. When a body is supplied with
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>
- JSTL out tag: The out tag is used to evaluate an expression and write the result to JspWriter. For Example :
<%@ 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>
Thevalue
attribute specifies the expression to be written to the JspWriter. Thedefault
attribute specifies the value to be written if the expression evaluates null. - JSTL forEach tag: This tag provides a mechanism for iteration within a JSP page. JSTL
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. - JSTL choose, when, otherwise tag: These are conditional tags used to implement conditional operations. If the test condition of the
when
tag evaluates to true, then the content withinwhen
tag is evaluated, otherwise the content within theotherwise
tag is evaluated.We can also implementif-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 thechoose
block. If none of the when condition evaluates to true, thenotherwise
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>