JSTL tags are grouped into five major categories:
1.Core Tags
2.Formatting Tags
3.SQL Tags
4.XML Tags
5.Function Tags
JSTL Core Tags
Core tags in JSTL are used for iteration, conditional logic, url management, handling exceptions,
redirect, etc.
●prefix: c
●uri: http://java.sun.com/jsp/jstl/core
The following tags are included in the JSTL Core tag library:
●catch
●choose
●if
●import
●forEach
●forTokens
●out
●otherwise
●param
●redirect
●remove
●set
●url
●when
c:set
It is used to set a value to a variable that can be used within the specified scope in a JSP.
●Tag handler class: org.apache.taglibs.standard.tag.rt.core.SetTag
●Body content: jsp
●Attributes:
ovar: It is used to provide the name of the variable.
ovalue: It is used to provide the value for the variable. It can contain any
expression.
oscope: It is used to set the scope of the variable.
otarget: It is used to provide the target object whose property is to be set.
oproperty: It is used to specify the name of the property to be set for the target
object.
My Name is John
c:remove
removes the specified variable.
Tag handler class: org.apache.taglibs.standard.tag.common.core.RemoveTag
Body-content: empty
Attributes:
var: It is used to specify the variable to be removed.
Example:
remove.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@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>Remove Tag</title>
</head>
<body>
<h1>
<c:set var="name" value="John"/>
My Name is
<c:out value="${name}"/>
<br>
<c:remove var="name"/>
My Name is
<c:out value="${name}"/>
</h1>
</body>
</html>
Output:
My Name is John
My Name is
In the above example, the variable name is removed, therefore, the second <c:out> doesn't
display the name.
c:out
It is used to display the content on the web page similar to the expression tag (<%= %).
Tag handler class: org.apache.taglibs.standard.tag.rt.core.OutTag.
Body content: jsp
Attributes:
value: It is used to provide the value to be displayed.
default: It is used to provide the default value, in case, the value provided is null.
Example:
We are learning JSTL Core Tags
c:forEach
used to iterate over a collection or an array.
Tag handler class: org.apache.taglibs.standard.tag.rt.core.ForEachTag
Body content: jsp
Attributes:
items: It is used to provide a collection of objects to iterate over.
begin: It is used to specify the beginning of the iteration.
end: It is used to specify the end of the iteration.
step: It is used to provide the steps to be taken between two consecutive iterations.
var: It is used to provide a variable for a current item of the iteration.
Example:
1 3 5 7 9
10 12 15 14 9
c:import
It is used to include the contents from relative or absolute urls within or outside the server.
Tag handler class: org.apache.taglibs.standard.tag.rt.core.ImportTag
Body content: jsp
Attributes:
url: It is used to specify the url of the resource to be imported.
var: It is used to provide the var in which contents of the imported resource are to be stored.
scope: It is used to specify the scope.
Example:
import.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@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>Import Tag</title>
</head>
<body>
<c:import var="content" url="http://www.google.com"/>
<c:out value="${content}"/>
</body>
</html>
c:param
It is used with <c:import> or <c:redirect> to send parameters in the url to the imported or
redirected pages respectively.
Tag handler class: org.apache.taglibs.standard.tag.rt.core.ParamTag
Body content: jsp
Attributes:
name: It is used to provide the name of the parameter.
value: It is used to provide value for the parameter.
c:redirect
It is used to redirect a page to another url.
Tag handler class: org.apache.taglibs.standard.tag.rt.core.RedirectTag
Body content: jsp
Attributes:
url: It is used to provide the url of the resource to be redirected to.
Example:
Welcome John!
c:catch
It is used to catch any exception of type Throwable that occurs in the body.
Tag handler class: org.apache.taglibs.standard.tag.common.core.CatchTag
Body-content: jsp
Attributes:
var: used to store the exception thrown by body-content.
Example:
java.lang.ArithmeticException:/by zero
c:if
It is used to include the conditional statement in the java server pages. Body content is evaluated
only
when the condition evaluates to true.
Tag handler class: org.apache.taglibs.standard.tag.rt.core.IfTag
Body-content: jsp
Attributes:
test: used to provide the testing condition.
Example:
Qualified!!
c:forTokens
It is used to iterate over given tokens separated by the specified delimiters.
Tag handler class: org.apache.taglibs.standard.tag.rt.core.ForTokensTag
Body-content: jsp
Attributes:
items: It is used to provide the collection of objects to iterate over.
begin: It is used to specify the beginning of the iterations.
end: It is used to specify the end of the iterations.
step: It is used to provide the steps to be taken between to consecutive iterations
var: used to provide a variable for a current item of the iteration.
delims: used to provide a set of delimiters.
Example:
10 20 30 40 50
c:choose, c:when, c:otherwise
It is used to include a conditional statement on the page. It allows multiple conditions similar to
if-else ladder or switch case statements.
<c:choose>: It marks the beginning of conditional and encloses <c:when> and <c:otherwise>.
Tag handler class: org.apache.taglibs.standard.tag.common.core.ChooseTag
Body content: jsp
Attributes: no attribute
<c:when>: It is used to provide the condition and encloses the body to be executed if the
condition evaluates to true.
Tag handler class: org.apache.taglibs.standard.tag.rt.core.WhenTag
Body content: jsp
Attributes:
test: It is used to provide the condition.
<c:otherwise>: It encloses the content to be executed if all the above conditions evaluate as false.
Tag handler class: org.apache.taglibs.standard.tag.common.core.OtherwiseTag
Body content: jsp
Attributes: no attribute
A Grade
c:url
It is used to create a formatted url along with parameters using a nested <c:param> tag.
Tag handler class: org.apache.taglibs.standard.tag.rt.core.UrlTag
Body content: jsp
Attributes:
var: It is used to provide the variable that holds the url.
scope: It is used to set the scope.
value: It is used to provide the url to be formatted.
Example:
JSTL Function Tags
JSTL Function tags provide various string formatting functions.
prefix: fn
uri: http://java.sun.com/jsp/jstl/functions
Tag handler class: org.apache.taglibs.standard.functions.Functions
Include the <taglib> tag in the JSP as follows
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
The following tags are included in the JSTL function tag library :
It checks whether a string is contained inside the given string.
Syntax: boolean contains(String s1, String s2)
Return value: boolean (true if the string is present within the given string, false otherwise)
Parameters:
s1: String to be processed.
s2: String that must be contained in s1.
Example:
fn:containsIgnoreCase()
It checks whether a string is contained inside the given string in a case-insensitive way.
Syntax: boolean containsIgnore(String s1, String s2)
Return value: boolean (true if the string is present within the given string, false otherwise)
Parameters:
s1: String to be processed.
s2: Substring that is to be searched.
Example:
true
fn:endsWith()
It checks whether a string ends with the given suffix.
Syntax: boolean endsWith(String s1, String s2)
Return value: boolean (true if the string ends with the given suffix, false otherwise)
Parameters:
s1 : string to be processed.
s2 : suffix
Example:
true
fn:escapeXml()
It is used to escape the characters that can be interpreted as XML.
Syntax: String escapeXml(String s)
Return value: String (String obtained after escaping XML characters)
Parameters:
s: String to be processed.
Example:
Without escaleXml : HelloEveryOne
With escapeXml : <abc>HelloEveryOne</abc>
fn:indexOf()
It finds the index of the first occurrence of a given substring.
Syntax: int indexOf(String s1, String s2)
Return value: int(index of first occurrence of substring if found, - 1 otherwise)
Parameters:
s1: string to be processed.
s2: substring whose index is to be determined.
Example:
5
fn:split()
It is used to split a string into an array of substrings based on the given separator.
Syntax: String[] split(String s1, String s2)
Return value: String[] (array of substrings obtained after splitting )
Parameters:
s1: spring that is to be split.
s2: separator
fn:join()
It is used to join an array of String with a given separator.
Syntax: String join (String s1[], String s2)
Return value: String (String formed after joining the elements of the array with a given
separator)
Parameters:
s1: array of strings to be joined.
s2: separator
Example:
Original string : Hello every one
String after join : Hello-every-one
fn:length()
It is used to determine the length of a string or the size of a collection.
Syntax: int length(Object o)
Return value: int (length of string or size of collection object)
Parameters:
String or collection whose length is to be determined.
Example:
hxllo
fn:startsWith()
It checks whether a string starts with the given prefix.
Syntax: boolean startsWith(String s1, String s2)
Return value: boolean (true if string starts with the given prefix, false otherwise)
Parameters:
s1: string to be processed.
JSTL stands for JavaServer Pages Standard Tag Library. It is introduced in the JSP 2.0 version to
simplify the JSP.
By using JSTL, JSP becomes a 100% tag-based application. JSTL can provide the group of tags
that are used to perform the tasks in the JSP pages without using the Java code. JSTL tags are
divided into the Five types Formatting tags, Core tags, SQL tags, Function tags, and XML tags.
Now we are discussing the Formatting tags.
The JSTL formatting tags are used for formatting data. It allows the developer to manipulate the
data and display the data in different formats like numbers, currencies, and dates on the JSP
pages.
Prerequisites:
The following are the prerequisites which are used in the JSTL formatting tags:
The Prerequisites for using the JSTL formatting tags are:
Java Environment
Servlet Container
JSTL Library
Taglib Declaration
The Syntax of the 'taglib' directive is shown below:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
In the above syntax,
uri: It specified the URI of the tag library.
prefix: It defines the prefix used to the reference tags from the library in the JSP page.
Note: By make sure that the above prerequisites will be met, you can easily use the JSTL
formatting tags with the JSP pages for numbers, format dates, currencies and other data types
according to the requirements of your application.
It is used to create a simple data source by taking url, username, password, and driver class name
as parameters. It is used for further database operations.
Tag handler class: org.apache.taglibs.standard.tag.rt.sql.SetDataSourceTag
Body content: empty
Attributes:
var: It is used to provide the variable which holds the data source generated.
scope: It is used to define the scope.
driver: It is used to provide the name of the driver class using which connection is to be
established.
url: It is used to provide the url associated with the database.
user: It is used to provide the username on whose behalf the connection is to be made.
password: It is used to provide the password for the specified user account.
Example:
It is used to execute the query given in the body or the attribute using the specified data source
and store the results in the variable provided in its attribute. It is used to execute the SELECT
statement.
Tag handler class: org.apache.taglibs.standard.tag.rt.sql.QueryTag
Body content: jsp
Attributes:
var: It is used to provide the variable for storing the results of the query execution.
scope: It is used to set the scope for the variable.
dataSource: It is used to provide the data source that is to be used to execute the query.
sql: It is used to provide the sql query.
startRow: It is used to specify the start index of the row to be included in the result. If not
specified, the result contains rows starting from 0.
maxRows: It is used to specify the maximum number of rows to be included in the result. If not
specified, no limit is imposed.
Example:
It is used to execute the update query given in the body or the attribute using the specified data
source.
Tag handler class: org.apache.taglibs.standard.tag.rt.sql.UpdateTag
Body content: jsp
Attributes:
var: It is used to provide the variable for storing the results of the update query execution.
scope: It is used to set the scope for the variable.
dataSource: It is used to provide the data source that is to be used to execute the query.
sql: It is used to provide the sql query.
Example:
It is nested inside the <sql:query> or <sql:update> tags to provide the query parameters.
Tag handler class: org.apache.taglibs.standard.tag.rt.sql.ParamTag
Body content: jsp
Attributes:
value: It is used to provide the value of the parameter.
Example:
It is used to provide the date, time, or timestamp parameters in the query.
Tag handler class: org.apache.taglibs.standard.tag.rt.sql.DateParamTag
Body content: empty
Attributes:
value: It is used to provide the value for the parameter.
type: It is used to specify the type of value(date, time, or timestamp).
Example:
It is used to execute multiple queries as one transaction. All the queries are nested inside the
transaction.
Tag handler class: org.apache.taglibs.standard.tag.rt.sql.TransactionTag
Body content: jsp
Attributes:
dataSource: It is used to provide the data source using which queries are to be executed.
Example:
When you send the XML data via HTTP, it makes sense to use JSP to handle incoming and
outgoing XML documents; for example, RSS documents. As an XML document is merely a
bunch of text, creating one through a JSP is much easier than creating an HTML document.
Sending XML from a JSP
You can send the XML content using JSPs the same way you send HTML. The only difference is
that you must set the content type of your page to text/xml. To set the content type, use the
<%@page%> tag, like this −
<%@ page contentType = "text/xml" %>
Following example will show how to send XML content to the browser −
<%@ page contentType = "text/xml" %>
<books>
<book>
<name>Padam History</name>
<author>ZARA</author>
<price>100</price>
</book>
</books>
Access the above XML using different browsers to see the document tree presentation of the
above XML.
Processing XML in JSP
Before you proceed with XML processing using JSP, you will need to copy the following two
XML and XPath related libraries into your <Tomcat Installation Directory>\lib −
XercesImpl.jar − Download it from https://www.apache.org/dist/xerces/j/
xalan.jar − Download it from https://xml.apache.org/xalan-j/index.html
Let us put the following content in books.xml file −
<book>
<name>Great Mistry</name>
<author>NUHA</author>
<price>2000</price>
</book>
</books>
Try the following main.jsp, keeping in the same directory −
<h3>Books Info:</h3>
<c:import var = "bookInfo" url="http://localhost:8080/books.xml"/>
<x:parse xml = "${bookInfo}" var = "output"/>
<b>The title of the first book is</b>:
<x:out select = "$output/books/book[1]/name" />
<br>
<b>The price of the second book</b>:
<x:out select = "$output/books/book[2]/price" />
</body>
</html>
Access the above JSP using http://localhost:8080/main.jsp, the following result will be displayed
−
Books Info:
The title of the first book is:Padam History
The price of the second book: 2000
Formatting XML with JSP
Consider the following XSLT stylesheet style.xsl −
<?xml version = "1.0"?>
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
version = "1.0">
<c:import url = "http://localhost:8080/style.xsl" var = "xslt"/>
<x:transform xml = "${xmltext}" xslt = "${xslt}"/>
</body>
</html>
The following result will be displayed −