Updated April 3, 2023
Introduction to Custom Tags in JSP
JSP custom tags are defined as actions prepared by the developers as per their requirements. Custom Tags are also said to be “JSP Actions” or “Custom actions”. To prepare user-defined actions we have to prepare a set of user-defined tags called “Custom Tags”.
Real-Time Example: Let’s suppose we want to use our information with our own tags for security reason, without this custom tags concept we can’t achieve this. So developers come up with an idea to implement user custom tags in JSP.
Syntax:
<prefix_Name:tag_Name [Attribute_List]>//start tag
//Body of the page
</prefix_Name:tag_Name>//end tag
Preparation of Custom Tag in JSP
The preparation of the custom tag needs 3 elements.
1. Tag Handler Class
It creates the Tag Handler class and this class performs the action at the start or at the end of the tag. Create the tag Handler class must extend the TagSupport class and overriding its method doStartTag(). Writing data for the JSP file we must use the JspWriter class.getOut() method provided by the PageContext class and it returns the instance of JspWrite class. pageContext instance by default provided by TagSupport class.
Syntax:
public class HandlerClass extends TagSupport{
public int doStartTag() throws JspException {
JspWriter out=pageContext.getOut();//returns the instance of JspWriter
try{
//provide some business logic }
catch(Exception e){}
} }
2. TLD File
It is abbreviated as Tag Library Descriptor(TLD) for creating user custom tags. The extension should be .tld. This file has to create under WEB-INF directory.
Syntax:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tag>
<name>user defined tag name</name>
<tag-class>full class name of Handler class</tag-class>
</tag>
</taglib>
3. JSP Page with Tag lib Library
This is JSP page used for writing our created custom tags within the .jsp file
Syntax:
<%@ tagliburi="WEB-INF/" prefix="prefix for our custom tag" %>
<prefix_Tag:custom_Tag></prefix_Tag:custom_Tag>
Schematic Diagram of Custom Tags Project
Given below is the schematic diagram of Custom Tags project:
Project Structure of Custom Tags in Eclipse
Given below is the project structure of custom tags in eclipse:
Examples of Custom Tags in JSP
Given below are the examples:
Example #1
Generate Indian Standard for Custom Tag.
Java Code: CustomTagDate.java
package com.custom.tag;
import java.util.Calendar;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
//Custom Handler class
public class CustomTagDate extends TagSupport {
private static final long serialVersionUID = 1L;
public int doStartTag() throws JspException {
//JspWriter instance
JspWriter out = pageContext.getOut();
try {
out.print(Calendar.getInstance().getTime());//printing date and time in JSP page
} catch (Exception e) {
System.out.println(e);
}
return SKIP_BODY;// not evaluate body content of the tag
}
}
TLD Code: TagLibDate.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>simple</short-name>
<uri>http://tomcat.apache.org/example-taglib</uri>
<tag>
<name>mydate</name><!-- here we create a custom tag -->
<tag-class>com.custom.tag.CustomTagDate</tag-class><!-- Load the handler class into tld file for apply the business logic to the tag -->
</tag>
</taglib>
JSP Code: DateCustom.jsp
<%@ tagliburi="WEB-INF/TagLibDate.tld" prefix="india" %><!-- Declaring prefix for user tag -->
MY CURRENT INDIAN STANDARD TIME IS: <india:mydate/>
Output:
Example #2
Generate Name.
Java Code: CustomTagName.java
package com.custom.tag;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
//Custom Handler class
public class CustomTagName extends SimpleTagSupport {
private String name;
public void setName(String name) {
this.name = name;
}
StringWriter stringWriter = new StringWriter();
public void doTag()
throws JspException, IOException {
if (name != null) {
//JspWriter instance
JspWriter out = getJspContext().getOut();
out.println( name );
} else {
//make use of body content
getJspBody().invoke(stringWriter);
getJspContext().getOut().println(stringWriter.toString());
}
}
}
TLD Code: TagLibName.tld
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>TLD Body</short-name>
<tag>
<name>name</name><!-- here we create a custom tag -->
<tag-class>com.custom.tag.CustomTagName</tag-class>
<body-content>scriptless</body-content><!-- Load the handler class into tld file for apply the business logic to the tag -->
<attribute>
<name>name</name>
</attribute>
</tag>
</taglib>
JSP Code: NameCustom.jsp
<%@ taglib prefix = "com" uri = "WEB-INF/TagLibName.tld"%>
<html>
<head>
<title>A sample custom tag</title>
</head>
<body>
<com:name name = "My Name is Paramesh" />
</body>
</html>
Output:
Example #3
Generate Village name with Custom Tags.
Java Code: CustomTagBasicInfo.java
package com.custom.tag;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
//Handler class
public class CustomTagBasicInfo extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
//JspWriter instance for creating custom tag
JspWriter out = getJspContext().getOut();
out.println("I am from Phanigiri village.");
}
}
TLD Code: TagLibBasicInfo.tld
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>My Custom Tag</short-name>
<tag>
<name>information</name>
<tag-class>com.custom.tag.CustomTagBasicInfo</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
JSP Code: BasicInfoCustom.jsp
<%@ taglib prefix="myprefix" uri="WEB-INF/TagLibBasicInfo.tld"%>
<html>
<head>
<title>Custom Tags in JSP Example</title>
</head>
<body>
<myprefix:information/>
</body>
</html>
Output:
Conclusion: Custom tags are used for creating user-defined tags. In JSP user-defined tags can be created by using 3 steps, 1st one is the Java Handler class, 2nd one is the TLD file, and 3rd one is the JSP file.
Recommended Article
This is a guide to Custom Tags in JSP. Here we discuss the Introduction, preparation of custom tag, schematic diagram, project structure of custom tags in eclipse, and examples. You may also have a look at the following articles to learn more –