BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to access body of Custom tags in JSP tutorial

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: JSP tutorial

In the last tutorial we learnt how to create and use custom tags in JSP. In this tutorial we will see how to access the body of custom tag. For e.g. If our custom tag is xyz then we would learn to access the content between <prefix: xyz> and </prefix:xyz>

<prefix: xyz>
Body of custom tag: This is what we will access in the below example
</prefix:xyz>

Example:
In this example or custom tag will append a String to its own body and will display the result.

Tag handler class: Details.java

package beginnersbook.com;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class Details extends SimpleTagSupport {
   //StringWriter object
   StringWriter sw = new StringWriter();

   public void doTag() throws JspException, IOException
   {
       getJspBody().invoke(sw);
       JspWriter out = getJspContext().getOut();
       out.println(sw.toString()+"Appended Custom Tag Message");
   }
}

TLD file: message.tld
Remember to have this file in WEB-INF folder.

<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>My Custom Tag: MyMsg</short-name>
<tag>
<name>MyMsg</name>
<tag-class>beginnersbook.com.Details</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>

JSP Page: index.jsp

<%@ taglib prefix="myprefix" uri="WEB-INF/message.tld"%>
<html>
<head>
  <title>Accessing Custom Tag Body Example</title>
</head>
<body>
  <myprefix:MyMsg>
    Test String
  </myprefix:MyMsg>
</body>
</html>

Output:

Test String Appended Custom Tag Message

Top Related Articles:

  1. JSP Scriptlet Tag (Scripting elements)
  2. Java Server Pages (JSP) Life Cycle
  3. Include Directive in JSP
  4. JSP Directives – Page, Include and TagLib
  5. JSP Expression Tag

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. sanjay says

    March 31, 2015 at 11:01 AM

    it is very helpful site for beginner’s …..

    thank u so much

    Reply
  2. Christian Fiegehen says

    March 10, 2017 at 2:15 PM

    Excellent.
    Thank you very much for your clear explanation.
    With the right detail to make things work.
    Better than many articles that live in limbo without explaining anything.

    Reply

Leave a Reply Cancel reply

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

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE

Scripting Elements

  • Scriptlet Tag
  • Expression tag
  • Declaration tag

Implicit Objects

  • Implicit Objects
  • JSP Request
  • JSP Response
  • JSP Config
  • JSP Application
  • JSP Session
  • JSP Out
  • JSP pageContext
  • JSP Exception
  • Validate session

JSP directives

  • JSP Directives
  • Include Directive

JSP Exception

  • Exception handling

JSP Action

  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty

Expression language

  • Expression language

JSP Custom Tags

  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap