O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

23.15. Using JSTL Functions

Problem

You want to use the built-in functions included with JSTL 1.1.

Solution

Use the proper taglib directive (with the uri value of "http://java.sun.com/jsp/jstl/functions") and prefix (e.g., the fn: in fn:contains) in your JSP.

Discussion

The JSTL 1.1 and its EL includes a nifty new functions library. These tags allow JSP developers to call built-in functions to handle and return values from Strings, arrays, Maps, and Collections. The nature of these functions will be familiar to anyone who has worked with java.lang.String and its numerous methods (see Table 23-4). Functions represent an evolution of JSTL from involving a collection of custom tags to giving you the ability to make function calls embedded inside template text.

Here is the setup that you need to use JSTL functions in your JSPs:

  1. A JSP 2.0 JSP container

  2. An implementation of JSTL 1.1 (I use the Java Web Services Developer Pack 1.2 in this recipe)

  3. A conversion of your web.xml file to the servlet API Version 2.4 (see later on in this recipe)

Example 23-17 shows the new taglib uri and prefix values to use with the functions library. This JSP uses the String "I am a test String" as input to four of the available functions: fn:length( ) , fn:contains( ), fn:toUpperCase( ), and fn:split( ).

Example 23-17. A JSP that uses JSTL 1.1 functions

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

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head><title>Using ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required