JSP Tutorial - JSP Custom Tags








JSP page are built from tags and other than the existing tags in the standard tag library we can create custom tag.

A custom tag is a user-defined JSP language element.

We use the Simple Tag Handlers to write the custom tags.

To create a customer tag, extend SimpleTagSupport class and override the doTag() method. Inside the doTag() method, we can add our code to generate content for the tag.

Example

The following code shows how to create a tag to output static string.

We call the new tag My.

After creating the tag, we can use it as follows.

<ex:My />

First, create MyTag.java as follows:

package com.java2s;

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

public class MyTag extends SimpleTagSupport {

  public void doTag() throws JspException, IOException {
    JspWriter out = getJspContext().getOut();
    out.println("My Custom Tag!");
  }
}

Second, create following tag library file as in <Tomcat-Installation-Directory>webapps\ROOT\WEB-INF\custom.tld.

<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>Example TLD</short-name>
  <tag>
    <name>My</name>
    <tag-class>com.java2s.MyTag</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>

Use above defined custom tag My in our JSP program as follows:

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html>
  <head>
    <title>A sample custom tag</title>
  </head>
  <body>
    <ex:My/>
  </body>
</html>




Access the Tag Body

You can include a message in the tag body. The following code shows how to add tag body to a custom tag.

<ex:My>
   This is message body
</ex:My>

The following code shows how to access the body text in Java code.

package com.java2s;

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

public class MyTag extends SimpleTagSupport {

   StringWriter sw = new StringWriter();
   public void doTag() throws JspException, IOException{
       getJspBody().invoke(sw);
       getJspContext().getOut().println(sw.toString());
    }
}

Add the custom tag to the tag declaration TLD file as follows:

<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>Example TLD with Body</short-name>
  <tag>
    <name>My</name>
    <tag-class>com.java2s.MyTag</tag-class>
    <body-content>scriptless</body-content>
  </tag>
</taglib>

Now let us call above tag with proper body as follows:

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html>
  <body>
    <ex:My>
        This is message body
    </ex:My>
  </body>
</html>




Custom Tag Attributes

To use an attribute value, a custom tag class needs to implement setter methods, in the same way as a JavaBean.

package com.java2s;

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

public class MyTag extends SimpleTagSupport {

   private String message;

   public void setMessage(String msg) {
      this.message = msg;
   }
   public void doTag() throws JspException, IOException
    {
       if (message != null) {
          JspWriter out = getJspContext().getOut();
          out.println( message );
       }else{
          getJspContext().getOut().println("no message");
       }
   }

}

Example 2

The following code shows how to add this attribute in TLD file using <attribute> element.

<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>Example TLD with Body</short-name>
  <tag>
    <name>My</name>
    <tag-class>com.java2s.MyTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
       <name>message</name>
    </attribute>
  </tag>
</taglib>

Now let us try following JSP with message attribute as follows:

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html>
  <head>
    <title>A sample custom tag</title>
  </head>
  <body>
    <ex:My message="This is custom tag" />
  </body>
</html>

We can set the following properties for an attribute:

Property Purpose
name defines the name of an attribute. Each attribute name must be unique for a particular tag.
required specifies if this attribute is required. It would be false for optional.
rtexprvalue if a runtime expression value for a tag attribute is valid
type Defines the Java class-type of this attribute. By default it is String.
description Informational description can be provided.
fragment Declares if this attribute value should be treated as a JspFragment.

Example 3

For example, the following code shows how to specify properties related to an attribute:

    ...
    <attribute>
      <name>attribute_name</name>
      <required>false</required>
      <type>java.util.Date</type>
      <fragment>false</fragment>
    </attribute>
    ...

We can set two attributes in TLD file as follows:

.....
    <attribute>
      <name>attribute_name1</name>
      <required>false</required>
      <type>java.util.Boolean</type>
      <fragment>false</fragment>
    </attribute>
    <attribute>
      <name>attribute_name2</name>
      <required>true</required>
      <type>java.util.Date</type>
    </attribute>
.....