Home arrow Java arrow JSP Custom Tags: Bringing Components to the Web
JAVA

JSP Custom Tags: Bringing Components to the Web


This article explains what a JSP custom tag is, how to create one, and why you should use them in your web development projects.

Author Info:
By: A.P.Rajshekhar
Rating: 5 stars5 stars5 stars5 stars5 stars / 17
June 13, 2006
TABLE OF CONTENTS:
  1. · JSP Custom Tags: Bringing Components to the Web
  2. · Creating a Custom Tag, Step By Step
  3. · Creating a Custom Tag continued
  4. · Custom Tags in the Real World

print this article
SEARCH DEVARTICLES

JSP Custom Tags: Bringing Components to the Web
(Page 1 of 4 )

The DRY or "Don't Repeat Yourself" philosophy is the source that powers the development of components. In the context of web development, especially server-side web-tier development, the application of the DRY philosophy and thus implementation of components have reduced the development time of server-side application modules. Each technology has its own technique for implementing server-side web-tier components.

In J2EE, the technique is creating custom tags for JSP, as JSP represents the server-side web tier. In this discussion, I will be focusing on how to implement custom tags in JSP. The first section would deal with the fundamentals of custom tags and the terminology associated with them. In the second section, the focus will be on the steps needed to implement a custom tag. In the third and last section, I will be implementing a custom tag that will create a table with a specified number of rows and columns. That's the outline for this discussion.

JSP Custom Tag: What is it?

JSP was brought to the fore to provide a clear separation between the presentation and the presentation logic. To take the separation to the next level and thus decrease the use of scriptlets, the concept of a custom tag was introduced in JSP version 1.1. When using custom tags, the application specific or general logic can be encapsulated and presented to the page developer/designer as friendly HTML/XML tags. In a nutshell, any presentation logic can be implemented as a library and used just like any other HTML tag, when the logic is implemented using a custom tag. Basically custom tags provide the following functions: 

  1. The ability to customize the generated page by using attributes either statically or  dynamically at runtime.
  2. The ability to modify the response generated by the page embedding the tag. 
  3. The ability to encapsulate both simple and complex  presentation logic in an easy to use and understand syntax.

Of these, the first two functions are provided by every type of custom tag. However, the last functionality has in fact two services: first, the encapsulation of simple logic and second, the encapsulation of complex logic. To make this distinction more generic, let's say that simple logic is like HTML tags that don't support the nesting of tags. An example of this is the <br/> tag.

Complex tags, on the other hand, are like HTML tags that support nesting. Think of the HTML <table> tag, for example.  Also nesting provides the ability to parse the contents present within the opening and closing tags. On the basis of the aforementioned aspects, custom tags can be divided into three types: basic tags, iteration tags, and  complex tags. 

On the implementation level, the type of tag is defined by the interface the tag implements. All the tags implement the required interface from the javax.servlet.jsp.tagext package. The difference is in which interface they implement. The basic tags may or may not parse the content within the opening and closing tags.

  1. Basic Tags: Basic tags implement the Tag interface. The implementation must generate the page content. The content included is executed if required. This means that it is not mandatory to execute the included content if it is not required. 
  2. Iteration Tags: These tags implement the IterationTag interface. The main difference between the iteration and basic tag (apart from the interface they implement) is that the iteration tag must repeat the execution of the body content. In essence, the iteration tag implements either for, while or do ...while logic. 
  3. Complex Tags: These tags implement the BodyTag interface. A complex tag must provide one more service in addition to the services provided by the basic and iteration tags. It should execute the content generated by the body of the tag. Also, just like the iteration tag, it should repeat execution of the body content.

Though all these interfaces have been provided, most of the time all that needs doing is extending TagSupport and TagBodySupport. The reason is that both of these together have provided the default implementation of all the above mentioned interfaces.

That covers the types of tags. The next obvious step is to explain the components that form the  basis of JSP custom tags. There are two main components of a custom tag: the TLD file and the tag handler. The TLD file is an XML file and the Tag Handler is a Java class. 

The Tag Library Descriptor (TLD) file is an XML file that describes a tag. If a collection of tags exists in the form of a library (also called a tag library), a single TLD file can describe the whole lot. In essence, the TLD file is used by a JSP container to map and validate the tag and its attributes to the handler class. The TLD file contains information such as tag name, tag class, and so forth, which are represented as XML tags.

The tag handler contains the logic to generate the content corresponding to the tag. Even the simplest tag should override the doStartTag() and doEndTag() methods. If the tag has to support attributes, then getters and setters for the corresponding attributes have to be provided. In short, the tag handler is class that encapsulates and handles all of the functionality provided by the Tag.

That concludes the brief overview of the terminologies that recur within the vocabulary of JSP custom tags. The next section will cover the steps involved in creating a custom tag.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials