Introduction to JSP custom tags

In this post, you will learn:

  • What is the use of custom tags in JSP?
  • How custom tags are processed?

Introduction to JSP custom tags:

Custom tags are application programmers’ defined actions which represent presentation logic as reusable components. The facility of defining custom tags makes the JSP extensible. To understand the use of custom tags, lets consider the following scenario. Let there be a web application in which 10 pages displays user data in tabular format. These pages can be defined in two ways:

1. In each page HTML <table>,<tr>,<td> tags are used to present data in tabular format.

2. A custom tag is defined to generate table and it is used in all the pages. Data which is to be presented in tabular format is provided to the tag as attribute.

From this scenario, you can judge on you own that the second approach is better. It facilitate re-usability and improves productivity.

Custom tags components:

1. Tag Processor: is a server component provided by the tag API, to facilitate processing of custom tags in an application.

2. Tag class: is an application class which contains the tag processing logic. It is defined using the Tag API of JSP.

3. Tag Library Descriptor: is a XML file created by application programmer. It contains the mapping of custom tags and their handler classes. It is used by the Tag processor to identify the Tag classes of the custom tags.

Following diagram describes the processing of a custom tag.

custom_tags

Description of the diagram:

1.0 – Tag Processor is activated when a custom tag is encountered during the processing of a JSP request.

1.1 – Tag Processor identifies the Tag class for the custom tag using the information of the TLD file.

1.2 – Tag class is instantiated i.e. an object of the Tag class is created by the Tag Processor to get the custom tag processed.

1.3 – Life-cycle methods are invoked on the tag object by the Tag Processor.

1.4 – Life-cycle methods process the custom tag and generate HTML contents which replace the tag in the generated page.


Its time to recap the main concepts:

  1. Custom tags are application programmers’ defined action tags.
  2. They represent presentation logic as reusable component.
  3. JSP provides a Tag API for defining custom tags.
  4. JSP provides a server component by the name Tag Processor for the processing of custom tags.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

One thought on “Introduction to JSP custom tags

Leave a Reply

Your email address will not be published. Required fields are marked *