Thursday, September 16, 2021

How to create a custom tag in JSP? Steps and Example

You can create a custom tag in JSP either by implementing the SimpleTag interface or extending SimpleTagSupport class. Custom tags are introduced in JSP 2.0 in an effort to minimize Java code from JSP to keep them maintainable and allow page authors to work more in HTML or XML, like environment than writing Java codes. SimpleTag and SimpleTagSupport allow you to create a custom tag in JSP. It's easier to start with SimpleTagSupport class because it implements all methods of the SimpleTagSupport interface and if you are writing a basic tag then you just need to override the doTag() method of this class. 


The code you put inside the doTag() is used to generate HTML content for the tag. In order to tell the system and user how to use your custom tag, you also need to define a .tld file, known as tag library descriptor, and you must put all your tld files inside the WEB-INF directory and specify their location in the deployment descriptor, also known as web.xml file. 


1. Steps to create a custom tag in JSP

Here are steps you can follow to create a custom tag in JSP

1. Create a class that extends SimpleTagSupport and provides content for the tag, here is an example of a simple Greeting tag that can greet users when used on an HTML page:

package com.javarevisited;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class GreetingTag extends SimpleTagSupport {
    public void doTag() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();
        out.println("Good Morning!");
    }


2. Create a tld file (tld stands for tag library descriptor), here is an example TLD file for our Greeting tag:

<taglib>
   <tlib-version>1.0</tlib-version>
   <jsp-version>2.0</jsp-version>
   <short-name>JSP Custom Tag TLD Example</short-name>
   
   <tag>
      <name>Greeting</name>
      <tag-class>com.javarevisited.GreetingTag</tag-class>
      <body-content>empty</body-content>
   </tag>
</taglib>

3. import the newly created tag in JSP using the taglib directive, for example 

<%@ taglib prefix="display" uri="http://displaytag.sf.net" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

In the first example, we have imported the display tag and provided a prefix that you can use later to use the display tag methods and the second is an example of using the core JSTL tag in a JSP page.

Here is one more example for our Greeting tag:

<%@ taglib prefix = "util" uri = "WEB-INF/greeting.tld"%>

3) use the tag in the JSP file, for example

<display:table name="sessionScope.players" pagesize="5">
     <display:column property="name" title="Name"/>
</display:table>

or

<html>
   <head>
      <title>Example of Custom tag in JSP</title>
   </head>
   
   <body>
      <util:Greeting/>
   </body>
</html>

That's all about how to create custom tags in JSP. Next, we will learn about how to create custom tags with a body in JSP. If you want to learn more about custom tags, you can also join these best Servlet and JSP Courses to start with. 


How to create a custom tag in JSP? Steps




2. Custom tag with Body in JSP

In order to create a custom tag that can accept parameters in its body, you can use JspFragment class. JDP developer uses tag body to pass information to tag handler in order to customize the behavior of custom tag at runtime.

For example, if your tag is supposed to display text then you can pass style information using parameters in the body tag. You also need to declare in your TLD file that your tag can accept attributes in the body, whether they are mandatory or not, and whether you can pass expression language or not. 


That's all about how to create custom tags in JSP and how to use them. We have learned to create both custom tags without the body and custom tags with the body. Most of the general-purpose tags are already declared in the JSTL core library and you should be using them instead of creating your own. You will also find several open-source tag libraries for many functionalities like display tag provides pagination.


Other Servlet and JSP Tutorials you may like to explore

Thanks for reading this article so far. If you find this article useful then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.