Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSP custom labels


May 12, 2021 JSP


Table of contents


JSP custom label

Custom labels are user-defined JSP language elements. When a JSP page contains a custom label, it is converted to a servlet, which translates into an action on an object called tag handler, that is, when the servlet executes, the Web container invokes those actions.

The JSP label extension lets you create new labels and insert them directly into a JSP page. The JSP 2.0 specification introduces Simple Handlers to write these custom tags.

You can inherit the SimpleTagSupport class and rewrite the doTag() method to develop the simplest custom tag.


Create a Hello label

Next, we'd like to create a custom tag called "lt;ex:Hello", in the label format:

<ex:Hello />

To create a custom JSP label, you first have to create a Java class that handles the label. So let's create a HelloTag class, as follows:

package com.tutorialspoint;

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

public class HelloTag extends SimpleTagSupport {

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

The following code rewrites the doTag() method, which uses the getJspContext() method to get the current JspContext object and "Hello Custom Tag!" Passed to the JspWriter object.

Compile the above class and copy it into the environment variable CLASSPATH directory. Finally, create the following tag library: .lt; 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>Hello</name>
    <tag-class>com.tutorialspoint.HelloTag</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>

Next, we can use the Hello tag in the JSP file:

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

The output of the above program is:

Hello Custom Tag!

Access the label body

You can include message content in labels like a standard tag library. If we want to include content in our custom Hello, the format is as follows:

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

We can modify the label processing class file, the code is as follows:

package com.tutorialspoint;

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

public class HelloTag extends SimpleTagSupport {

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

}

Next we need to modify the 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>Hello</name>
    <tag-class>com.tutorialspoint.HelloTag</tag-class>
    <body-content>scriptless</body-content>
  </tag>
</taglib>

Now we can use the modified label in JSP, as follows:

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

The output of the above program is as follows:

This is message body

Custom label properties

You can set various properties in a custom standard, and to receive properties, the value custom label class must implement the setter method, as shown in the setter method in JavaBean:

package com.tutorialspoint;

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

public class HelloTag extends SimpleTagSupport {

   private String message;

   public void setMessage(String msg) {
      this.message = msg;
   }

   StringWriter sw = new StringWriter();

   public void doTag()
      throws JspException, IOException
    {
       if (message != null) {
          /* 从属性中使用消息 */
          JspWriter out = getJspContext().getOut();
          out.println( message );
       }
       else {
          /* 从内容体中使用消息 */
          getJspBody().invoke(sw);
          getJspContext().getOut().println(sw.toString());
       }
   }

}

The name of the property is "message", so the setter method is setMessage(). Now let's add this property to the element used in the TLD file:

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

Now we can use the message property in the JSP file, as follows:

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

The above instance data output results are:

This is custom tag

You can also include the following properties:

Property Describe
name Define the name of the property. Each label's property name must be unique.
required Specify whether the property is required or optional, if set to false as optional.
rtexprvalue Declares whether the label property is valid when the expression is run.
type The Java class type that defines the property. The default is specified as String
description Describe the information
fragment If the property is declared, the property value is treated as a JspFragment.

Here are the instances of properties that specify the relevant:

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

If you use two properties, modify the 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>
.....