A simple custom tag

In this post, you will learn:

  • How to define a simple custom Tag class?
  • How to define TLD for the simple tag?
  • How to use the simple tag in a JSP?

Defining a simple custom tag:

To demonstrate the life cycle of a custom tag practically, I am creating a simple Tag class, its TLD file and a JSP page. This tag doesn’t do anything fancy, its purpose is just to let you understand how a custom tag works.

Here is the Source code of the application:

First, the SimpleTag class :

package com.techmentro.learningpad;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class SimpleTag extends TagSupport {

	public int doStartTag()
	{
		JspWriter out=pageContext.getOut();
		try
		{
		out.println("It is generated by doStartTag().<br/>");
		}catch(Exception ex)
		{
			System.out.println(ex);
		}
		return EVAL_BODY_INCLUDE;
	}
	public int doEndTag()
	{
		JspWriter out=pageContext.getOut();
		try
		{
		out.println("It is generated by doEndTag().<br/>");
		}catch(Exception ex)
		{
			System.out.println(ex);
		}
		return EVAL_PAGE;
	}
	
}

Second, the myTags.tld file:


<taglib>
<uri>techmentro/tags</uri>

<jsp-version>2.1</jsp-version>
<tlib-version>1.0</tlib-version>

<tag>
<name>simple</name>
<tag-class>com.techmentro.learningpad.SimpleTag</tag-class>
</tag>
</taglib>

Third, the index.jsp:

<%@ taglib uri="techmentro/tags" prefix="tm" %>

It is header.<br/>
<tm:simple>
It is tag body.<br/>
</tm:simple>
It is footer.

To understand the life cycle using this example use the following steps:

1. Deploy the application as it is on a web server and run it. It will produce following output:

It is header.

It is generated by doStartTag().

It is tag body.

It is generated by doEndTag().

It is footer.

The opening and closing tags are replaced by the output of doStartTag() and doEndTag() methods respectively. It is tag body, is written to the output stream because doStartTag() returned EVAL_BODY_INCLUDE and It is footer is written to the stream because doEndTag() returned EVAL_PAGE.

2. Comment the doStartTag() method in the SimpleTag class. Redeploy and run the application, this time it will produce following output:

It is header.

It is generated by doEndTag().

It is footer.

No output is generated for the opening tag because the doStartTag() method of TagSupport which is inherited in our class in this case, doesn’t write anything to the output stream. It is tag body, isn’t also written to the output stream because the inherited doStartTag() method returned SKIP_BODY.

3. Uncomment the doStartTag() method and comment the doEndTag() method in the SimpleTag class. Redeploy and run the application, this time it will produce following output:

It is header.

It is generated by doStartTag().

It is tag body.

It is footer.

No output is generated for the closing tag because the doEndTag() method of TagSupport which is inherited in our class in this case, doesn’t write anything to the output stream. It is footer, is written to the output stream because the inherited doEndTag() method returned EVAL_PAGE.

4. Uncomment the doEndTag() method and change the return value of the doStartTag() & doEndTag() methods to SKIP_BODY & SKIP_PAGE respectively in the SimpleTag class. Redeploy and run the application, this time it will produce following output:

It is header.

It is generated by doStartTag().

It is generated by doEndTag().

It is tag body and It is footer are not written to the output stream because of the return values of the doStartTag() & doEndTag() methods..


Example Source Code

If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

5 thoughts on “A simple custom tag

  1. Sir in the last output showing section please correct the error where you have written “it is footer” actually it should not be in the output.Thanks

  2. Wow what a cool blog you have here! I am impressed. You really put a lot of time and effort into this. I wish I had your creative writing skills, progressive talent and self discipline to produce a blog like you did. Your blog really does deserve an honest compliment.

Leave a Reply

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