JSP Standard Tag Library Tutorial

Job-ready Online Courses: Dive into Knowledge. Learn More!

Welcome to DataFlair JST Tutorial. This article discusses the JSTL, i.e., Standard Tag Library of JSP. It discusses the tags and functions present in it in detail. So let’s see JSP Standard tag Library in detail.

JSP Standard Tag Library

JSP Standard Tag Library Introduction

There are different tags in JSP. The Standard Tag Library of JSP, known as JSTL, is a collection of JSP tags. JSTL supports tasks that are common or structural in nature. They are iteration and conditionals, tags that stimulate internationalization tags, XML documents, and SQL tags. It integrates custom tags too.

To use them, we need to firstly install them. So let’s learn JSTL Installation.

How to Install JSTL Library

For apache tomcat-users, the following is the approach for JSTL Installation:

  • Download and unpack the compressed file of Apache Standard Taglib.
  • Copy the jar files into the lib folder to the destination: webapps\ROOT\WEB-INF\lib directory
    <taglib> directive include these tags.

JSTL tags and their classification

JSTL is a standard library tag used to implement functionalities in JSP programs. They classify under tag library groups based on their functionality. They are:

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

JSTL Core Tags

They are the most important and most used tags among the JSTL tags.
Syntax:

<%@ taglib prefix = "c" uri = "http://www.mysun.com/jsp/jstl/core" %>

Core tags in JSTL are as follows:

S.No.TagDescription
1<c:out>This tag is Similar to the <%=___> that are meant for expression statements.
2<c:set >This tag will set the scope for the result that we get from expression evaluation.
3<c:remove >This tag will remove the variable. It will remove the particular variable from scope if defined.
4<c:catch>This tag catches the Throwable exceptions in the section it is defined.
5<c:if>This tag will evaluate its body section if the condition specified is true. It is a conditional tag.
6<c:choose>This tag is implacable when there are conditional operations that are mutually exclusive. This tag is used with <when> and <otherwise> tags.
7<c:when>This tag is a subtag of <choose> that is discussed above. If the condition specified is true, then only this tag will include the body section.
8<c:otherwise >This is also a subtag of <choose> tag. If the condition becomes false, then this part runs that follows the section.
9<c:import>This tag will import the url.
10<c:forEach >This is an iteration tag that supports the subsetting of elements of different types.
11<c:forTokens>This tag will iterate itself over tokens.
12<c:param>This tag will add on a parameter to the url imported using <import>
13<c:redirect > This tag will redirect the user to new url
14<c:url> This tag will create a url with certain query parameters.

Formatting Tags in JSP

Formatting tags format the display of the date, time, text, especially for international websites. Through the following syntax, you can include the formatting library in JSP
Syntax: 

<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
S.No.TagDescription
1<fmt:formatNumber>This tag will format a numerical value to a precision.
2<fmt:parseNumber>This tag will parse the string representation of currency, percentage or number.
3<fmt:formatDate>This tag will format the date with styles and patterns.
4<fmt:parseDate>This tag will parse the string representation of date or time.
5<fmt:bundle>This method will load a resource bundle that is to be used by its body section.

Loads a resource bundle to be used by its tag body.

6<fmt:setLocale>This tag will store the locale information in the Locale variable.
7<fmt:setBundle>This method will store the loaded resource bundle in a named variable or bundle configuration variable.
8<fmt:timeZone>This tag will specify the time zone for a time formatting.
9<fmt:setTimeZone>This method will store the time zone in a time zone configuration variable.
10<fmt:message>This tag will display internationalized. message
11<fmt:requestEncoding>This method will set a request for character encoding.

JSTL SQL Tags

These JSTL tags will help in interacting with databases like MySQL, Oracle, etc. The syntax to include SQL library is as follows:
Syntax:

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

JSTL SQL tags are as follows:

S.No.TagDescription
1<sql:setDataSource>This tag will create a database that is simply for prototyping.
2<sql:query>This tag will execute the query on the database.
3<sql:update>This tag will execute the update query defined in its body.
4

5

<sql:param>

 <sql:dateParam>

This tag will set a parameter to the query statement with a specific value.

This tag will set a parameter for the sql statement and set it with java.util.Date value

6<sql:transaction >This tag executes all the actions or statements as one transaction. This is done over a shared connection.

JSTL XML Tags

These XML tags provide an easy way to manipulate XML documents. XML tag library has custom tags to interact with XML data. These interactions are transforming, controlling flow, parsing the XML data.
Syntax:

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

XML JSTL Tags are as follows:

S.No.TagDescription
1<x:out>This tag is alike <%=…%> but it is for XPath details.
2<x:parse>This tag will parse the XML data present in its section.
3<x:set >This tag will set the variable to XPath information.
4<x:if >This tag will evaluate the XPath expression. If this expression is true, it executes the body. It will be ignored if false.
5<x:forEach>This tag will loop for each statement in the body.
6<x:choose>This tag is implacable when there are conditional operations that are mutually exclusive. This tag is used with <when> and <otherwise> tags.
7<x:when >This tag is a subtag <choose>. If the expression is true, then it will include its body part.
8<x:otherwise>This tag runs when the condition is false. It is a subtag of <choose>
9<x:transform>This tag does transformation on an XML document.
10<x:param >This tag is used to set a parameter in the stylesheets. It is used with transform tags.

JSTL Functions

JSTL has a number of standard functions. They do string manipulation. Syntax to include Functions library of JSTL in JSP is as follows −
Syntax:

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

Various JSTL Functions are as follows:

S.No.FunctionDescription
1fn:contains()This function will test that a string contains a particular substring or not.
2fn:containsIgnoreCase()This function will test the same as above but in a case insensitive manner.
3fn:endsWith()This function will test that the string ends with the given suffix.
4fn:escapeXml()This function will escape the XML characters.
5fn:indexOf()This function gives the starting index of the substring in a string.
6fn:join()This function will form a string from all the elements of a given array.
7fn:length()This function will return the number of characters in a string or the number of items in a list.
8fn:replace()This function will replace a string with the other given string. The result will return as a replaced one.
9fn:split()This function will split a string. It will form an array of these substrings.
10fn:startsWith()This function will check that a string starts with a specific prefix or not.
11fn:substring()This function returns a substring.
12fn:substringAfter()This function will return a substring after a particular substring.
13fn:substringBefore()This function will return a substring before a particular substring.
14fn:toLowerCase()This function will convert the string to lowercase.
15fn:toUpperCase()This function will convert string to upper case.
16fn:trim()This function will trim white space from both ends of the string.

Features of JSTL

The tags simplify the task to create Web pages, especially for designers. They provide the following features.

  • JSTL core tags provide iterative tags. It supports the processing of the URL’s resources in a JSP page to locate URLs.
  • XML tag library helps in manipulating the XML documents.
  • They provide conditional and iteration processing on XML documents.
  • It supports internationalized web applications so that they are globally available for use. It makes the application localized so that it is reliable and effective. Internationalized libraries help it adapt to various locales.
  • JSTL provides functions from the functions tag library to perform actions like checking for substring passed as the parameter in the function is present in a string, the number of characters in a string, etc.
  • It provides the SQL tag library to retrieve and connect with the relational databases using these SQL tags.

Advantages of JSTL

  1. Standard Tag: The code is easy to understand for the developers as it provides the portable functionality for a JSP page.
  2. Clean code: JSTL makes the code clean, as the scriptlet code can be confusing.
  3. Easy for humans to read: As the code is in XML format, it is neat, clean, and easier to understand.
  4. Better off for computers to understand: Parsing of the JSTL becomes easier for the HTML as the JSTL code is written in the XML format.
  5. Javabeans Introspection Support: JSTL simplifies the use of bean objects, unlike JSP.

Example of JSTL

In this example, we have printed all the numbers from 1 to 5.

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>
<title> Numbers</title>
</head>

  <body>
  <c:forEach var="i" begin="1" end="5">
  	Value<c:out value="${i}" />

  	<br />
  </c:forEach>
  </body>
</html>

Explanation: An iteration tag for each. It takes a beginning and an ending value and iterates over.

Output: The output will be as follows:

Value1
Value2
Value3
Value4
Value5

Conclusion

This article consists of a detailed description of the standard tag library for JSP, known as JSTL. We also saw the tags in this library, as well as the functions present in it. Then we saw the way to install it. Finally, we also pondered upon some of its advantages and features.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

courses

DataFlair Team

The DataFlair Team is passionate about delivering top-notch tutorials and resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With expertise in the tech industry, we simplify complex topics to help learners excel. Stay updated with our latest insights.

Leave a Reply

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