Access body of Custom Tag in JSP

In my previous article we have learnt how to create custom tags in jsp and how to use it. In this tutorial we will learn how to access the body of the custom tags (i.e) text which is given between our custom tags

<my:mytag>Body of Custom Tag</my:mytag>

Again we need to have all the below three things

  • 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.

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 java.io.StringWriter;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class MyCustomTag extends SimpleTagSupport 
{
    StringWriter sw = new StringWriter();
    
    @Override
    public void doTag()throws IOException,JspException
    {
        getJspBody().invoke(sw);
        JspWriter out = getJspContext().getOut();
        out.print("<h3>"+sw.toString()+" Text Appended!!!</h3>");
    }
}

In the above code we are getting the content between our tag using “getJspBody().invoke(sw)” which we will be appending to the current JspContext along with the text “Text Appended!!!”.

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

 <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>scriptless</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> and the text body is added between the custom tag.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@taglib 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 of Custom Tag</my:mytag>
    </body>
</html>

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

Body of Custom Tag Text Appended!!!

Leave a Reply

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