
JSTL stands for JSP standard tag library. It was introduced to simplify JSP development as it avoids the use of Scriptlets.
Also we can reuse the tag in various pages so it results in faster development.
JSTL defines 5 types of tags which the developer can use to simplify the development.Let’s take a look into the table below.
Tag Name | Description |
---|---|
Core | The JSTL core tag provide variable support, URL management etc. The URL for the core tag is http://java.sun.com/jsp/jstl/core. The prefix of core tag is c. |
Function | The functions tags provide support for string manipulations etc. The URL for the functions tags is http://java.sun.com/jsp/jstl/functions and prefix is fn. |
Formatting | The Formatting tags provide support for message of date,number etc. The URL for the Formatting tags is http://java.sun.com/jsp/jstl/fmt and prefix is fmt. |
XML | The XML tags provide flow control, transformation, etc. The URL for the XML tags is http://java.sun.com/jsp/jstl/xml and prefix is x. |
SQL | The JSTL SQL tags basically provide for SQL related support. The URL for the SQL tags is http://java.sun.com/jsp/jstl/sql and prefix is sql. |
Let’s take an example from each of the above 5 types–
Core:
< c:out > – This tag works similarly on the lines of the JSP expression tag. It displays the values of a variable. It will display the result of an expression, similar to the way < %=…% > work.
Kindly check out the below example.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Core Tag Example</title>
</head>
<body>
<c:out value="${'James'}"/>
</body>
</html>
Functions:
fn:contains() – This JSTL function is used to check whether a sub string is present or not in a sample string.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>JSTL Functions for Contains Check</title>
</head>
<body>
<c:set var="String" value="Hello World"/>
<c:if test="${fn:contains(String, 'Wor')}">
<p>Found Wor<p>
</c:if>
<c:if test="${fn:contains(String, 'Wore')}">
<p>Found Wore<p>
</c:if>
</body>
</html>
O/P:

SQL:
<sql:query> – The <sql:query> tag is used for executing the SQL query which is defined under the tag. After executing the query, the result would be saved in th scoped variable. Take a look into the below example.
<sql:query dataSource="${ds}" var="rs">
SELECT * from Employees;
</sql:query>
Formatting:
<fmt:parseNumber> – This particular tag will parse an input number and show a particular value accoridng to the type in which it was parsed.
Kindly take a look into the below example.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>fmt:parseNumber tag example</title>
</head>
<body>
<h3>The fmt:parseNumber tag scenario is below:</h3>
<c:set var="Price" value="475.850" />
<fmt:parseNumber var="var1" type="number" value="${Price}" />
<p>Price is - <c:out value="${var1}" /></p>
<fmt:parseNumber var="var1" integerOnly="true" type="number" value="${Price}" />
<p>Price is - <c:out value="${var1}" /></p>
</body>
</html>
O/P:

XML<x:out> – The <x:out> tag is used for displaying the result of an XML Path expression and writes the result to JSP writer object. It is similar to the scriptlet tag <%= %> used in JSP.
Let’s take a look into the implementation below:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>XML Tags Test</title>
</head>
<body>
<h3>Employee Data:</h3>
<c:set var="employees">
<employees>
<employee>
<name>John</name>
<age>30</age>
</employee>
<employee>
<name>James</name>
<age>40</age>
</employee>
</employees>
</c:set>
<x:parse xml="${employees}" var="output"/>
<b>Name of the employee is </b>:
<x:out select="$output/employees/employee[1]/name" /><br>
<b>Age of the Employee is </b>:
<x:out select="$output/employees/employee[2]/age" />
</body>
</html>
O/P:

Visit the JSTL oracle doc for complete list of tags for each type.