Custom Tags in JSP with Example

Custom tags are user-defined tags. By using Custom tags we can get the following advantages:
1. We can reduce the java code into a JSP page.
2. We can provide security in JSP.
3. We can get the logic reusability.
4. By using the custom tag we can separate the business logic from the JSP page.
To creating any Custom Tag we need three things:
1. The Tag Handler Class.
2. The Tag Library Descriptor.
3. A JSP page.

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.
i) TagSupport Class
ii) 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.
i) doStartTag()
ii) doEndTag()
Custom Tags

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.
Custom Tags

3. A JSP page: 

A JSP page is a page where we will be using our custom tag. In the JSP page to define some prefix 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.
Custom Tags

You may also like:
Custom Tags in JSP with Example Custom Tags in JSP with Example Reviewed by Prashant Srivastava on December 22, 2018 Rating: 5

No comments:

Powered by Blogger.