Jstl tutorial – JSP Standard Tag Library Tutorial (JSTL), JSTL Tags | Definition, Advantages, Classification of Tags in JSTL with Examples

Jstl tutorial: In this tutorial, you will learn how to start using this JSP standard library and its numerous methods and tags. Also, you can have some knowledge on its advantages and how to install JST Library along with the JSTL Core Tags with Examples.

What is JSTL (JSP Standard Tag Library)

JSTL stands for JSP Standard Tag Library. It can be used only on JSP pages. JSTL tag can be used for conditional, iteration tags for manipulating the XML document, internationalization tags, and SQL tags. It is used to remove scriptlet code from a JSP page.

How to download and install JSTL?

The following steps will help you understand how to install the JSTL library within seconds:

  • Firstly, Download JSTL.jar and Standarad.jar file (or you will obtain these from your native Apache tomcat installation too!).
  • Later place both the files into your ‘WEB-INF/lib’ folder.
  • Once you are done with the above step, add them to the classpath.
  • At last, you can use JSTL in your project.

Advantages of JSTL

  1. Fast Development: we can provide many tags which simplify the JSP.
  2. Automatic JavabeansInterospection Support
  3. Code Reusability: Use the JSTL tags on the previous page.
  4. Easier for computers to understand
  5. No need to use a scriptlet tag: It avoids the scriptlet tag.
  6. Easier for humans to read

Do Read:

Classification of Tags in JSTL

JSTL is a standard library tag utilized to perform functionalities in JSP programs. They range under tag library groups depends on their functionality. They are:

  1. Core tags
  2. Formatting tags
  3. Function tags
  4. XML tags
  5. SQL tags

1. JSTL Core Tags

JSTL Core tags include those which are related to variables, and flow control, as well as it is a generic way to access URL-based resources.

Syntax:

JSP Standard Tag Library Tutorial (JSTL), JSTL Tags 1

The following are the JSTL Core Tags:

i) c:out:

it is used to display the result of an expression. It is just like an expression tag.

JSP Standard Tag Library Tutorial (JSTL), JSTL Tags 2

ii) c:set:

It is used to set the value of a variable in a specified scope.

JSP Standard Tag Library Tutorial (JSTL), JSTL Tags 3

iii) c:remove:

This JSTL core tag is used to remove an attribute from a specified scope or from all scope.

JSP Standard Tag Library Tutorial (JSTL), JSTL Tags 4

iv) c:catch:

It is used for exception handling in JSP.

JSP Standard Tag Library Tutorial (JSTL), JSTL Tags 5

v) c:forEach:

It is used for iteration. It is just like for loop in java.

JSP Standard Tag Library Tutorial (JSTL), JSTL Tags 6

vi) c:param:

It is used to add a parameter and its values to the output of these tags.

vii) c:redirect:

It is used for redirecting the current page to another URL.

JSP Standard Tag Library Tutorial (JSTL), JSTL Tags 7

viii) c:url:

To rewrite the URL returned from a JSP page, we can use URL tag.

ix) c:if:

It is used to testing the condition. It is just like if statement in java.

JSP Standard Tag Library Tutorial (JSTL), JSTL Tags 8

x) c:import:

It is used for importing the content from another file/page to the current JSP page.

JSP Standard Tag Library Tutorial (JSTL), JSTL Tags 9

2. JSTL Formatting Tags

JSTL formatting tags are used to formatting the text, date, number, etc. The JSTL Formatting tags are also used to formatting the date, and time of internationalization websites.

Syntax:

JSP Standard Tag Library Tutorial (JSTL), JSTL Tags 10

The following are the JSTL Formatting Tags:

i) fmt:timeZone: It specifies the time zone for any time formatting or parsing action nested in its body.

ii) fmt:setTimeZone: Stores the given time zone in the time zone configuration variable.

iii) fmt:formatDate: Formats a date and/or time using the supplied styles and pattern.

iv) fmt:message: used to display an internationalized message.

v) fmt:formatNumber: It is used to render numerical value with a specific precision or format.

vi) fmt:parseNumber: Parses the string representation of number, currency, or percentage.

vii) fmt:parseDate: Parses the string representation of the date and/or time.

viii) fmt:bundle: Load a resource bundle to be used by its tag body.

ix) fmt:setBundle: Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable.

3. JSTL Function Tags

This tag provides a number of predefined functions that can be used to perform a common operation such as String concatenation, String split, etc. It is used for String manipulation.

Syntax:

JSP Standard Tag Library Tutorial (JSTL), JSTL Tags 11

The following are the JSTL Function Tags:

i) fn:contains(): It tests if an input string contains the specified substring.

ii) fn:containsIgnoreCase(): It tets if an input string contains the specified substring in a case-insensitive way.

iii) fn:endsWith(): It tets if an input string ends with the specified suffix.

iv) fn:indexOf(): Return the index writing a string of the first occurrence of the specified string.

v) fn:join(): It joins all the elements of an array into a String.

vi) fn:length(): It returns the number of characters in a String or the number of collections in a String.

vii) fn:replace(): It is used to replacing an input string with all occurrences with the given String.

viii) fn:split(): Split a string into an array of a substring.

ix) fn:startsWith(): It tests if an input string starts with the specified prefix.

x) fn:substring(): It returns a subset of a string

xi) fn:substringAfter(): It returns a subset of a string after a specific substring.

xii) fn:substringBefore(): It returns a subset of a String before a specific substring.

xiii) fn:toLowerCase():
It converts all of the characters of a string to a lower case.

xiv) fn:toUpperCase(): It converts all of the characters of a string to the upper case.

xv) fn:trim(): It removes white spaces from both ends of a string.

4) JSTL XML Tags

JSTL XML Tags are used to work with XML documents.It is used for manipulating and creating XML documents. It provides flow control, Transformation, etc.

Syntax:

JSP Standard Tag Library Tutorial (JSTL), JSTL Tags 12

The following are the JSTL XML Tags:

i) x:out: It evaluates the expression of XPath. It is just Like <%= %>, but for XPath expression.

ii) x:parse: It is used to parse the XML data specified either via an attribute or in the tag body.

iii) x:set: It sets the value of a variable of an XPath expression.

iv) x:choose: Subtag of that will include its body if the condition evaluated to be ‘true.

v) x:when: Subtag of that includes its body if its expression evaluates to ‘true.

vi) x:otherwise: subtag of that follows the tags and run only if all of the prior conditions evaluated to ‘false.

vii) x:if: It evaluates a test XPath expression and if it is true, it processes its body. If the condition is false, then the body is ignored.

viii) x:param: It is used along with the transform tag to set a parameter in the XSLT stylesheet.

ix) x: transform: It applies an XSL transformation to an XML document.

5. JSTL SQL Tag

This tag provides SQL support. It is used for interacting with RDBS(Relational Database Management System) such as MySql, Oracle, etc. we can run database queries using SQL tags.

Syntax:

JSP Standard Tag Library Tutorial (JSTL), JSTL Tags 13

The following are the JSTL SQL Tags:

i) sql:setDataSource: It creates a simple data source suitable for prototyping.

ii) sql:query: It is used to executes the SQL queries defined in its body or through the SQL attribute.

iii) sql:update: It executed the SQL update defined in its body or through the SQL attribute.

iv) sql:param: It sets a parameter in an SQL statement to the specified value.

v) sql:dateParam: It sets a parameter in an SQL statement to the specified java.util.Date value.

vi) sql:transaction: It provides nested database action elements with a shared connection, set up to execute all statements as one transaction.