JSTL Functions with Examples

The JSTL function provides a number of standard functions, most of these functions are common string manipulation functions. The syntax used for including JSTL function library in your JSP is:

<%@ tagliburi="http://java.sun.com/jsp/jstl/functions"prefix="fn" %>

JSTL Function Tags List

Let's list out all the JSTL function with it's meaning.

fn:contains function: This function checks whether the given string is present in the input as sub-string. It does a case sensitive check.

fn:containsIgnoreCase(): It does a case-insensitive check to see whether the provided string is a sub-string of input.

fn:indexOf(): It is used for finding out the start position of a string in the provided string. Function returns -1 when string is not found in the input.

fn:escapeXML(): It is used for HTML/XML character escaping which means it treats html/xml tags as a string. Similar to the escapeXml attribute of <c:out> tag.

fn:join() and fn:split() functions: JSTL functions: fn:join() concatenates the strings with a given separator and returns the output string. fn:split() splits a given string into an array of substrings.

fn:length(): The JSTL function fn:length() is used for computing the length of a string or to find out the number of elements in a collection. It returns the length of the object.

fn:startsWith(): It checks the specified string is a prefix of a given string.

fn:endsWith(): fn:endsWith() JSTL function is used for checking the suffix of a string. It checks whether the given string ends with a particular string.

fn:substring(): This JSTL function is used for getting a substring from the provided string.

fn:substringAfter(): It is used for getting a substring which is present in the input string before a specified string.

fn:substringBefore(): It gets a substring from the input which comes after a specified string.

fn:trim(): JSTL Function fn:trim() removes spaces from beginning and end of a string and function.

fn:toUpperCase(): It is just opposite of fn:toLowerCase() function. It converts input string to an uppercase string.

fn:toLowerCase(): This function is used for converting an input string to a lower case string.

fn:replace(): fn:replace() function search for a string in the input and replace it with the provided string. It does case-sensitive processing.

Let's discuss each JSTL function with an example.

1. fn:contains function

This function checks whether the given string is present in the input as sub-string. It does a case sensitive check.

fn:escapeXML()

This function is used for HTML/XML character escaping which means it treats html/xml tags as a string. Similar to the escapeXml attribute of <c:out> tag.

System:

String escapeXml(String input_string)

Example

Message1: <b>Hi This is just a message</b>
<br>Message2: <i>This is an example</i>
<br>Message1 and fn:escapeXml(): ${fn:escapeXml("<b>Hi This is just a message</b>")}
<br>Message2 and fn:escapeXml(): ${fn:escapeXml("<i>This is an example</i>")}

fn:join() fn:join()

It concatenates the strings with a given separator and returns the output string.

Syntax:

String fn:join(String arrayofstrings, String separator)

Example:

fn:split()

It splits a given string into an array of substrings. Splitting process considers the delimiter string which we provide during a function call. I.e. we provide the string and delimiter as arguments to the function and it returns the array of strings after splitting the input based on the delimiter string.

Example:

fn:endsWith()

The fn:endsWith() JSTL function is used for checking the suffix of a string. It checks whether the given string ends with a particular string.

Syntax:

boolean fn:endsWith(input_string, suffix_string)

It validates whether the input_string ends with suffix_string. If the test is successful then the function returns true else it gives false. The return type of the function is boolean and it receives both the strings as arguments.

Syntax:

Three string arguments and return type are also String. It searches the search_for string in input and replaces it with replace_with string. If the string is not found it returns the actual input as it is.