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

Author posted by Jitendra on Posted on under category Categories JAVA, JSP and tagged as Tags with 2 Comments on How to Create JSP Custom Tag – using Tag interface or TagSupport

Tutorial of creating JSP Custom Tag – using Tag interface or TagSupport in JAVA

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 :
</ct:showTime>

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.io.IOException;
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;
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	@Override
	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;
	}

	@Override
	public Tag getParent() {

		return parent;
	}

	@Override
	public void release() {
		pc = null;
		parent = null;
	}

	@Override
	public void setPageContext(PageContext arg0) {
		pc = arg0;
	}

	@Override
	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"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
	<tlibversion>1.0.0</tlibversion>
	<jspversion>1.1</jspversion>
	<shortname>CustomTag</shortname>
	<uri>http://JitendraZaa.com/blog</uri>
	<info>Custom tags</info>
	<tag>
		<name>ShowDate</name>
		<tagclass>com.G2.CustomTag.DisplayDate</tagclass>
		<bodycontent>empty</bodycontent>
		<info>Custom tag used to display the current date</info>
		<attribute>
			<name>displayDate</name>
			<required>true</required>
		</attribute>
	</tag>
</taglib>

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"
	pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/tlds/CustomTags.tld" prefix="ct"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Custom Tag</title>
</head>
<body>
<h2>Custom tag to display date</h2>
<ct:ShowDate displayDate="true" />
</body>
</html>
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

Related posts