How to Create JSP Custom Tag – using Tag interface or TagSupport

In Previous two articles, i have explained:

Life Cycle of JSP “Tag” interface.

Life Cycle of JSP “BodyTag” interface.

This article is about creating custom Tag in JSP using “Tag” interface.
Creating custom tag in JSP has its unique advantages. This is same as Custom controls of the ASP.Net.
By creating the Custom tag, we can resuse that tag every where in our application. Best example of custom tags are the Struts.

Types of Tag:
Tag without body:

<ct:showTime displayTime="false" />

Tag with body:

<ct:showTime displayTime="false" >
Current Time is :

As ther are two types of tag, there are two way of creating them in JSP:

  • By implementing below interfaces:
    1. Tag
    2. BodyTag
  • Or by extending any of the two below class (provides default implementation of interfaces discussed above):
    1. TagSupport (internally implements Tag)
    2. BodyTagSupport (internally implements BodyTag)

In this article, I will use the interface approach, so that the beginners can get broader idea about how Tag library actually implemented.

How to Create Custom Tag in JSP
How to Create Custom Tag in JSP

Step 1: Write Tag Handler Class

Create a class which implements interface Tag.

package com.G2.CustomTag;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class DisplayDate implements Tag {

	private PageContext pc = null;
	private Tag parent = null;
	private boolean displayDate;

	public boolean isDisplayDate() {
		return displayDate;

	public void setDisplayDate(boolean displayDate) {
		this.displayDate = displayDate;

	public int doEndTag() throws JspException {
		return EVAL_PAGE;

	public int doStartTag() throws JspException {
		try {
			JspWriter out = pc.getOut();
			if (isDisplayDate()) {
				SimpleDateFormat frm = new SimpleDateFormat("dd-MMM-yyy EEEE");
				out.print(frm.format(new Date()));
			} else {
				out.print("Custom Tag is not configured to display Date");
		} catch (IOException e) {
			throw new JspException(e);
		return SKIP_BODY;

	public Tag getParent() {

		return parent;

	public void release() {
		pc = null;
		parent = null;

	public void setPageContext(PageContext arg0) {
		pc = arg0;

	public void setParent(Tag arg0) {
		parent = arg0;


In above class, you can see that I have created two objects of Tag and PageContext. getParent() and setParent() method uses the object of Tag to specify that who is the parent of that custom Tag. setPageContext() is used to set the PageContext for out custom Tag.
Tag Attributes must have setters and getters, in above class displayDate is the attribute.
doStartTag(), this is the method where we have to write our code. If the attributes value is true then it displays the date other wise displays the message.
release(), this method is responsible to clear all the resources used by this custom tag. To know more about the life cycle of custom tag using Tag interface, please read this article.

Step 2 : create Tag Library Descriptor (TLD) File

Now create xml file named “CustomTags.tld” in folder “WEB-INF/tlds“ and below code lines:

We can also make Tag Handler class and tld file accessible by creating a jar and placing in lib folder.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
	<info>Custom tags</info>
		<info>Custom tag used to display the current date</info>

tlibversion – Your tag library version
jspversion – jsp version
shortname – Refers to the name of the JSP Custom Tag that your jsp code will be referring to
name – Refers to the name the Custom Tags
tagclass – Refersn to the class structure used for your JSP Tags
bodycontent – This is used in case we are using BodyTagSupport, in TagSupport we keep this empty.

Now create “index.jsp” with following lines of code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@taglib uri="/WEB-INF/tlds/CustomTags.tld" prefix="ct"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Custom Tag</title>
<h2>Custom tag to display date</h2>
<ct:ShowDate displayDate="true" />
output of the custom tag created using Tag interface
output of the custom tag created using Tag interface

Next article : Create Custom tag using BodyTag interface


Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.