JSTL(JSP Standard Tag Library) is a collection of custom tags that provide common functionalities like flow control, database operations, etc. JSTL tags can be embedded in Java Server Pages just like other HTML tags. It is convenient for front-end developers to work with HTML-like tags for including logic in webpages rather than writing Java code in scripts. To use JSTL tags, the following dependencies must be included in pom.xml in a maven project:
XML
dependency>
< groupId >javax.servlet</ groupId >
< artifactId >jstl</ artifactId >
< version >1.2</ version >
</ dependency >
|
Alternatively, you can download the jar files from this link. JSTL tags are grouped into five major categories:
- Core Tags
- Formatting Tags
- SQL Tags
- XML Tags
- Function Tags
This article focuses on JSTL Function tags
JSTL Function Tags
JSTL Function tags provide various string formatting functions.
- prefix: fn
- uri: http://java.sun.com/jsp/jstl/functions
- Tag handler class: org.apache.taglibs.standard.functions.Functions
Include the <taglib> tag in the JSP as follows
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
The following tags are included in the JSTL function tag library :
- boolean contains(String, String)
- boolean containsIgnoreCase(String, String)
- boolean endsWith(String, String)
- String escapeXml(String, String)
- int indexOf(String, String)
- String join(String[], String)
- int length(Object)
- String replace(String, String, String)
- String[] split(String, String)
- boolean startsWith(String, String)
- String substring(String, int, int)
- String substringAfter(String, String)
- String substringBefore(String, String)
- String toLowerCase(String)
- String toUpperCase(String)
- String trim(String)
fn:contains()
It checks whether a string is contained inside the given string.
- Syntax: boolean contains(String s1, String s2)
- Return value: boolean (true if the string is present within the given string, false otherwise)
- Parameters:
- s1: String to be processed.
- s2: String that must be contained in s1.
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-Contains</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value = "GeeksForGeeks" />
${fn:contains(name,'Geeks')}
</ h1 >
</ body >
</ html >
|
Output:
true
fn:containsIgnoreCase()
It checks whether a string is contained inside the given string in a case-insensitive way.
- Syntax: boolean containsIgnore(String s1, String s2)
- Return value: boolean (true if the string is present within the given string, false otherwise)
- Parameters:
- s1: String to be processed.
- s2: Substring that is to be searched.
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-ContainsIgnoreCase</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value = "GeeksForGeeks" />
${fn:containsIgnoreCase(name,'geeks')}
</ h1 >
</ body >
</ html >
|
Output:
true
fn:endsWith()
It checks whether a string ends with the given suffix.
- Syntax: boolean endsWith(String s1, String s2)
- Return value: boolean (true if the string ends with the given suffix, false otherwise)
- Parameters:
- s1 : string to be processed.
- s2 : suffix
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-endsWith</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value = "GeeksForGeeks" />
${fn:endsWith(name,'Geeks')}
</ h1 >
</ body >
</ html >
|
Output:
true
fn:escapeXml()
It is used to escape the characters that can be interpreted as XML.
- Syntax: String escapeXml(String s)
- Return value: String (String obtained after escaping XML characters)
- Parameters:
- s: String to be processed.
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-EscapeXml</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value="<abc>GeeksForGeeks</ abc >"/>
Without escapeXml : ${name}
With escapeXml : ${fn:escapeXml(name)}
</ h1 >
</ body >
</ html >
|
Output:
Without escaleXml : GeeksForGeeks
With escapeXml : <abc>GeeksForGeeks</abc>
fn:indexOf()
It finds the index of the first occurrence of a given substring.
- Syntax: int indexOf(String s1, String s2)
- Return value: int(index of first occurrence of substring if found, – 1 otherwise)
- Parameters:
- s1: string to be processed.
- s2: substring whose index is to be determined.
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-indexOf</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value = "GeeksForGeeks" />
${fn:indexOf(name,"For")}
</ h1 >
</ body >
</ html >
|
Output:
5
fn:split()
It is used to split a string into an array of substrings based on the given separator.
- Syntax: String[] split(String s1, String s2)
- Return value: String[] (array of substrings obtained after splitting )
- Parameters:
- s1: spring that is to be split.
- s2: separator
fn:join()
It is used to join an array of String with a given separator.
- Syntax: String join (String s1[], String s2)
- Return value: String (String formed after joining the elements of the array with a given separator)
- Parameters:
- s1: array of strings to be joined.
- s2: separator
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-Split-Join</ title >
</ head >
< body >
< h1 >
< c:set var = "str" value = "Geeks For Geeks" />
Original string : ${str} < br >
< c:set var = "str1" value = "${fn:split(str,' ')}" />
< c:set var = "str2" value = "${fn:join(str1,'-')}" />
String after join : ${str2}
</ h1 >
</ body >
</ html >
|
Output:
Original string : Geeks For Geeks
String after join : Geeks-For-Geeks
fn:length()
It is used to determine the length of a string or the size of a collection.
- Syntax: int length(Object o)
- Return value: int (length of string or size of collection object)
- Parameters:
- String or collection whose length is to be determined.
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-length</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value = "GeeksForGeeks" />
Length : ${fn:length(name)}
</ h1 >
</ body >
</ html >
|
Output:
Length : 13
fn:replace()
It is used to replace all the occurrences of a substring with another substring.
- Syntax: String replace(String s1, String s2, String s3)
- Return value: String (string obtained after replacements)
- Parameters:
- s1: string to be processed.
- s2: old substring
- s3: new substring
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-Replace</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value = "GeeksForGeeks" />
${fn:replace(name,"e","x")}
</ h1 >
</ body >
</ html >
|
Output:
GxxksForGxxks
fn:startsWith()
It checks whether a string starts with the given prefix.
- Syntax: boolean startsWith(String s1, String s2)
- Return value: boolean (true if string starts with the given prefix, false otherwise)
- Parameters:
- s1: string to be processed.
- s2: prefix
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-startsWith</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value = "GeeksForGeeks" />
${fn:startsWith(name,'Geeks')}
</ h1 >
</ body >
</ html >
|
Output:
true
fn:substring()
It returns a substring of a given string between specified start and end indices.
- Syntax: String substring(String s, int start, int end)
- Return value: String (substring obtained)
- Parameters:
- s: string whose substring is to be determined.
- start: start index of the substring.
- end: end index of the substring.
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-Substring</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value = "GeeksForGeeks" />
${fn:substring(name,5,8)}
</ h1 >
</ body >
</ html >
|
Output:
For
fn:substringAfter()
It is used to return a substring of the given string present after the specified target string.
- Syntax: String substringAfter(String s1, String s2)
- Return value: String (substring obtained)
- Parameters:
- s1: string whose substring is to be determined.
- s2: target string
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-SubstringAfter</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value = "GeeksForGeeks" />
${fn:substringAfter(name,"For")}
</ h1 >
</ body >
</ html >
|
Output:
Geeks
fn:substringBefore()
It is used to return a substring of the given string present before the specified target string.
- Syntax: String substringBefore(String s1, String s2)
- Return value: String (substring obtained)
- Parameters:
- s1: string whose substring is to be determined.
- s2: target string
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-SubstringBefore</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value = "GeeksForGeeks" />
${fn:substringBefore(name,"For")}
</ h1 >
</ body >
</ html >
|
Output:
Geeks
fn:toLowerCase()
It is used to convert a given string to a lowercase.
- Syntax: String toLowerCase(String s)
- Return value: String (String in lowercase)
- Parameters:
- s: string to be processed.
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-toLowerCase</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value = "GeeksForGeeks" />
${fn:toLowerCase(name)}
</ h1 >
</ body >
</ html >
|
Output:
geeksforgeeks
fn:toUpperCase()
It is used to convert a given string to uppercase.
- Syntax: String toUpperCase(String s)
- Return value: String (String in uppercase)
- Parameters:
- s: string to be processed.
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-toUpperCase</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value = "GeeksForGeeks" />
${fn:toUpperCase(name)}
</ h1 >
</ body >
</ html >
|
Output:
GEEKSFORGEEKS
fn:trim()
It is used to remove blank spaces from both ends of the given string.
- Syntax: String trim(String s)
- Return value: String (String obtained after removal of blank spaces)
- Parameters:
- s: string to be processed.
Example:
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
< title >Function-trim</ title >
</ head >
< body >
< h1 >
< c:set var = "name" value = " GeeksForGeeks " />
!${name}! < br >
!${fn:trim(name)}!
</ h1 >
</ body >
</ html >
|
Output:
! GeeksForGeeks !
!GeeksForGeeks!
Similar Reads
JSTL Function Tags
JSTL(JSP Standard Tag Library) is a collection of custom tags that provide common functionalities like flow control, database operations, etc. JSTL tags can be embedded in Java Server Pages just like other HTML tags. It is convenient for front-end developers to work with HTML-like tags for including
8 min read
JSTL fn:startsWith() Function
In JSTL the fn:startsWith() function is used to check if a given string starts with a particular prefix. This function returns the boolean value as True or False according to the check result. If the input string begins with the prefix then true is been returned else false is been returned. In this
2 min read
JSTL Core Tags
JSTL(JSP Standard Tag Library) is a collection of custom tags that provide common functionalities like flow control, database operations, etc. JSTL tags can be embedded in Java Server Pages just like other HTML tags. It is convenient for front-end developers to work with HTML-like tags for including
8 min read
JSTL Formatting tags
JSTL stands for JavaServer Pages Standard Tag Library. It is introduced in the JSP 2.0 version to simplify the JSP. By using JSTL, JSP becomes a 100% tag-based application. JSTL can provide the group of tags that are used to perform the tasks in the JSP pages without using the Java code. JSTL tags a
3 min read
JSTL fn:length() Function
In JSTL, the fn:length() function is mainly used to specify the length or size of a collection, array, or string in JSP. This function returns the number of elements in the given object as input. Used to retrieve the size or length information within the JSTL expressions. The fn:length() function is
2 min read
Custom Tags in JSP
Custom tags are user-defined action tags that can be used within Java Server Pages. A tag handler is associated with each tag to implement the operations. Therefore, it separates the business logic from JSP and helps avoid the use of scriptlet tags. The scriptlet tag embeds java code inside the JSP
8 min read
Introduction to JSP
In Java, JSP stands for Jakarta Server Pages( (JSP; formerly JavaServer Pages)). It is a server-side technology that is used for creating web applications. It is used to create dynamic web content. JSP consists of both HTML tags and JSP tags. In this, JSP tags are used to insert JAVA code into HTML
6 min read
JSTL Core <c:import> Tag
JavaServer Pages (JSP) is a technology through which developers can create dynamic, server-side web applications. The Java code can be easily integrated into HTML templates to generate dynamic content. The JavaServer Pages Standard Tag Library (JSTL), is one of the main components of JSP. The <c:
2 min read
JSP | Expression tag
Expression tag is one of the scripting elements in JSP. Expression Tag in JSP is used for writing your content on the client-side. We can use this tag for displaying information on the client's browser. The JSP Expression tag transforms the code into an expression statement that converts into a valu
2 min read
JSTL Formatting < fmt:bundle > Tag
In JSTL the <fmt:bundle> tag is mainly used to reference and format messages from the resource bundles. This tag allows us to get localized messages based on the user's locale. The main difference between <fmt:setBundle> and <fmt:bundle> is that, the <fmt:setBundle> sets the
3 min read
JSTL Core <c:forEach> Tag
The JSTL Core <c:forEach> tag is used to iterate over a collection of objects or a range of values. It is the most commonly used JSTL tag because it can be used to implement a variety of functionality. Attributes of <c:forEach>The <c:forEach> tag has the following attributes: var:
3 min read
JSTL Formatting <fmt:parseDate> Tag
In JSTL, the formatting <fmt:parseDate> tag is used to parse the date strings into proper java.util.Date objects. This is useful if we need to represent a string in a formatted Date object. Using the tag the tasks of date parsing and formatting can be easily done in Java Server Pages applicati
3 min read
JSTL Core <c:out> Tag
Java Server Pages (JSP) is a technology used to create web applications with the popular programming language Java. With JSP we can write Java code inside a HTML page, making it easier to develop web applications. To enhance the feature set and usability of JSPs, Java Server Pages Standard Tag Libra
5 min read
JSTL Core <c:url> Tag
To construct and manage URLs within JSP pages we have this tag <c:url> for your JSP application. It is among the most fundamental tags within JSTL's core library. Purpose of <c:url> tag. Uses of JSTL Core <c:url>The <c:url> tag primarily serves two key purposes: URL Formattin
3 min read
Custom Tags with Attributes in JSP
Custom tags are user-defined action tags that can be used within Java Server Pages. A custom tag may be defined with a number of attributes for providing additional information to perform the operation associated with the tag. Each attribute is mapped to a property in the tag handler. While implemen
5 min read
JSTL Formatting <fmt:formatNumber> Tag
In JSTL the <fmt:formatNumber> tag is mainly used to format the numeric value according to the specified localization setting, which also includes the grouping separators, currency symbols, and decimal separators. This function mainly simplified the presentation of numeric data in JSP by offer
3 min read
JSTL Formatting <fmt:setBundle> Tag
The <fmt:setBundle> tag in JSTL is used to set or load the resource bundle that is to be used in localization. This tag allows us to specify the bundle that contains the localized messages and enables the rendering of content in various languages that are specified by the user. In this article
3 min read
JSTL Formatting <fmt:timeZone> Tag
In JSTL applications, the <fmt:timeZone> tag is mainly used to set the time zone for the data and time formatting. We can set a particular time zone for showing dates and times.The main difference between <fmt:setTimeZone> and <fmt:timeZone> is that, the <fmt:setTimeZone> is
2 min read
JSTL Formatting <fmt:formatDate> Tag
In developing JSTL applications, we used date and time as one of the representative components. But in some cases, the date or time which is displayed is to be more sorted and in a proper format. So to solve this, we can use the JSTL Formatting <fmt:formatDate> Tag. tag is used to format the t
3 min read