JavaRanch Home    
This page:         last edited 16 October 2009         What's Changed?         Edit

Define E L Functions   

Under JSP 2.0 you can define functions that can be referenced from within EL expressions. For full details, please see section JSP.2.6 in the JSP 2.0 Specification:

Let's say that you want to use some static functions such as those defined by the Jakata Commons StringEscapeUtils? class ( from within your EL expressions. In particular, let's pick the escapeJavaScript method whose signature is:

public static String escapeJavaScript( String str )

In order to define an EL function, a <function> element must be added to an existing or new TLD file that is in the scope of your application and declared within the referencing JSP page. For our example, if we wanted to name the EL function the same name as the static method (though they do not need to be the same), we could add:

        java.lang.String escapeJS( java.lang.String )

to a TLD file in our web application. (Of course, this class must be available in the classpath of the web app).

Note that the class name of the enclosing class must be fully qualified, and note that the class names in the signature must also be fully qualified.

On a JSP page where you wish to reference your new EL function, you would declare the taglib as any other. For example:

<%@ taglib uri="" prefix="whatever" %>

and then you could use the function in any EL expression on that page. Be sure that the uri value matches the URI defined within your TLD file. For example:


Let's say that we have a scoped variable named someText that contains text that we want to emit into the context of a JavaS'cript string. In order to make sure that there will be no quoting problems, we want to call our escape function on the string so that any embedded quotes will be escaped and will not interfere with the syntax of the JavaS'cript notation on the page.


  var someTextVar = '${whatever:escapeJavaScript( someText )}';

Cool stuff, no?

A fully worked example of defining and using an EL function can be found at


JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton