How to Create JSP Custom Tag – using BodyTag interface or BodyTagSupport

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 BodyTag interface or BodyTagSupport

Tutorial on How to Create JSP Custom Tag – using BodyTag interface or BodyTagSupport

In Previous articles, i have explained:

Life Cycle of JSP “Tag” interface.

Life Cycle of JSP “BodyTag” interface.

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

we have seen that how to create the custom JSP tag using interface “Tag”, means tags which does not have a body. In this article, I will explain creating custom Tags which have a body. We can manipulate the content of body as we want.

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

So, create a class which implements interface “BodyTag“, in this article also I am not going to use Support classes. As it will not clear the basic idea.

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.BodyContent;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.Tag;

public class DisplayDateBody implements BodyTag {
	private PageContext pc = null;
	private Tag parent = null;
	private BodyContent bd = null;
	private boolean showInUpperCase;

	public boolean isShowInUpperCase() {
		return showInUpperCase;

	public void setShowInUpperCase(boolean showInUpperCase) {
		this.showInUpperCase = showInUpperCase;

	public void doInitBody() throws JspException {


	public void setBodyContent(BodyContent arg0) {
		bd = arg0;


	public int doAfterBody() {
		try {
			String bodyString = bd.getString();
			JspWriter out = bd.getEnclosingWriter();
			SimpleDateFormat frm = new SimpleDateFormat("dd-MMM-yyy EEEE");
				out.print((bodyString + frm.format(new Date())).toUpperCase());
				out.print((bodyString + frm.format(new Date())).toLowerCase());
			bd.clear(); // empty buffer for next evaluation
		} catch (IOException e) {
			System.out.println("Error in BodyContentTag.doAfterBody()" + e.getMessage());
		} // end of catch

		return SKIP_BODY;

	public int doEndTag() throws JspException {
		return EVAL_PAGE;

	public int doStartTag() throws JspException {

	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;


As per discussed in life cycle of BodtTag interface, two extra method is added to evaluate the body content of the tag.
Now create tld file in “WEB-INF/tlds” folder as shown below:

<?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 with option to choose uppercase</info>


Note here that attribute empty used in previous article is not used here, as we have bodycontent here, so it cannot be left blank.

Now create JSP page:

<%@ 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:ShowDateWithCase showInUpperCase="true">
Current Date is

We can also add the loop functionality in our code, what we have to do additionally is:

  1. Add one more attribute in tag library descriptor (tld) file.
  2. Create getter and setter for that attribute in Tag handler class.
  3. Add one more variable counter in tag handler class.
  4. initialize that variable in method doStartTag(), doInitBody() or any other setter of attribute.
  5. in method doAfterBody(), check the condition if counter<loopcounter then return “EVAL_BODY_BUFFERED” else return “SKIP_BODY”.  EVAL_BODY_BUFFERED will tell the page compiler that eval that body again until the “SKIP_BODY” returned by the method.

Please leave your comment for any suggestion or queries.

Related posts