Creating a custom tag

Creating a custom tag

In a JSP page we can create a custom tag by simply adding the following components in the page:

  1. We need the tag handler class. The tag handler class is used to extend SimpleTagSupport.
  2. The second thing that we need to add custom tags in JSP page is the TLD that is tag library descriptor.
  3. Then we can use the custom tag in our JSP file.

Tag Handler class:

A tag handler class can be created by using the following two different ways:

  1. There are three tags that can be used for invoking the methods during the life cycle of the tag, these methods are as follows: SimpleTag, tag and the body tag.
  2. A tag handler can also be created when we extend the abstract based class and which is capable of implemented the SimpleTag, tag, and the body tag interfaces. The interfaces of the Simpletag, tag and the bodyTag can be implemented by using the SimpleTagSupport, TagSupport and the bidyTagSupport classes. When we extend these classes, we are able to implement the methods of interface by using the tag handler class.

Tag Library Descriptor:

TLD stands for tag library descriptor. Tag library descriptor is considered as an XML document and this XML document provides the information for all libraries and also provides the information for all the tag that are existed in the library. The development tools of the JSP page use the tag library descriptor and the web container of JSP also uses tag library descriptor for the validation of the tags.

The file of the tag library descriptor has an extension of .tld. The file of the tag library descriptor should be packaged in the subdirectory of the WAR file or the subdirectory of the JAR file. We can also package the tag library descriptor in the directory of /WEB/INF or the /META/INF.

Example of custom tag:

Consider the following example in which a tag handler class is created. The tag handler class further extends the TagSupport class. When the tag handler class is extended the method doStartTag () is overridden. This class has also two other methods that include doEndtag () method and the release () method.


Matching. java:

package com. JSPTutorials. taghandler;

import java. io. IOException;

import javax. servlet. jsp.*;

import org. apache. commons. lang. StringUtils;

public class Matching extends TagSupport {

  private String instring;

  private String searchstring;

  public String getInstring () {

    return instring;


  public void setInstring(String instring) {

    this. instring = instring;


  public String getsearchstring() {

    return searchstring;


  public void setsearchstring (String searchstring) {

    this. searchstring = searchstring;


  public int doStartTag () throws JspException {

    try {

      JspWriter out = pageContext. getOut ();

      out. println (StringUtils. Matching (instring, searchstring));


    catch (IOException x) {

  1. printStackTrace ();


    return SKIP_BODY;



In the above example, the implementation of the doStartTag () is important when the tag handler class is implemented. The two variables that are instring and searchstring are used as the attributes of the custom tag. The values of these variables will be set by using the setter and getter methods. These values of the variables will be provided when the custom tag will be used. Consider the following code which is the TLD file of the above java code:

MatchingDescriptor. tld:

<?xml version=”1.0″ encoding=”UTF-8″?>




<shortname>match </shortname>

<info> Example of taglib for the sub string operations </info>


<name>Matching </name>

<tagclass>com. taghandler. Matching </tagclass>

<info> String Manipulation </info>


<name>instring </name>









It can be seen that the above code is the XML document and is saved by using the .tld extension. In the above example the taglib tag is used to specify the version of the JSP and all those tags that are in the library. In the example we have the tag element which is used to represent the custom tags in the library. These tags must have a tag handler class with them.

It should be noted that we have used attribute tags or elements. These attribute elements are used to represent that the tag contains two attributes and the value that is provided inside the required tag that is true is used to show that the attributes are must for the tags to work properly.