Custom Tag Tutorial JSP

Custom Tag Tutorial JSP


Posted in : Java Posted on : February 6, 2012 at 7:20 PM Comments : [ 0 ]

In this tutorial you will learn about the custom tag.

Custom Tag Tutorial JSP

In this tutorial you will learn about the custom tag.

Custom Tag is a mechanism provided by the JSP technology that encapsulates the task that is to be repeated such as processing forms, accessing databases etc.

In JSP a custom tag is a tag or element that is defined by a user or you can say in JSP language a user-defined tag or element is called a custom tag. Custom tags are collectively called a tag library into which there is a set of related custom tags are defined and holds objects which implements the tags. A JSP page which contains a custom tag is translated into a servlet, and the tag performs the operation on an object which implements the custom tag. Tag handler object implements a custom tag. There are two types of tag handler :

  • Simple tag handlers : It can be used only for the tags that does not use a scripting element in attribute values / tag body.
  • Classic tag handlers : It must be used for the tags that is needed to use a scripting element in attribute values / tag body.

To define a custom tag you would be required at least the following :

Tag Handler : A tag handler is an object which implements a custom tag. To define a tag handler we have to use the  javax.servlet.jsp.tagext package. This package contains the classes and interfaces to implement the tag handler. A classic tag handler can implements either of the following interfaces :

  • Tag
  • IterationTag
  • BodyTag

or can extends its base classes :

  • TagSupport
  • BodyTagSupport

Methods of Tag handler are defined by the Tag and BodyTag interfaces.

TagLibraryDescriptor (.tld) : It is a XML file into which information of whole library and each tag (contained in the library) are held. This xml file is accessed/used by the web server for tags validating and by the jsp page development tools. Extension of this file must be given as .tld and must be kept in the web-inf directory as /WEB-INF/fileName.tld. A TLD(.tld) file must be start with the taglib root element.

Tag : It is an interface which defines a basic set of rule between tag handler and the class that implements the JSP page. It's life cycle and the other methods invoked at the start and end tag.

There are some features for using the custom tag :

  • The first and very simple feature is it's reusability. Due to the reusability it decreases the web application development time.
  • In a JSP page all the available objects can be accessed by the custom tag.
  • Responses that are generated by the calling page can be changed (modified) by the custom tag.
  • It can be nested.
  • custom tags can be customized by passing the attributes from the calling page.
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics