JSP Custom Tag Development

In this tutorial, we will learn how to create a simple JSP custom tag and how to use it in JSP. Custom tags are user-defined tags. They eliminate the possibility of a scriptlet tag and separates the business logic from the JSP page.
Before diving into an example, let's discuss why to create custom tags?

Why Custom Tags?

Custom tags provide a means for bridging the gap between the two specialties. Java programmers can provide application functionality in convenient packages that Web designers can use as building blocks. While JavaBeans can also encapsulate code, they are most useful as repositories for attributes. Notions of iteration, nesting, or cooperative actions are difficult to express with beans. Custom tags enable a higher-level application-specific approach to JSP development.
For example, a database query written with custom tags might look like the following,
    <db:connect url="mydatabase">
            SELECT *
            FROM FD_GROUP
            WHERE FdGp_Desc LIKE '%F%'
            ORDER BY FdGp_Cd
  <table border="1">
    <th>Food Group Code</th>
     <td><db:getField name="FdGp_Cd" /></td>
     <td><db:getField name="FdGp_Desc" /></td>
where connect, runQuery, forEachRow, and getField are application-oriented custom tags.
All the logic of the above custom tags could have been written with scriptlets embedded in the JSP page.
Besides the separation of content and presentation, other benefits of custom tags include:
  • Eliminates the need of scriptlet tag The custom tags eliminates the need of scriptlet tag which is considered bad programming approach in JSP.
  • Separation of business logic from JSP The custom tags separate the business logic from the JSP page so that it may be easy to maintain.
  • Re-usability The custom tags makes the possibility to reuse the same business logic again and again.

JSP Custom Tag Example

In this example, we are going to create a custom tag that prints the current date, time and months.

Steps to create a custom tag

  1. Write the Tag Handler
  2. Create the Tag Library Descriptor (TLD) File
  3. Incorporate the Tag into a JSP Page
  4. Output

1. Write the Tag Handler

A tag’s action is implemented in a Java class known as a tag handler. Instances of tag handlers are created and maintained by the JSP container, and predefined methods in these classes are called directly from a JSP page’s generated servlet.
A tag handler class should implement Tag/IterationTag/ BodyTag interface or it can also extend TagSupport/BodyTagSupport/SimpleTagSupport class. All the classes that support custom tags are present inside javax.servlet.jsp.tagext. In the below example, we are extending the class SimpleTagSupport.
Let's create a CustomTag tag handler which will print current date, time and months. Here is the complete source code for the tag handler:
package net.javaguides.jsp.tutorial.jstl;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

 * Simple JSP Custom tag handler
 * @author Ramesh Fadatare
public class CustomTag extends SimpleTagSupport {
    public void doTag() throws IOException {
         * This is just to display a message, when we will use our custom tag. This
         * message would be displayed
        JspWriter out = getJspContext().getOut();
        out.println("Current Date :: " + LocalDate.now());
        out.println("Current Time :: " + LocalTime.now());
        out.println("Print Months");
        for (Month month: Month.values()) {

2. Create the Tag Library Descriptor (TLD) File

Tag Library Descriptor (TLD) file contains information of tag and tag handler classes. It must be contained inside the WEB-INF directory. If you are new to TLD then check out What is TLD with Examples article.
Here is the complete source code of the message.tld file:
 <short-name>My Custom Tag</short-name>

3. Incorporate the Tag into a JSP Page

At this point, the tag is ready to be used. The following JSP page (customtag.jsp) demonstrates how this is done:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

<%@ taglib prefix="myprefix" uri="/WEB-INF/message.tld"%>
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>JSP Custom Tag Example</title>
 <h3>Basic Example of a Custom Tag </h3>
 <myprefix:CustomTag />

The taglib Directive

The second line contains the taglib directive: <%@ taglib prefix="myprefix" uri="../WEB-INF/message.tld"%> This directive must appear in the JSP page before any of the custom tags it refers to are used. The top of the page is a good place.

How to Use the Tag in the JSP Page

The rest of the Web page is traditional HTML, with the exception of the line on which the custom tag is specified:
<myprefix:CustomTag />
When customtag.jsp is first invoked, the JSP container uses information from the taglib directive to locate the tag library descriptor and to identify where its tags are used on this page.

4. Running application

Let's deploy customtag.jsp in tomcat server and hit respective URL in the browser will display this page on screen: