Open In App

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 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:




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:



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

This article focuses on JSTL Function tags

JSTL Function Tags

JSTL Function tags provide various string formatting 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 :

fn:contains()

It checks whether a string is contained inside the given string.

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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. 

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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. 

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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. 

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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. 

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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.

fn:join()

It is used to join an array of String with a given separator. 

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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. 

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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. 

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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.

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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.  

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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. 

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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.

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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. 

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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.

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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. 

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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! 

Article Tags :