beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to access body of Custom tags in JSP tutorial

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

Enjoyed this post? Try these related posts

  1. Difference between include directive and include tag in JSP
  2. JSP Directives – Page, Include and TagLib
  3. JSP Custom tags with example – JSP Tutorial
  4. JSP include directive with parameters example
  5. JSP include action tag – JSP Tutorial
  6. How to run JSP in Eclipse IDE using Apache Tomcat Server

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
  • Declaration tag
  • Expression tag
  • Scriptlets
  • Directives
  • Include Directive
  • Param Directive
  • Exception handling
  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty
  • Implicit Objects
  • Session implicit object
  • Validate session
  • Request implicit object
  • Response implicit object
  • Out implicit object
  • Application implicit object
  • Config implicit object
  • pageContext implicit object
  • Exception implicit object
  • Expression language
  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap