JSP Standard Tag Library

JSP Standard Tag Library (JSTL) with Examples

In this article, I am going to discuss JSP Standard Tag Library (JSTL) with Examples. Please read our previous article, where we discuss JSP Date Handling i.e. How to Handle Dates in JSP Applications. At the end of this article, you will understand the following pointers.

  1. What is JSP Standard Tag Library (JSTL)?
  2. Features of JSTL (JSP Standard Tag Library)
  3. Why we need JSTL (JSP Standard Tag Library)?
  4. Advantages of JSP Standard Tag Library
  5. How to Download and Install JSTL?
  6. Classification of JSTL tags
  7. Examples of JSTL tags
What is JSP Standard Tag Library (JSTL)?

JSTL is a specification given by Sun Microsystems for developing a tag library. These tag libraries are developed by server providers. The JSTL tags are classified consistent with their functions into different tag JSTL tag library groups which will be used when creating a JSP page. JSTL used to develop the JSP files without writing any java code. JSTL tags are released as a part of jstl.jar files.

JSTL encapsulates core functionality common to several JSP applications. Instead of mixing tags from numerous vendors in your JSP applications, JSTL allows you to use one, a standard set of tags. This standardization allows you to deploy your applications on any JSP container supporting JSTL and makes it more likely that the implementation of the tags is optimized.

JSTL has tags such as iterators and conditionals for handling flow control, tags for manipulating XML documents, internationalization tags, tags for accessing databases using SQL, and commonly used functions.

Features of JSTL (JSP Standard Tag Library)
  1. JSTL provides an easy way to maintain JSP pages. JSTL tags simplify the task of the designers to create Web pages.
  2. JSTL provides supports for conditional processing and URL-related actions to process URL resources on a JSP page.
  3. JSTL encapsulates web applications to be accessed globally by providing the internationalization tag library.
  4. JSTL enables interaction with relational databases by using various SQL commands Web applications require databases to information required for the application.
  5. JSTL provides a series of functions to perform manipulations.
Why we need JSTL (JSP Standard Tag Library)?

In JSP technology, by using scripting elements we are ready to provide Java code inside the JSP pages. To preserver JSP principles we have to eliminate scripting elements, for this, we have to use JSP actions. In the case of JSP Actions, we will use standard actions as an alternative to scripting elements, but which are limited in number and having bounded functionality so that standard actions are not specified in the required application format. Still, it is required to provide Java code completely from JSP pages we have to use custom actions.

In the case of custom actions, to implement simple Java syntaxes like if condition, for loop and so on we have to provide a lot of Java code internally. To overcome the above problem JSP technology has provided a separate tag library with simple Java syntax implementation and regularly used operations.

JSTL is an abstraction provided by Sun Microsystems, but where implementations are provided by all the server vendors. With the above convention, Apache Tomcat has provided JSTL implementation in the form of the jar files as standard.jar and jstl.jar. Apache Tomcat has provided the above two jar files in the following location.

C:\Tomcat 9.0\webapps\examples\WB_INF\lib

If we want to get JSTL support in our JSP pages then we have to keep the above two jar files in our web application lib folder.

Advantages of JSP Standard Tag Library
  1. JSTL has many tags which simplify the JSP and provides fast development.
  2. It is allowed to use JSTL tags on various pages which provides code reusability.
  3. It is not required to use scriptlet tags in JSTL.
How to Download and Install JSTL

Step-1: Download jar files, jstl.jar, and standard.jar file from the below link:
http://www.java2s.com/Code/Jar/j/Downloadjstljar.htm
Step-2: Paste both the files in your WEB-INF/lib folder.
Step-3: Then add jar files into classpath also.
Step-4: At last, you can use JSTL in your projects.

Classification of JSTL tags

JSTL includes a good sort of tags that fit into discrete functional areas. To reflect this, as well as to give each area its own namespace, JSTL is exposed as multiple tag libraries. JSTL has provided the following tag library in the form of the following five types of tags:

  1. Core Tags
  2. XML Tags
  3. Formatting Tags
  4. SQL Tags
  5. Functions Tags

To get a particular tag library support into the present JSP page we have to use the following standard URLs to the attributes in the respective taglib directives.

http://java.sun.com/jstl/core

http://java.sun.com/jstl/xml

http://java.sun.com/jstl/fmt

http://java.sun.com/jstl/sql

http://java.sun.com/jstl/functions

Core Tags in JSP

Core JSTL tags are wont to perform some core operations like printing, assigning if test, iteration, etc. in any JSP page without writing java code

Syntax: <% taglib prefix=”c” uri=http://java.sun.com/jsp/jstl/core %>

  1. <c:catch>: Used to catch any throwable that happens in its body and optionally exposes it.
  2. <c:choose>: it’s an easy conditional tag that establishes a context for mutually exclusive conditional operations, marked by and <when> and <otherwise>.
  3. <c:if>: it’s an easy conditional tag that evaluates its body if the supplied condition is true and optionally exposes a Boolean scripting variable representing the evaluation of this condition.
  4. <c:import>: it’s wont to retrieve an absolute or relative URL and exposes its contents to either the page, a String, or a Reader
  5. <c:forEach>: it’s the essential iteration tag that accepts many various collection types and supporting subsetting and other functionality.
  6. <c:forTokens>: it’s wont to iterate over tokens, separated by the supplied delimiters.
  7. <c:out>: it’s wont to display a specific value on the client browser.
  8. <c:otherwise>: it’s a subtag of <choose> tag that follows tags and runs as long as all the prior conditions evaluated to false.
  9. <c:param>: Used to add a parameter to a containing “import” tag’s URL.
  10. <c:redirect>: Used to redirect to a replacement URL
  11. <c:remove>: it’s used to remove a scoped variable.
  12. <c:set>: it’s used to set the results of an expression evaluation during a scope.
  13. <c:url>: it’s used to create a URL with optional query parameters.
  14. <c:when>: it’s a subtag of <choose> that has its body if its condition evaluates to “true”.
Formatting Tags in JSP

These formatting JSTL tags are used to change the format date or any number. To use formatting JSTL tags we’ve to write down the following taglib directives inside the JSP.

Syntax: <%@taglib uri=”http://java.sun.co/jsp/jstl/fmt” prefix=”fmt”%>

  1. <fmt:requestEncoding>: It is used to set the request character encoding.
  2. <fmt:setLocale>: It is used to store the given locale within the locale configuration variable.
  3. <fmt:timeZone>: It is used to specify the zone for any time formatting or parsing actions nested in its body.
  4. <fmt:setTimeZone>: It is used to store the given zone within the zone configuration variable.
  5. <fmt:bundle>: It is used to load a resource bundle to be employed by its tag body.
  6. <fmt:setBundle>: It is used to load resource bundle stores within the named scoped variable or the bundle configuration variable.
  7. <fmt:message>: It is used to map key to localized message and performs parametric replacement.
  8. <fmt:param>: Supplies an argument for parametric replacement to a containing <message> tag.
  9. <fmt:formatNumber>: Formats a numeric value as a number, currency, or percentage.
  10. <fmt:parseNumber>: It is used to parse the string representation of a number, currency, or percentage.
  11. <fmt:formatDate>: It is used to formats a date and/or time using the supplied styles and pattern.
  12. <fmt:parseDate>: It is used to parse the string representation of a date and/or time.
SQL Tags in JSP

The main purpose of the SQL tag library is to interact with the database in order to perform the basic database operations. It is used to communicate with any database in any JSP without writing java code. To use JSTL tags we have to write the following taglib directives inside the JSP file.

Syntax: <% taglib prefix=”sql” uri=http://java.sun.com/jsp/jstl/sql %>

  1. <sql:transaction>: It is used to provide the nested database action elements with a shared Connection, set up to execute all statements as one transaction.
  2. <sql:query>: It is used to execute the SQL query defined in its body or through the SQL attributes.
  3. <sql:update>: It is used to execute the SQL update defined in its body or through the SQL attributes.
  4. <sql:param>: It is used to set the parameter in an SQL statement to the specified value.
  5. <sql:dateParam>: It is used to set a parameter in an SQL statement to the specified java.util.Date value.
  6. <sql:setDataSource>: It is used to create a simple DataSource suitable only for prototyping.
XML Tags in JSP

The XML tags are used to read, process, format the XML files. To use JSTL XML tags we have to write the following taglib directives in the JSP file.

Syntax: <% taglib prefix=”x” uri=http://java.sun.com/jsp/jstl/xml %>

  1. <xml:choose>: It is a simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>.
  2. <xml:if>: It is a XML conditional tag, which evaluates its body if the supplied XPath expression evaluates to ‘true’ as a Boolean.
  3. <xml:forEach>: It is used for XML iteration tag.
  4. <xml:otherwise>: It is a subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to “false”.
  5. <xml:param>: It is used to add a parameter to a containing “transform” tag’s Transformer.
  6. <xml:parse>: It is used to parse XML content from “source” attribute or “body”.
  7. <xml:set>: It is use to save the result of an XPath expression evaluation in a “scope”.
  8. <xml:transform>: It is use to conduct a transformation given a source XML document and an XSTL stylesheet.
  9. <xml:when>: It is a subtag of <choose> that includes its body if its expression evaluates to “true”.
JSTL Functions in JSP

The main purpose of the functions tag library is to perform all the String operations which are defined in the String class.

Syntax: <% taglib prefix=”fn” uri=http://java.sun.com/jsp/jstl/functions %>

Example:

<% taglib uri=http://java.sun.com/jstl/core prefix=”c”%>
<% taglib uri=http://java.sun.com/jstl//jspfunctions” prefix=”fn”%>
<c:set var=”a” value=”Hello World”/>
${fn.length(a)}
${fn.concat(a, “Hyderabad”)}
${fn.toLowerCase(a)}
${fn.toUpperCase(a)}
${fn.contains(a,”World”)}
${fn.startsWith(a, “Hello”)}
${fn.endsWith(a,”World”)}
${fn.substring(a,4,20)}

Example of Core Tags

<c:out>

In this example, the prefix “c” is added which can be used as a prefix for all core tags. The out will print the value in the expression tag.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
 <c:out value="${'Hello world'}" />
</body>
</html>
<c:catch>

In this example, the prefix “c” is added which can be used as a prefix for all core tags. Here core tag catch is used to catch the exception and print the exception.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Core Tag JSP2</title>
</head>
<body>
 <c:catch var="Exception">
  <%
   int num = 10 / 0;
  %>
 </c:catch>
 The Exception is : ${Exception}
</body>
</html>
<c:import>

In this example, we are using an import tag so the content from different FTP servers and websites can be accessed.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
 <c:import var="data" url="http://www.google.com" />
 <c:out value="${data}" />
</body>
</html>
<c:forEach>

In this example, we are using the “ForEach” loop where the variable name “count” has begun count as 5 and end count as 10.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Core Tag JSP4</title>
</head>
<body>
 <c:forEach var="count" begin="5" end="10">
  <c:out value="${count}" />
 </c:forEach>
</body>
</html>
<c:if>

In this example, we are setting the variable named count to 100 and using “if condition” where we are checking whether the count is equal to 100.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Core Tag JSP5</title>
</head>
<body>
 <c:set var="count" value="100" />
 <c:if test="${count == 100}">
  <c:out value="The count is 100" />
 </c:if>
</body>
</html>
<c:redirect>

In this example, we are using the redirect tag where we specifying urlname and when we click on that page it redirects to the site.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Tag Example</title>
</head>
<body>
 <c:redirect url="/" />
</body>
</html>

In the next article, I am going to discuss How to interact with a Database in JSP with Examples. Here, in this article, I try to explain JSP Standard Tag Library (JSTL) with Examples. I hope you enjoy this JSP Standard Tag Library (JSTL) with Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *