JSP Taglib Directive

In this tutorial you will learn about the JSP Taglib Directive and its application with practical example.

JSP Taglib Directive

The taglib directive is used to define a tag library in which we can defines many tags. The JSP allows us to define custom tag and with the help of Tag Library Descriptor(TLD) we can define our tags.

Table Of Contents

Syntax:

Example: :Let’s get understand it by an example, we need to follow some steps for creation our own tag, see below.

 

#Need to create TagHandler java class: With the help of this class we can perform action at the start and at the end of tag. Check below code.

MyTagHandler.java: This is the java file which I created for TagHandler.

#Need to create TLD(Tag Library Descriptor): Here we will define our tag.

1.step: do right click on your project and then “New>Tag Library Descriptor” like below image.

7

If you are unable to find here click on “Other” and then “Web>Tag Library Descriptor” like below image.

 

9

2.Step:Click on Next and define your tld file name,and then finish it, like below image

10

3.step: Define Tag: After creation tld file we have to define our tag here like i did, please check below code.

customtag.tld:

#Create JSP file: In this file we will use our custom tag.create a jsp file and paste below code there.

Index.jsp:

#Run: So we all set with taglib directive, Simply run this project you will get output like below image.

11

 

In this tutorial we have learn about the JSP Taglib Directive and its application with practical example. I hope you will like this tutorial.