JSP Custom Tags with Examples

We offer you a brighter future with industry-ready online courses - Start Now!!

This article consists of a detailed description of user-defined tags in JSP, better known as JSP custom tags. Their interfacing, advantages, detailed example has been discussed further in this article. So let’s learn the same.

JSP Custom Tags

JSP Custom Tags

JSP Custom tags are simply called user-defined tags. When a JSP is converted into a servlet, these tags get converted to a class. They are called tag handlers as they do action on an object. These actions get invoked by the web container at the time of execution of servlet.

To create these tags, we need to extend and SimpleTagSupport, map TLD(Tag Library Descriptor), override doTag().

These tags can be created so that the functionality of JSP can be extended. They allow the encapsulation of functionality. This is thus made available to non-expert page authors.

Advantages of Custom Tags in JSP

  1. Portable – Once declared in the Tag Library, these tags can be used anywhere.
  2. Simple – They are simple and convenient to use.
  3. Expressive – It provides a wide range of functionalities, scripting elements to the page authors.
  4. Usable from different scripting languages – This functionality can extend to other scripting languages.
  5. No need for scriptlet tag – Once we create a custom tag, we don’t need scriptlet tag. Though it is a bad approach in JSP.
  6. Reusable – We can use the similar business logic over and over again.
  7. Separation of business logic from JSP – For easy maintenance, Custom tags separate business logic and JSP.

Attributes of JSP Custom Tag

S.No.PropertyUse
1nameThis attribute’s purpose is to define the name of the tag. Each tag must have a unique name.
2requiredThis attribute will specify the requirement of the tag i.e., either required or optional.
3rtexprvalueThis attribute declares the runtime expression for the tag is valid or not.
4typeIt will define the class type of the attribute. By default, it will take it as a string.
5descriptionThis attribute retrieves the description regarding information. 
6fragmentIt specifies that the value this attribute holds will be a JspFragment or not.

Using Properties related to Attributes

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

JSP Tag Interface

  • Tag Handler extends SimpleTagSupport class.
  • This class overrides doTag() method. This method is a part of the parent SimpleTagSupport class.
  • It is a subinterface of the JSPTag interface.
  • There will be a need to map the Tag Library descriptor(TLD) as well.

Fields of JSP Tag interface

There are four fields under custom tags:

1. public static int EVAL_BODY_INCLUDE
This field will evaluate the body content.

2. public static int EVAL_PAGE
This field will evaluate the page contents of JSP after the custom tag.

3. public static int SKIP_BODY
This field will skip the content in the body of that tag.

4. public static int SKIP_PAGE
This field will skip the content of JSP present after the custom tag

Method of JSP Tag Interface

There are two methods that help the interfacing of tags. They are :

  • doTag(): This method will override the contents for the tag.
  • JSPContext() : This tag will take the JSP Context.

JSP Custom Tag API

JSP Custom Tag API

The javax.servlet.jsp.tagext is the package that contains classes and interfaces for JSP custom tag API.
Here the JspTag is the root interface in the Custom Tag hierarchy. Further tags and Iteration tags extend it. Body tag extends the above hierarchy and tag support implements it. Further, the body tag supports extends as well as implements it.

JSP Custom Tag Flow

JSP Custom tag flow is as follows. A better description is in the example below.

JSP Custom Tag Flow

Creating JSP Custom Tag

Here we are creating a custom tag named Customtest with testTag being the class. This class will override the dotage() method.

<ex:Customtest/>
 Class testTag extends SimpleTagSupport{ public void doTag()}

Further, we will map the testTag to Tag Library descriptor. JSP container will itself connect the class file and uri in the TLD file

For Example:

CustomTag.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
  <%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html>
<head>
<title>Custom Tags</title>
</head>
<body>
<ex:MyTag/>
</body>
</html>

Custom.tld

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

MyTag.java ( TagHandler )

package demotest;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
MyTag.java(Tag Handler)
public class MyTag extends SimpleTagSupport{
      	public void doTag() throws JspException,IOException
      	{
               	JspWriter out = getJspContext().getOut();
    out.println("--DataFlair--");
               	out.println("My Custom Tag");
      	}
 
}

Explanation:
Mytag.java which is a tag handler, extends SimpleTagSupport and overrides doTag(). It writes my Custom Tag using JspWriter.
The tag class of the tld has the complete path of the tag handler so that the java file can be mapped to the Tag Library Descriptor.
The JSP file consists of the custom tag we defined.

Output:

jsp custom tags

Conclusion

JSP Custom tags are user-defined tags that extend the functionality of JSP. They are simple and effective to use and efficiently encapsulate the functionalities. They are easy to understand and use, thus they are available to non-expert page authors. In this article, we saw different custom tags, their methods. We understood their interfacing in JSP and their advantages as well. We also glanced upon the Custom Tag API and its working along with the Custom Tag Flow.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

courses

DataFlair Team

The DataFlair Team is passionate about delivering top-notch tutorials and resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With expertise in the tech industry, we simplify complex topics to help learners excel. Stay updated with our latest insights.

Leave a Reply

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