Nearby lessons

15 of 30

JSP - EL Functions

📌 What You Will Learn
  • Write a Java class with static methods for EL functions
  • Configure EL functions in a TLD file
  • Invoke EL functions in JSPs using the taglib directive

EL Functions let you call Java methods directly from EL expressions. They are an alternative to custom tags — the page designer only needs to know the function name and TLD URI.

Why EL Functions?

EL's main objective is to separate Java code from JSPs. If you have business logic, you can put it in a Java class and invoke it through EL syntax.

The page designer only needs to know:

  • The function name
  • The TLD file URI

EL Functions are considered an alternative to custom tags.

Steps to Develop an EL Function

  1. Write a Java class with public static methods
  2. Write a TLD file to map the Java class to JSP
  3. Write a taglib directive to make the function available
  4. Write the EL function call

Step 1: Write the Java Class

Any Java class can act as a repository for EL functions. The method must be:

  • public
  • static
  • Can take parameters
  • void return type is not recommended (but legal)
Example03
JCode Cell
1 
2package pack1;
3 
4public class DiceRoller {
5 public static int rollDice() {
6 return (int)((Math.random() * 6) + 1);
7 }
8}
9

Step 2: Write the TLD File

The TLD file maps the Java class to JSP using the <function> tag with 4 child elements:

Example04
JCode Cell
1 
2<taglib version="2.1">
3 <tlib-version>1.2</tlib-version>
4 <uri>DiceFunctions</uri>
5 
6 <function>
7 <description>Roll a dice</description>
8 <name>rollIt</name>
9 <function-class>pack1.DiceRoller</function-class>
10 <function-signature>int rollDice()</function-signature>
11 </function>
12</taglib>
13

TLD Function Elements

ElementPurpose
<name>Function name used in EL (e.g. rollIt)
<function-class>Fully qualified Java class name
<function-signature>Return type, method name, and parameter types (e.g. int rollDice())
<description>Optional description

Step 3 & 4: Taglib Directive and EL Call

Example06
JCode Cell
1 
2<%-- Step 3: Declare the taglib --%>
3<%@ taglib prefix="mine" uri="DiceFunctions" %>
4 
5<%-- Step 4: Call the function in EL --%>
6<h1>Your 3-digit Lucky number is:
7 ${mine:rollIt()}
8 ${mine:rollIt()}
9 ${mine:rollIt()}
10</h1>
11

Execution Flow

When the JSP engine encounters an EL function call:

  1. Finds the taglib directive matching the prefix
  2. From the taglib, finds the TLD file matching the URI
  3. From the TLD, finds the Java class and method signature
  4. Executes the method and returns the result to the EL expression

Demo: Wrapping Math.sqrt()

Write an application to invoke Math.sqrt() as an EL function named m1():

Example08
JCode Cell
1 
2test.jsp:
3<%@ taglib prefix="mine" uri="MathFunctions" %>
4<h1>
5 Square Root of 4: ${mine:m1(4)}<br>
6 Square Root of 36: ${mine:m1(36)}
7</h1>
8 
9MyTld.tld:
10<taglib version="2.1">
11 <tlib-version>1.2</tlib-version>
12 <uri>MathFunctions</uri>
13 <function>
14 <name>m1</name>
15 <function-class>java.lang.Math</function-class>
16 <function-signature>double sqrt(double)</function-signature>
17 </function>
18</taglib>
19
📝 Key Takeaways
  • EL functions must be public static methods
  • Configure functions in a TLD file with name, class, and signature
  • Invoke with ${prefix:functionName()} after declaring the taglib

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3