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

JSP Custom tags with example – JSP Tutorial

By Chaitanya Singh | Filed Under: JSP tutorial

User-defined tags are known as custom tags. In this tutorial we will see how to create a custom tag and use it in JSP.

To create a custom tag we need three things:
1) Tag handler class: In this class we specify what our custom tag will do when it is used in a JSP page.
2) TLD file: Tag descriptor file where we will specify our tag name, tag handler class and tag attributes.
3) JSP page: A JSP page where we will be using our custom tag.

Example:
In the below example we are creating a custom tag MyMsg which will display the message “This is my own custom tag” when used in a JSP page.

Tag handler class:
A tag handler class should implement Tag/IterationTag/ BodyTag interface or it can also extend TagSupport/BodyTagSupport/SimpleTagSupport class. All the classes that support custom tags are present inside javax.servlet.jsp.tagext. In the below we are extending the class SimpleTagSupport.

Details.java

package beginnersbook.com;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class Details extends SimpleTagSupport {
   public void doTag() throws JspException, IOException {
      /*This is just to display a message, when
       * we will use our custom tag. This message
       * would be displayed
       */
      JspWriter out = getJspContext().getOut();
      out.println("This is my own custom tag");
   }
}

TLD File
This file should present at the location: Project Name/WebContent/WEB-INF/ and it should have a .tld extension.

Note:
<name> tag: custom tag name. In this example we have given it as MyMsg
<tag-class> tag: Fully qualified class name. Our tag handler class Details.java is in package beginnersbook.com so we have given the value as beginnersbook.com.Details.

message.tld

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

Using custom tag in JSP:
Above we have created a custom tag named MyMsg. Here we will be using it.
Note: taglib directive should have the TLD file path in uri field. Above we have created the message.tld file so we have given the path of that file.
Choose any prefix and specify it in taglib directive’s prefix field. Here we have specified it as myprefix.
Custom tag is called like this: <prefix:tagName/>. Our prefix is myprefix and tag name is MyMsg so we have called it as <myprefix:MyMsg/> in the below JSP page.

<%@ taglib prefix="myprefix" uri="WEB-INF/message.tld"%>
<html>
 <head>
    <title>Custom Tags in JSP Example</title>
 </head>
 <body>
    <myprefix:MyMsg/>
 </body>
</html>

Output:

This is my own custom tag

Next tutorial: How to access the body of custom tags

Comments

  1. PRAVEEN says

    May 4, 2015 at 1:16 PM

    SIR WE Want more examples on custom tags….

    Reply
  2. parveen says

    May 6, 2015 at 12:38 PM

    sir….we want more examples on custom tags….

    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 – 2022 BeginnersBook . Privacy Policy . Sitemap