Standard Tag Library (JSTL) in JSP

Last updated on May 31 2022
Mohnish Patil

Table of Contents

Standard Tag Library (JSTL) in JSP

In this blog , we will understand the different tags in JSP. 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.

Install JSTL Library

To begin working with JSP tages you need to first install the JSTL library. If you are using the Apache Tomcat container, then follow these two steps −

Step 1 − Download the binary distribution from Apache Standard Taglib and unpack the compressed file.

Step 2 − To use the Standard Taglib from its Jakarta Taglibs distribution, simply copy the JAR files in the distribution’s ‘lib’ directory to your application’s webapps\ROOT\WEB-INF\lib directory.

To use any of the libraries, you must include a <taglib> directive at the top of each JSP that uses the library.

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 −

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

Core Tags

The core group of tags are the most commonly used JSTL tags. Following is the syntax to include the JSTL Core library in your JSP −

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

Following table lists out the core JSTL Tags −

S.No. Tag & Description
1 <c:out>

Like <%= … >, but for expressions.

2 <c:set >

Sets the result of an expression evaluation in a ‘scope’

3 <c:remove >

Removes a scoped variable (from a particular scope, if specified).

4 <c:catch>

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

5 <c:if>

Simple conditional tag which evalutes its body if the supplied condition is true.

6 <c:choose>

Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>.

7 <c:when>

Subtag of <choose> that includes its body if its condition evalutes to ‘true’.

8 <c:otherwise >

Subtag of <choose> that follows the <when> tags and runs only if all of the prior conditions evaluated to ‘false’.

9 <c:import>

Retrieves an absolute or relative URL and exposes its contents to either the page, a String in ‘var’, or a Reader in ‘varReader’.

10 <c:forEach >

The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality .

11 <c:forTokens>

Iterates over tokens, separated by the supplied delimeters.

12 <c:param>

Adds a parameter to a containing ‘import’ tag’s URL.

13 <c:redirect >

Redirects to a new URL.

14 <c:url>

Creates a URL with optional query parameters

Formatting Tags

The JSTL formatting tags are used to format and display text, the date, the time, and numbers for internationalized Websites. Following is the syntax to include Formatting library in your JSP −

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

Following table lists out the Formatting JSTL Tags −

S.No. Tag & Description
1 <fmt:formatNumber>

To render numerical value with specific precision or format.

2 <fmt:parseNumber>

Parses the string representation of a number, currency, or percentage.

3 <fmt:formatDate>

Formats a date and/or time using the supplied styles and pattern.

4 <fmt:parseDate>

Parses the string representation of a date and/or time

5 <fmt:bundle>

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

6 <fmt:setLocale>

Stores the given locale in the locale configuration variable.

7 <fmt:setBundle>

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

8 <fmt:timeZone>

Specifies the time zone for any time formatting or parsing actions nested in its body.

9 <fmt:setTimeZone>

Stores the given time zone in the time zone configuration variable

10 <fmt:message>

Displays an internationalized message.

11 <fmt:requestEncoding>

Sets the request character encoding

SQL Tags

The JSTL SQL tag library provides tags for interacting with relational databases (RDBMSs) such as Oracle, mySQL, or Microsoft SQL Server.

Following is the syntax to include JSTL SQL library in your JSP −

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

Following table lists out the SQL JSTL Tags −

S.No. Tag & Description
1 <sql:setDataSource>

Creates a simple DataSource suitable only for prototyping

2 <sql:query>

Executes the SQL query defined in its body or through the sql attribute.

3 <sql:update>

Executes the SQL update defined in its body or through the sql attribute.

4 <sql:param>

Sets a parameter in an SQL statement to the specified value.

5 <sql:dateParam>

Sets a parameter in an SQL statement to the specified java.util.Date value.

6 <sql:transaction >

Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction.

XML tags

The JSTL XML tags provide a JSP-centric way of creating and manipulating the XML documents. Following is the syntax to include the JSTL XML library in your JSP.

The JSTL XML tag library has custom tags for interacting with the XML data. This includes parsing the XML, transforming the XML data, and the flow control based on the XPath expressions.

<%@ taglib prefix = "x"

   uri = "http://java.sun.com/jsp/jstl/xml" %>

Before you proceed with the examples, you will need to copy the following two XML and XPath related libraries into your <Tomcat Installation Directory>\lib

Following is the list of XML JSTL Tags −

S.No. Tag & Description
1 <x:out>

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

2 <x:parse>

Used to parse the XML data specified either via an attribute or in the tag body.

3 <x:set >

Sets a variable to the value of an XPath expression.

4 <x:if >

Evaluates a test XPath expression and if it is true, it processes its body. If the test condition is false, the body is ignored.

5 <x:forEach>

To loop over nodes in an XML document.

6 <x:choose>

Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise> tags.

7 <x:when >

Subtag of <choose> that includes its body if its expression evalutes to ‘true’.

8 <x:otherwise >

Subtag of <choose> that follows the <when> tags and runs only if all of the prior conditions evaluates to ‘false’.

9 <x:transform >

Applies an XSL transformation on a XML document

10 <x:param >

Used along with the transform tag to set a parameter in the XSLT stylesheet

JSTL Functions

JSTL includes a number of standard functions, most of which are common string manipulation functions. Following is the syntax to include JSTL Functions library in your JSP −

<%@ taglib prefix = "fn"

   uri = "http://java.sun.com/jsp/jstl/functions" %>

Following table lists out the various JSTL Functions −

S.No. Function & Description
1 fn:contains()

Tests if an input string contains the specified substring.

2 fn:containsIgnoreCase()

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

3 fn:endsWith()

Tests if an input string ends with the specified suffix.

4 fn:escapeXml()

Escapes characters that can be interpreted as XML markup.

5 fn:indexOf()

Returns the index withing a string of the first occurrence of a specified substring.

6 fn:join()

Joins all elements of an array into a string.

7 fn:length()

Returns the number of items in a collection, or the number of characters in a string.

8 fn:replace()

Returns a string resulting from replacing in an input string all occurrences with a given string.

9 fn:split()

Splits a string into an array of substrings.

10 fn:startsWith()

Tests if an input string starts with the specified prefix.

11 fn:substring()

Returns a subset of a string.

12 fn:substringAfter()

Returns a subset of a string following a specific substring.

13 fn:substringBefore()

Returns a subset of a string before a specific substring.

14 fn:toLowerCase()

Converts all of the characters of a string to lower case.

15 fn:toUpperCase()

Converts all of the characters of a string to upper case.

16 fn:trim()

Removes white spaces from both ends of a string.

 

So, this brings us to the end of blog. This Tecklearn ‘Standard Tag Library (JSTL) in JSP’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn JSP and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/java-and-jee-training/

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

  • Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
  • Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
  • Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

  • Java Fundamentals
  • Introduction to Java Basics
  • Features of Java
  • Various components of Java language
  • Benefits of Java over other programming languages
  • Key Benefits of Java

Installation and IDE’s for Java Programming Language

  • Installation of Java
  • Setting up of Eclipse IDE
  • Components of Java Program
  • Editors and IDEs used for Java Programming
  • Writing a Simple Java Program

Data Handling and Functions

  • Data types, Operations, Compilation process, Class files, Loops, Conditions
  • Using Loop Constructs
  • Arrays- Single Dimensional and Multi-Dimensional
  • Functions
  • Functions with Arguments

OOPS in Java: Concept of Object Orientation

  • Object Oriented Programming in Java
  • Implement classes and objects in Java
  • Create Class Constructors
  • Overload Constructors
  • Inheritance
  • Inherit Classes and create sub-classes
  • Implement abstract classes and methods
  • Use static keyword
  • Implement Interfaces and use it

Polymorphism, Packages and String Handling

  • Concept of Static and Run time Polymorphism
  • Function Overloading
  • String Handling –String Class
  • Java Packages

Exception Handling and Multi-Threading

  • Exception handling
  • Various Types of Exception Handling
  • Introduction to multi-threading in Java
  • Extending the thread class
  • Synchronizing the thread

File Handling in Java

  • Input Output Streams
  • io Package
  • File Handling in Java

Java Collections

  • Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
  • Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
  • Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

  • Introduction to SQL: Connect, Insert, Update, Delete, Select
  • Introduction to JDBC and Architecture of JDBC
  • Insert/Update/Delete/Select Operations using JDBC
  • Batch Processing Transaction
  • Management: Commit and Rollback

Java Enterprise Edition – Servlets

  • Introduction to J2EE
  • Client Server architecture
  • URL, Port Number, Request, Response
  • Need for servlets
  • Servlet fundamentals
  • Setting up a web project in Eclipse
  • Configuring and running the web app with servlets
  • GET and POST request in web application with demo
  • Servlet lifecycle
  • Servlets Continued
  • Session tracking and filter
  • Forward and include Servlet request dispatchers

Java Server Pages (JSP)

  • Fundamentals of Java Server Page
  • Writing a code using JSP
  • The architecture of JSP
  • JSP Continued
  • JSP elements: Scriptlets, expressions, declaration
  • JSP standard actions
  • JSP directives
  • Introduction to JavaBeans
  • ServletConfig and ServletContext
  • Servlet Chaining
  • Cookies Management
  • Session Management

Hibernate

  • Introduction to Hibernate
  • Introduction to ORM
  • ORM features
  • Hibernate as an ORM framework
  • Hibernate features
  • Setting up a project with Hibernate framework
  • Basic APIs needed to do CRUD operations with Hibernate
  • Hibernate Architecture

POJO (Plain Old Java Object)

  • POJO (Plain Old Java Object)
  • Persistent Objects
  • Lifecycle of Persistent Object

Spring

  • Introduction to Spring
  • Spring Fundamentals
  • Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "Standard Tag Library (JSTL) in JSP"

Leave a Message

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