Java EE 6 with GlassFish 3 Application Server : JSP Standard Tag Library - JSTL functions
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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=UTF-8">
<title>Function Tag Demo</title>
</head>
<body>
<c:set var="nameArr" value="${fn:split('Kevin,Danielle,Alex,Beatrice',',')}" />
We have a list of ${fn:length(nameArr)} names, here they are:
<br />
<ol>
<c:forEach var="currentName" items="${nameArr}">
<li />
${fn:toUpperCase(currentName)}
<br />
</c:forEach>
</ol>
</body>
</html>
After packaging this JSP in a WAR file, deploying it and pointing the browser to its URL, we should see a page like the following:
This JSP illustrates the use of some of the JSTL functions. The fn:split() function splits a String into an array of strings using the character specified by its second parameter as a delimiter.
Notice that the strings inside the fn:split() function are enclosed inside single quotes. JSTL allows this as using double quotes for the strings would have resulted in illegal syntax, as the fn:split() function is already inside double quotes.
In the example, the fn:length() function returns the number of elements in the array we created when the fn:split() function was executed. Like we mentioned earlier, the fn:length() function can also take a Collection or a String as a parameter. When applied to a Collection, the function returns the number of elements in it. When applied to a String, the function returns the number of characters in the String.
The next function illustrated in the example is fn:toUpperCase(), which simply makes every alphabetical character in the String it takes as a parameter uppercase. There are many other JSTL functions, all of them are very intuitive to use. The following table lists all the JSTL functions:
| Function | Description | Example |
|---|---|---|
| fn:contains(String, String) | Returns a boolean indicating if the second parameter is contained in the first one. | ${fn:contains("envir onment", "iron")} |
| fn:containsIgnoreCase( String, String) | Case-insensitive version of fn:contains(). | ${fn:containsIgnoreC ase("environment", "Iron")} |
| fn:endsWith(String, String) | Returns a boolean indicating if the first parameter ends with a string equal to the second parameter. | ${fn:endsWith("Glass Fish", "Fish")} |
| fn:escapeXml(String) | Returns a string with all XML characters in the parameter escaped into their respective XML character entity code. | ${fn:escapeXml( "<html>")} |
| fn:indexOf(String, String) | Returns an int indicating the index of the second parameter in the first parameter. Returns -1 if the second parameter is not a substring of the first parameter. | ${fn:indexOf("GlassF ish", "Fish")} |
| fn:join(String[], String) | Returns a string composed of the elements in the first parameter, using the second parameter as a delimiter. | ${fn:join(arrayVar," , ")} |
| fn:length(Object) | Returns the length of an array, the size of a collection, or the length of a string, depending on the type of the parameter. | ${fn:length("String, Collection or Array")} |
| fn:replace(String, String, String) | Returns a string replacing every instance of the second parameter on the first parameter with the third parameter. | ${fn:replace("Crysta lFish", "Glass")} |
| fn:startsWith(String, String) | Returns a boolean indicating if the first parameter starts with the first parameter. | ${fn:startsWith("Gla ssFish", "Glass")} |
| fn:split(String, String) | Returns an array of strings containing elements in the first parameter as delimited by the second parameter. | ${fn:split("Eeny, meeny", ",")} |
| fn:substring(String, int, int) | Returns a string containing the substring in the first parameter, starting at the index indicated by the second parameter and ending just before the index indicated by the second parameter. | ${fn:substring( "0123456789", 3, 6)} |
| fn:substringAfter (String, String) | Returns a string containing the substring in the first parameter, starting after the first occurrence of the second parameter until the end of the first parameter. | ${fn:substringAfter( "GlassFish", "Glass")} |
| fn:substringBefore (String, String) | Returns a string containing the substring in the first parameter, starting before the first occurrence of the second parameter until the end of the first parameter. | ${fn:substringBefore ("GlassFish", "Fish")} |
| fn:toLowerCase (String) | Returns a string containing a version of the parameters with all alphabetical characters as lowercase. | ${fn:toLowerCase( "GlassFish")} |
| fn:toUpperCase (String) | Returns a string containing a version of the parameters with all alphabetical characters as uppercase. | ${fn:toUpperCase (" GlassFish ")} |
| fn:trim(String) | Returns a string containing a modified version of the parameter with all whitespace at the beginning and end of the parameter removed. | ${fn:trim(" Gla ssFish ")} |