JSP Standard Tag Library (JSTL) Tutorial

The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates the core functionality common to many JSP applications.

JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization tags, and SQL tags. It also provides a framework for integrating the existing custom tags with the JSTL tags.

JSTL Dependencies or Jars

In order to work with JSTL tags, we will need to add below two jars:
javax.servlet.jsp.jstl-api-1.2.1.jar - This contains the JSTL API interfaces and support classes. However, a large number of interfaces do not have implementation classes.
javax.servlet.jsp.jstl-1.2.1.jar - This contains an implementation of the JSTL API. This code implements all of the interfaces from the API above.
Here are the links to download JSTL jars:
Copy the two JAR files
  • javax.servlet.jsp.jstl-1.2.1.jar
  • javax.servlet.jsp.jstl-api-1.2.1.jar
In Eclipse, paste the two JAR files to your project directory: WebContent/WEB-INF/lib

Classification of The JSTL Tags

The JSTL tags can be classified, according to their functions, into the following JSTL tag library groups that can be used when creating a JSP page −
diagram here
  1. Core Tags
  2. Formatting tags
  3. SQL tags
  4. XML tags
  5. JSTL Functions

1. JSTL Core Tags

JSTL Core tags provide support for iteration, conditional logic, catch exception, url, forward or redirect response etc. To use JSTL core tags, we should include it in the JSP page like below.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
You can learn all JSTL core tags with examples at JSTL Core Tags with Examples.

2. JSTL Functions Tags

JSTL tags provide a number of functions that we can use to perform a common operation, most of them are for String manipulation such as String Concatenation, Split String etc. Syntax to include JSTL functions in JSP page is:
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
You can learn all JSTL function tags with examples at JSTL Functions with Examples.

3. JSTL Formatting Tags

JSTL Formatting tags are provided for formatting of Numbers, Dates and i18n support through locales and resource bundles. We can include these JSTL tags in JSP with below syntax:
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
You can learn all JSTL formatting tags with examples at JSTL Formatting tags.

4. JSTL SQL Tags

JSTL SQL Tags provide support for interaction with relational databases such as Oracle, MySql etc. Using JSTL SQL tags we can run database queries, we include these JSTL tags in JSP with below syntax:
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
You can learn all JSTL SQL tags with examples at JSTL SQL tags.

5. JSTL XML Tags

JSTL XML tags are used to work with XML documents such as parsing XML, transforming XML data and XPath expressions evaluation. Syntax to include JSTL XML tags in JSP page is:
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
You can learn all JSTL XML tags with examples at JSTL XML tags.

Benefits of JSP Custom Tags

  • Minimize the amount of scriptlet code in a JSP
  • Avoids dumping thousands of lines of code in a JSP
  • JSP page is simple … a main focus of JSP is only the presentation
  • Tag is reusable

In the next article, we will learn JSTL core tags with examples at JSTL Core Tags with Examples

Comments