Custom tags are user-defined JSP language elements. When a JSP page contains a custom tag that is converted into a servlet, the tag is converted into operations on an object called a tag handler, which is what the Web container calls when the servlet is executed.
The JSP tag extension allows you to create new tags and insert them directly into a JSP page. Simple Tag Handlers were introduced in the JSP 2.0 specification to write these custom tags.
You can inherit the SimpleTagSupport class and override the doTag() method to develop the simplest custom tag.
Next, we want to create a custom tag called <ex:Hello> with the following format:
<ex:Hello />
To create custom JSP tags, you must first create the Java class that handles the tags. So, let us 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 obtain the current JspContext object and passes "Hello Custom Tag!" to the JspWriter object.
Compile the above class and copy it to the environment variable CLASSPATH directory. Finally, create the following tag library: <Tomcat installation directory> webappsROOTWEB-INFcustom.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!
You can include message content in tags just like the standard tag library. If we want to include content in our customized 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 tag in JSP as shown below:
<%@ 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
You can set various attributes in custom standards. To receive attributes, the value custom tag class must implement the setter method. The setter method in JavaBean is as follows:
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) { /* Use the message from the attribute */ JspWriter out = getJspContext().getOut(); out.println( message ); } else { /* Use the message from the content body */ getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); } }}
The name of the property is "message", so the setter method is setMessage(). Now let us add this attribute in the <attribute> 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 attribute 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 output result of the above example data is:
This is custom tag
You can also include the following attributes:
property | describe |
---|---|
name | Defines the name of the attribute. The attribute name must be unique for each tag. |
required | Specifies whether the attribute is required or optional. If set to false, it is optional. |
rtexprvalue | Declares whether the label attribute is valid when running the expression. |
type | The Java class type that defines this property. Default specified as String |
description | Description information |
fragment | If this attribute is declared, the attribute value will be treated as a JspFragment . |
The following is an example of specifying relevant properties:
..... <attribute> <name>attribute_name</name> <required>false</required> <type>java.util.Date</type> <fragment>false</fragment> </attribute>.. ...
If you are using both attributes, 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>.....