<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
Introduction to JSTL Functions
JSTL (JavaServer Pages Standard Tag Library) functions are a set of functions provided to ease the development of JSP pages by enabling common operations without the need for scriptlets. These functions help in performing string manipulations, collection operations, and other basic tasks seamlessly within JSP pages.
List of JSTL Function Tags
Let's list all the JSTL functions and their meanings and examples.
1. fn:contains
Function
This function checks whether the given string is present in the input as a substring. It does a case-sensitive check.
Syntax:
boolean fn:contains(String inputstring, String checkstring)
Example:
<h1>JSTL fn:contains() function example</h1>
<c:set var="str1" value="Java Guides" />
<c:set var="str2" value="Guides" />
<c:if test="${fn:contains(str1, str2)}">
<c:out value="'Guides' substring present in 'Java Guides' string" />
</c:if>
<hr>
2. fn:containsIgnoreCase
Function
It does a case-insensitive check to see whether the provided string is a substring of the input.
Syntax:
boolean fn:containsIgnoreCase(String input, String checkstring)
Example:
<h1>JSTL fn:containsIgnoreCase() function example</h1>
<c:set var="str1" value="Java Guides" />
<c:set var="str2" value="java guides" />
<c:if test="${fn:containsIgnoreCase(str1, str2)}">
<c:out value="'java guides' substring present in 'Java Guides' string" />
</c:if>
<hr>
3. fn:indexOf
Function
This JSTL function finds the start position of a string in the provided string. The function returns -1 when the string is not found in the input.
Syntax:
int fn:indexOf(String input, String searchString)
Example:
<h1>JSTL fn:indexOf() function example</h1>
<c:set var="str1" value="Java Guides" />
<c:set var="str2" value="Guides" />
<p>Index of "Guides" in "Java Guides": ${fn:indexOf(str1, str2)}</p>
<hr>
4. fn:escapeXML
Function
This function is used for HTML/XML character escaping, which means it treats HTML/XML tags as a string. It is similar to the escapeXml attribute of the <c:out>
tag.
Syntax:
String fn:escapeXml(String inputString)
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>")}
<hr>
5. fn:join
and fn:split
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 based on a delimiter.
Syntax for fn:join
:
String fn:join(String arrayofstrings, String separator)
Example:
<h1>JSTL fn:join() function example</h1>
<%
String arr[] = { "Java", "Programming", "Language" };
session.setAttribute("names", arr);
%>
${fn:join(names, " & ")}
<hr>
Syntax for fn:split
:
String[] fn:split(String inputString, String delimiterString)
Example:
<h1>JSTL fn:split() function example</h1>
<c:set var="msg" value="This is an example of JSTL function" />
<c:set var="arrayOfMsg" value="${fn:split(msg,' ')}" />
<c:forEach var="i" items="${arrayOfMsg}">
arrayOfMsg[${i.index}]: ${i}<br>
</c:forEach>
<hr>
6. fn:length
Function
The JSTL function fn:length()
is used to compute the length of a string or to find out the number of elements in a collection. It returns the length of the object.
Syntax:
int fn:length(Object obj)
Example:
<h1>JSTL fn:length() function example</h1>
<c:set var="str" value="Java Guides" />
Length of str is: ${fn:length(str)}
<hr>
7. fn:startsWith
Function
It checks if the specified string is a prefix of a given string.
Syntax:
boolean fn:startsWith(String input, String prefix)
Example:
<h1>JSTL fn:startsWith() function example</h1>
<c:set var="str" value="Java Guides" />
Java Guides starts with Java: ${fn:startsWith(str, 'Java')}
<hr>
8. fn:endsWith
Function
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(String inputString, String suffixString)
Example:
<h1>JSTL fn:endsWith() function example</h1>
<c:set var="str" value="Java Guides" />
Java Guides ends with Guides: ${fn:endsWith(str, 'Guides')}
<hr>
9. fn:substring
Function
This JSTL function is used for getting a substring from the provided string.
Syntax:
String fn:substring(String inputString, int start, int end)
Example:
<h1>JSTL fn:substring() function example</h1>
<c:set var="str" value="Java Guides" />
${fn:substring(str, 5, 10)}
<hr>
10. fn:substringAfter
Function
This method is used for getting a substring that is present in the input string before a specified string.
Syntax:
String fn:substringAfter(String inputString, String subString)
Example:
<h1>JSTL fn:substringAfter() function example</h1>
<c:set var="str" value="Java Guides" />
${fn:substringAfter(str, "Gu")}
<hr>
11. fn:substringBefore
Function
This method gets a substring from the input, which comes after a specified string.
Syntax:
String fn:substringBefore(String inputString, String subString)
Example:
<h1>JSTL fn:substringBefore() function example</h1>
<c:set var="str" value="Java Guides" />
${fn:substringBefore(str, "Gu")}
<hr>
12. fn:trim
Function
This fn:trim()
function removes spaces from the beginning and end of a string.
Syntax:
String fn:trim(String input)
Example:
<h1>JSTL fn:trim() function example</h1>
<c:set var="str" value=" Java Guides " />
Trim of " Java Guides " is: ${fn:trim(str)}
<hr>
13. fn:toUpperCase
Function
It is the opposite of fn:toLowerCase()
function. It converts the input string to an uppercase string.
Syntax:
String fn:toUpperCase(String input)
Example:
<h1>JSTL fn:toUpperCase() function example</h1>
<c:set var="str" value="java guides" />
Upper case of "java guides" is: ${fn:toUpperCase(str)}
<hr>
14. fn:toLowerCase
Function
This function is used for converting an input string to a lower case string.
Syntax:
String fn:toLowerCase(String input)
Example:
<h1>JSTL fn:toLowerCase() function example</h1>
<c:set var="str" value="JAVA GUIDES" />
Lower case of "JAVA GUIDES" is: ${fn:toLowerCase(str)}
<hr>
15. fn:replace
Function
This fn:replace()
function searches for a string in the input and replaces it with the provided string. It performs
case-sensitive processing.
Syntax:
String fn:replace(String input, String searchFor, String replaceWith)
Example:
<c:set var="author" value="Ramesh Fadatare" />
<c:set var="randomString" value="abc def abc ghi ABC" />
${fn:replace(author, "Ramesh", "Umesh")}
${fn:replace(randomString, "abc", "hello")}
<hr>
Conclusion
JSTL functions are a powerful feature in JSP that simplify common tasks such as string manipulation, collection handling, and HTML/XML escaping. By using these functions, developers can write cleaner, more readable, and maintainable JSP pages. This guide has covered the most commonly used JSTL functions along with examples to help you understand how to use them effectively in your web applications.
Comments
Post a Comment
Leave Comment