Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaJSTL Function Tags

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:

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:

  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. 

  • 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"%>
<%@ 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. 

  • 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"%>
<%@ 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. 

  • 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"%>
<%@ 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. 

  • 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"%>
<%@ 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. 

  • 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"%>
<%@ 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.

  • 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"%>
<%@ 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. 

  • 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"%>
<%@ 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. 

  • 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"%>
<%@ 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.

  • 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"%>
<%@ 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.  

  • 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"%>
<%@ 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. 

  • 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"%>
<%@ 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.

  • 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"%>
<%@ 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. 

  • 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"%>
<%@ 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:

neveropen

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"%>
<%@ 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. 

  • 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"%>
<%@ 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! 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments