Table of contents
1.
Introduction
2.
Advantages of JSP Standard Tag Library
3.
Installing JSP Standard Tag Library
4.
The JSP Standard Tag Library Tags
4.1.
Core Tags
4.2.
Formatting Tags
4.3.
SQL Tags
4.4.
XML Tags
4.5.
JSTL Functions
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Mar 27, 2024

JSP Standard Tag Library

Author Abhay Trivedi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JSP Standard Tag Library(JSTL) is a compilation of useful JSP tag libraries that deliver core functionality common to many JSP applications and web development projects. JSTL is the standard tag library of the JSP. JSTL supports everyday structural tasks such as iteration and conditionals, tags for working with XML(Extensible Markup Language) documents, SQL(Structured Query Language) tags, and internationalization tags. It furthermore provides a framework for integrating the existing custom tags with the JSP Standard Tag Library tags.

Advantages of JSP Standard Tag Library

Below are the advantages of the JSP Standard Tag Library(JSTL):

  • Fast Development: It provides many tags that simplify the JSP.
  • Standard Tags: It provides a rich layer of JSP pages' mobile functionality, making it easy for a developer to understand.
  • Code Reusability: We can use the JSP Standard Tag Library tags on various pages.
  • Code Neat and Clean: As scriptlets confuse developers, the usage of JSP Standard Tag Library makes the code neat and clean.
  • No need for a scriptlet tagIt avoids using a scriptlet tag.
  • Easy for humans to readIt is easy for the developers to understand since XML is the JSP Standard Tag Library base, similar to HTML(HyperText Markup Language).
  • Easy for computers to understand: It is easy for HTML generation to parse the JSTL code in the document using tools such as Dreamweaver and the front page.
  • Automatic JavabeansInterospection Support: JSTL Expression language handles JavaBean code quickly. We don't require to downcast the objects retrieved as scoped attributes.

Installing JSP Standard Tag Library

To begin working with JSP tags, first install the JSTL library. Follow these steps using the Apache Tomcat container:

Step 1: Download the binary distribution from Apache Standard Taglib and extract the compressed file.

Step 2: For using the Standard Taglib from its Jakarta Taglibs distribution, go to the distribution's 'lib' directory and copy the JAR files to your application's webapps\ROOT\WEB-INF\lib directory.

Include a <taglib> directive on top of each JSP to use any libraries.

Must Read Apache Server

The JSP Standard Tag Library Tags

We can categorize the JSP Standard Tag Library tags according to their functions. For creating a JSP page, use the following JSTL tag library groups:

  • Core Tags
  • Formatting Tags
  • SQL Tags
  • XML Tags
  • JSTL Functions

Core Tags

The JSTL Core tag delivers variable support, URL management, flow control, etc. The URL for the Core tag is http://java.sun.com/jsp/jstl/core, and the prefix of the core tag is c.

Tags

Description

<c:out>

It is like <%= ... >, but for expressions.

<c:set>

It sets the result of an expression evaluation in a 'scope.'

<c:remove>

If specified, it removes a scoped variable from a particular scope.

<c:catch>

It catches any Throwable that occurs in its body and optionally exposes it.

<c:if>

Simple conditional tag which evaluates its body if the supplied condition is actual.

<c:choose>

A simple conditional tag establishes a context for mutually exclusive dependent operations, marked by <when> and <otherwise>.

<c:when>

Subtag of <choose> includes its body if its condition evalutes to 'true'.

<c:otherwise >

The subtag of <choose> that follows the <when> tags only runs if all initial conditions evaluate as 'false.'

<c:import>

Helps in retrieving an absolute or relative URL and exposes its contents to either page, String in 'var,' or Reader in 'varReader.'

<c:forEach >

The primary iteration tag accepts many different collection types and supports subsetting and other functionality.

<c:forTokens>

Iterates over token with the supplied delimiters separation.

<c:param>

It adds a parameter to a containing 'import' tag's URL.

<c:redirect >

Redirects to a new URL.

<c:url>

Creates a URL with optional query parameters

 

Formatting Tags

We use the JSP Standard Tag Library formatting tags to format and display the text, date, time, and numbers for internationalized Websites. The Formatting tags sustain message formatting, number and date formatting, etc. The URL for the Formatting tags is http://java.sun.com/jsp/jstl/fmt, and the prefix is fmt.

Tags

Description

<fmt:formatNumber>

It helps to render numerical value with specific precision or format.

<fmt:parseNumber>

It helps in Parsing the string representation of a number, currency, or percentage.

<fmt:formatDate>

Format a date and time using the supplied styles and pattern.

<fmt:parseDate>

It helps in Parsing the string representation of a date and time.

<fmt:bundle>

It loads a resource bundle to be used by its tag body.

<fmt:setLocale>

It stores the given locale in the locale configuration variable.

<fmt:setBundle>

It loads a resource bundle and stores it in the named scoped or bundle configuration variable.

<fmt:timeZone>

It helps specify the time zone for any time formatting or parsing actions nested in its body.

<fmt:setTimeZone>

It stores the given time zone in the time zone configuration variable.

<fmt:message>

Displays an internationalized message.

<fmt:requestEncoding>

It sets the request as character encoding.

 

SQL Tags

The JSP Standard Tag Library SQL tag library provides tags for interacting with relational databases such as MySQL, Oracle, or Microsoft SQL Server. The JSTL SQL tags give SQL support. The URL for the JSTL SQL tags is http://java.sun.com/jsp/jstl/sql, and the prefix is sql.

Tags

Description

<sql:setDataSource>

It makes a simple DataSource suitable only for prototyping.

<sql:query>

It executes the SQL update defined in its body or through the SQL attribute.

<sql:update>

It executes the SQL update defined in its body or through the SQL attribute.

<sql:param>

It sets the parameter in an SQL statement to the given value.

<sql:dateParam>

It sets the parameter in an SQL statement to the specified java.util.Date value.

<sql:transaction >

It executes all statements as one transaction by providing nested database action elements with a shared Connection.

 

XML Tags

The JSP Standard Tag Library XML tag library has custom tags for interacting with the XML data, providing a JSP-centric mode of creating and manipulating the XML documents. XML tags include parsing, transforming the XML data, and the flow control based on the XPath expressions. URL for the JSTL XML tags is http://java.sun.com/jsp/jstl/xml, and the prefix is x.

Tags

Description

<x:out>

Like <%= ... >, but for XPath expressions.

<x:parse>

Use it to parse the XML data specified via an attribute or the tag body.

<x:set >

It sets a variable to the value of an XPath expression.

<x:if >

Evaluate a test XPath expression. If true, it processes its body. If false, it ignores the body.

<x:forEach>

It helps to loop over nodes in an XML document.

<x:choose>

A simple conditional tag establishes a context for mutually exclusive dependent operations, marked by <when> and <otherwise> tags.

<x:when >

Subtag of <choose> includes its body if its expression evalutes to 'true'.

<x:otherwise >

It subtag of <choose> that follows the <when> tags and runs only if all of the initial conditions evaluate to 'false.'

<x:transform >

Applies XSL transformation on a XML document.

<x:param >

Use the transform tag to set a parameter in the XSLT stylesheet.

 

JSTL Functions

JSP Standard Tag Library includes several standard functions and the most common string manipulation functions. The function tags provide support for string manipulation and string length. The URL for the functions tags is http://java.sun.com/jsp/jstl/functions, and the prefix is fn.

Functions 

Description

fn:contains()

Tests if an input string contains the given substring.

fn:containsIgnoreCase()

Tests if an input string contains the given substring in a case-insensitive way.

fn:endsWith()

It tests if an input string ends with the specified suffix.

fn:escapeXml()

It escapes characters that can be interpreted as XML markup.

fn:indexOf()

It returns the index within a string of the first occurrence of a specified substring.

fn:join()

It helps to Join all elements of an array into a string.

fn:length()

It returns the number of items in a collection or the number of characters in a string.

fn:replace()

It returns a string that replaces all occurrences with a given string in an input string.

fn:split()

It helps to split a string into an array of substrings.

fn:startsWith()

It tests if an input string starts with the given prefix.

fn:substring()

It returns a subset of a string.

fn:substringAfter()

It returns a subset of a string following a specific substring.

fn:substringBefore()

It gives a subset of a string before a given substring.

fn:toLowerCase()

It converts all of the characters of a string to lower case.

fn:toUpperCase()

It converts all of the characters of a string to upper case.

fn:trim()

It removes trailing white spaces from both ends of a string.

You can also read about the JSP Architecture and Lifecycle.

Frequently Asked Questions

1. What is JSP?

JSP or Java Server Pages is a Web-based server-side technology used for creating web applications & dynamic web content. We use JSP tags to insert JAVA code into HTML pages. JSP is an advanced version of Servlet Technology that helps create dynamic and platform-independent web pages by inserting Java code in HTML/ XML pages or both. The JSP container first converts JSP into a servlet and processes the client’s request.

2. What is the difference between JSP and HTML?

JSP is a server-side technology that we use to create dynamic web applications. HyperText markup language (HTML) is used to create the structure of web pages. JSP allows placing the custom tag or third-party tag. It does not permit the placement of the custom tag or third-party tag.

3. What is JSTL or JSP Standard Tag?

JSTL stands for Java server pages standard tag library. JSP Standard Tag Library is a pack of useful JSP tag libraries that deliver core functionality common to many JSP applications and web development projects. JSTL is the standard tag library of the JSP.

4. How to include JSTL Core, JSTL Formatting, JSTL SQL, JSTL XML and JSTL functions library in the JSP?

You can include the above tags in the following way:

Core Tags:

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

Formatting Tags:

<%@ taglib prefix = 'fmt' uri = 'http://java.sun.com/jsp/jstl/fmt' %>

SQL Tags:

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

XML tags:

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

JSTL Functions:

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

 

5. What is the difference between JSTL and JSP?

JSP Standard Tag Library is a standard tag library provided by Oracle to carry out everyday tasks like looping, formatting, etc. In contrast, JSP lets you define your tags, i.e., you can write the code that implements those tags' logic in Java.

Key Takeaways

This article teaches about JSP Standard Tag Library and how we use them. We saw why JSP Standard Tag Library could be beneficial for a developer to learn.

Click here to read about Java Interview Questions. Click here to see other related blogs on JAVA. Check out our Web development course and blogs on Backend Web Technologies.

If you are preparing for your DSA interviews then, Coding Ninjas Studio is a one-stop destination. This platform will help you acquire effective coding techniques and overview student interview experience in various product-based companies.

Happy Learning!

Live masterclass