JSP Custom tags

Custom tags are known as user-defined tags. When a JSP page containing a custom tag is executed, the tag is translated into a servlet and the tag operations are performed on object of a Tag Handler.

In order to create a custom tag we need to know the below three highlighters.

  • Tag Handler : The Tag Handler class contains the opertations which needs to be performed when a JSP page with custom tag is executed.
  • TLD File : The TLD is a descriptor file which contains the details about our tag such as tag name, tag class and attributes.
  • JSP Page : A Page where we will be using our custom tag.

In this example we will learn how to create a custom tag and print “Custom Tag Example!!!” when our custom tag is executed.

Tag Handler Class (MyCustomTag.java)

Our MyCustomTag class act as a Tag Handler Class which extends the SimpleTagSupport class and we need to override doTag() method where we need to place the code which need to executed when our custom tag is encountered.

package com.javainterviewpoint;

import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class MyCustomTag extends SimpleTagSupport
{
    @Override
    public void doTag() throws IOException
    {
        JspWriter out = getJspContext().getOut();
        out.print("<h3>Custom Tag Example!!!</h3>"); 
    } 
}

In the above code we will be getting the current JspContext using the getJspContext() method and using it we write our custom message “Custom Tag Example!!!”

TLD File (custom.tld)

Our TLD file should be placed in the location “<<Project Name>>/WebContent/WEB-INF/custom.tld” and our file should always end with .tld extension

Important tags in our custom.tld are

<name> : Our Custom tag name (mytag) which we will be using in our jsp page.
<tag-class> : we need to give our fully qualified path of Tag Handler class including the package. Our Tag Handler class is MyCustomTag class whose full path needs to be given.
<body-content> : We will discuss about this later, as of now mention it as empty.

 <taglib>
     <tlib-version>1.0</tlib-version>
     <jsp-version>2.0</jsp-version>
     <short-name>My Custom Tag</short-name>
     <tag>
         <name>mytag</name>
         <tag-class>com.javainterviewpoint.MyCustomTag</tag-class>
         <body-content>empty</body-content>
     </tag>
</taglib>

JSP Page

We have created our custom tag mytag which we will be using here. Using the taglib directive we need to add the uri of our tld file and prefix to call our tag. So now we can call our custom tag by calling <my:mytag>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<[email protected] uri="/WEB-INF/custom.tld" prefix="my" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <my:mytag/>
    </body>
</html>

Output
Once we run the above jsp page we will get the below output

Custom Tag Example!!!

Leave a Reply

Your email address will not be published. Required fields are marked *