JSP custom tag examples: In this tutorial, you will be going to review & learn completely about the Custom Tags in JSP. A custom tag is a user-defined JSP language element. Stay tuned to this page and collect full-fledged information on JSP Custom Tags like definition, syntax, benefits, and how to create any custom tag in JSP with Examples.
- Custom Tags in JSP
- Syntax of JSP Custom Tag
- Advantages of Custom Tags
- JSP Custom Tag API
- How to Create a JSP Custom Tags?
Custom Tags in JSP
JSP customtags: In JSP, the user-defined tags are called Custom Tags. They reduce the possibility of a scriptlet tag and depart the business logic from the JSP page.
Syntax of JSP Custom Tag
The syntax to use custom tag are in two ways. They are as follows:
(i)
<prefix:tagname attr1=value1....attrn=valuen />
(ii)
<prefix:tagname attr1=value1....attrn=valuen > body code </prefix:tagname>
Advantages of Custom Tags
By applying the Custom tags we can notice the following advantages:
- We can reduce the java code into a JSP page.
- Eliminates the need of scriptlet tag
- We can provide security in JSP.
- It can help in the separation of business logic from JSP
- We can get the logic reusability.
JSP Custom Tag API
The javax.servlet.jsp.tagext package includes classes and interfaces for JSP custom tag API. In the Custom Tag hierarchy, the JspTag is the root interface. Here is the hierarchy of the JSP custom tag:
Do Refer:
How to Create a JSP Custom Tags?
To create any Custom Tag, we need to do the three following things:
- Create the Tag handler class
- Create the Tag Library Descriptor (TLD) file
- Create the JSP file that uses the Custom tag defined in the TLD file
1. The Tag Handler Class
The Tag Handler class is a java class in which the custom tag logic is defined. For every custom tag, there should be an associated tag handler class. The number of custom tags in a JSP page is equal to the number of tag handler classes. To create a tag handler class our java class should extend one of the following two classes.
- TagSupport Class
- BodyTagSupport class
These two classes are available inside the javax.servlet.jsp.tagext. package. if we want to define the logic for a custom tag start and custom tag end then we override the following two methods of the Tag Support class.
- doStartTag()
- doEndTag()
2. The Tag Library Descriptor
A TLD file is like an XML file in which custom tags are configured. The main purpose of the TLD file is to connect the Tag handler class with a tag name that will appear in the JSP document. It also gives more information to the JSP container about the tags it describes. It describes the tag attributes that can be used with the tag and tells whether they’re required or are optional.
3. Create the JSP file
A JSP page is a page where we will be using our custom tag. On the JSP page to define some prefixes for our custom tags, we use taglib directive in JSP. Using taglib directive we also define a unique identification name for the tag started with the same prefix. To add a prefix and a unique identification name we use two attributes URI and prefix.