Tutorial: Create JSP custom tag library

A Custom tag is a user defined JSP language element. When a JSP page containing custom tag is translated into a Servlet, the tag is converted to operations on an object called tag handler. The Web container then invokes those operations when the JSP page’s servlet is executed. It speeds up web application development because of code reuse feasibility. Custom tags can access all the objects available in JSP pages. Custom tags can modify the response generated by the calling page. Custom tags can be nested. Custom tag library consists of one or more Java classes called Tag Handlers and an XML tag library descriptor file (tag library). A class which has to be a tag handler needs to implement Tag interface or IterationTag interface or BodyTag interface or it can also extend TagSupport class or BodyTagSupport class. All the classes that support custom tags are present inside a package called javax.servlet.jsp.tagext. Let us create a custom tag which will does substring operation on given input. For this we will first create our Tag Handler class. Create a class called SubstrTagHandler and copy/paste following code in it.
package net.viralpatel.jsp.custom.taglib; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class SubstrTagHandler extends TagSupport { private String input; private int start; private int end; @Override public int doStartTag() throws JspException { try { //Get the writer object for output. JspWriter out = pageContext.getOut(); //Perform substr operation on string. out.println(input.substring(start, end)); } catch (IOException e) { e.printStackTrace(); } return SKIP_BODY; } public String getInput() { return input; } public void setInput(String input) { this.input = input; } public int getStart() { return start; } public void setStart(int start) { this.start = start; } public int getEnd() { return end; } public void setEnd(int end) { this.end = end; } }
Code language: Java (java)
In above code we have created three attributes: input, start and end. These are the inputs that we will get when the custom tag will be invoked from a JSP file. Also note that, these attributes have getter and setter methods which will be used to set the property values. Now we will create the tag descriptor file also called TLD. Create a file called SubstrDescriptor.tld in WEB-INF directory of your web project and copy / paste following content in it.
<?xml version="1.0" encoding="UTF-8"?> <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>substr</shortname> <info>Sample taglib for Substr operation</info> <uri>https://www.viralpatel.net/jsp/taglib/substr</uri> <tag> <name>substring</name> <tagclass>net.viralpatel.jsp.custom.taglib.SubstrTagHandler</tagclass> <info>Substring function.</info> <attribute> <name>input</name> <required>true</required> </attribute> <attribute> <name>start</name> <required>true</required> </attribute> <attribute> <name>end</name> <required>true</required> </attribute> </tag> </taglib>
Code language: HTML, XML (xml)
In above tld file, <tag> is used to define a custom tag. Each new tag will have its own tag handler class which we specify in <tagclass> tag. Also <name> tag in <tag> represents the tag name that we use in jsp file. We have provided the three attributes with this tag: input, start and end. input is the String whose sub string needs to be parsed. start is the start index and end is the end index. We have just created our first Custom Taglib for JSP. Now let us use the custom taglib in a JSP file. For this create index.jsp in your web application (if it exists, modify it and add this code) and add following code in it. <required> true make these attribute mandatory.
<%@taglib prefix="test" uri="/WEB-INF/SubstrDescriptor.tld"%> <html> <head> <title>JSP Custom Taglib example: Substr function</title> </head> <body> SUBSTR(GOODMORNING, 1, 6) is <font color="blue"> <test:substring input="GOODMORNING" start="1" end="6"/> </font> </body> </html>
Code language: HTML, XML (xml)
Following is the screenshot of the output.

View Comments

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

5 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

5 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

5 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

5 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

5 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago