Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to create a custom Function for JSTL

    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 18.5k
    Comment on it

    JSTL Custom Function : In this article I am going to teach you how to create custom JSTL function. We had used many JSTL functions which are already in the lib.But if you want to create a custom function for JSTL you need to follow some easy steps.

    Step 1: Create tag library descriptor file which defines the configuration of custom function, like it's class and the function which is open to use. Create the .tld file inside the 'WEB-INF/tld/ ' directory .


    <?xml version="1.0" encoding="UTF-8" ?>
    <taglib version="2.0" xmlns="" 
      xsi:schemaLocation=" web-jsptaglibrary_2_0.xsd">
        <display-name>Custom EL Functions</display-name>    
                String getSum(int, int)

    Step 2 : Create an entry in web.xml for this tld file information:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">

    Step 3 : Create a class file which should match as described in function-class tag in tld file. Note: Method signature must include static identifier.

    package com.evon.example;
    public class CustomELFunction {
        public static String getSum(int x, int y){
            int sum = x+y;
            return sum;

    Step 4 : Finally we need to create a jsp file, where were going to call custom function using jstl. First we need to define the object of that taglib using jsp directives <%@ taglib %>. Following the jsp which calling a custom JSTL method using Expression Language.

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/WEB-INF/tld/customTagLibrary" prefix="cg" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Custom Function </title>
    ${cg:getSum(25+30) }

 2 Comment(s)

  • Can I add many functions to my library ? I mean, do I need to follow these steps every time I create a new Function ? I would like to do this for example:

        <function-signature>newFunction(String str)</function-signature>
Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: