Nearby lessons
15 of 30JSP - 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
- Write a Java class with public static methods
- Write a TLD file to map the Java class to JSP
- Write a taglib directive to make the function available
- 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
Step 2: Write the TLD File
The TLD file maps the Java class to JSP using the <function> tag with 4 child elements:
Example04
TLD Function Elements
| Element | Purpose |
|---|---|
<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
Execution Flow
When the JSP engine encounters an EL function call:
- Finds the taglib directive matching the prefix
- From the taglib, finds the TLD file matching the URI
- From the TLD, finds the Java class and method signature
- 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
📝 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 QuestionsProgress: 0 / 3