You are on page 1of 6

JSP Various types of custom tags are: 1.

Empty tag:refers to the custom tags that does not have any attribute or body.The following code snippet shows an empty custom tag: <td:welcome> 2.Tags with attribute: Refers to custom tags for which you can define attributes to customize the behaviour of the custom tag.You can set the attribute value from the string variable or from a runtime expression with the request object.The following code snippet shows a custom tag with an attribute color: <td:welcome color=blue></td:welcome> 3.Tags with a body:Refers to the custom tag within which you can define nested custom tags,scripting elements,actions,HTML text,and JSP directives .The following code snippet shows a custom tag that contains a JSP scripting element as its body: <td: welcome> <%=today_date%> </td:welcome> 4.Nested tags: Refers to the set of custom tags in which one custom tag encloses one or more custom tags.The tag that encloses the other tags is knows as the parent tag.The tag that encloses the other tags is known as the parent tag.The tag that is enclosed by the parent tag is known as the child tag.The following code snippet shows a nested custom tag: <td1:ifTag condition <%=eval>> <td2:valesTrue> The expression evaluates to true </td2:valueTrue> </td1:ifTag> Creating custom tag: Steps: 1.Develop a tag handler. 2.Develop the Tag Library Descriptor(TLD)file. 3.Include the Tag Library in a JSP page. 4.Deploy the application. Develop a tag handler The javax.servlet.jsp.tagext package provides the classes and interfaces that you can use to develop tag handlers.Base classes,such as TagSupport and BodyTagSupport of the

javax.servlet.jsp.tagext package implements the Tag interface to provide implementations of the interface methods.You can extend these helper classes in your tag handler class and override those methods that are required to implement the functionality of your tag. To develop a tag handler for an empty tag,you can extend the TagSupport class of the javax.servlet.jsp.tagext package in your tag handler.The following code snippet shows a tag handler,WelcomeTag that extends the TagSupport class to implement a custom tag: Import javax.servlet.jsp.*; Import javax.servlet.jsp.tagext.*; Public class WelcomeTag extends TagSupport The Tag interface defines lifecycle methods of a custom tag.The base classes,such as TagSupport and BodyTagSupport provide default implementation of these methods.You can override these methods in your tag handler to specify how your custom tag needs to behave.Some important methods that you need to override in the tag handler of an empty custom tag are: doStartTag(): The method is called when the container encounters the start tag of a custom tag.You can override this method in your tag handler to implement the functionality of your tag.For an empty cutom tag,the doStart () method needs to return the int field SKIP_BODY defined in the Tag interface.This field indicates that the container should not evaluate the body content of the tag. doEndTag(): This method is called when the container encounters the end tag of a custom tag.For an empty custom tag, the doEndTag() method needs to return SKIP_PAGE.The SKIP_PAGE indicates that the container should not process the remaining content of the JSP page. Develop the TLD File: A TLD file defines a custm tag in XML format.A TLD file stored with a .tld extension provides information,such as tag library version,name of the tag,description of the tag,and the name of the tag handler that implements the tag. A TLD file contains a root element <taglib> within which various elements appear.A TLD file can define moe than one custom tag.Each custom tag definition appears within a <tag> element inside <taglib> element.For example,if a TLD file defines tow custom tags,there will be two <tag> elements within the <taglib> element.

Element Name <tlib-version>

Description Defines the version of the tag library.The element,<tlibversion></tlib-version>,must be declared when you define a new tag library Defines the version of the JSP page that the tag library uses. Defines a short name of the tag library that refers to the tag library.

<jsp-version> <short-name>

The tag element that appears within the tablib element defines the custom tag. Element Name <name> Description Defines the name of custom tag.This is a required tag because it must be defined while creating a JSP custom tag. Defines the tag handler class that provides the functionality of custom tag.It is a required element and you must specify the fully qualified name of the class. Defines the tag functionality.This is an optional element. Defines the body content enclosed within the opening and closing tag of the custom tag.For empty custom,the body of this element is empty.

<tag-class>

<description> <body-content>

Eg: <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>Welcome Tag</short-name> <description> A custom tag to display welcome message </description> <tag> <name>Welcome</name> <tag-class>welcome.WelcomeTag</tag-class>

<body-content>empty</body-content> </tag> </taglib> 3.Include the Tag Library in a JSP page The JSP taglib directive allows you to include a tag library in a JSP page.The taglib directive contains the uri attribute that specifies the location of the TLD file and a prefix attribute that specifies the name with which the JSP page will use the custom tag. You can use the following code snippet to include the tag library defined by Welcome.tld in your JSP page: %@taglib uri =/Welcome.tld prefix =mytag% Once you have included the tag library,you can use the following code snippet to use the custom tag: <mytag:Welcome/> 4.Deploy the application During deployment,you need to map the tag library uri specified in the uri attribute of the taglib directive to the actual location of the TLD file.You can specify the mapping in the JSP Tag Libraries pane under the File Refs tab of the J2EE Deploytool window. <jsp-config> <tag-lib> <taglib-uri>/Welcome.tld</taglib-uri> <taglib-location> /web-inf/Welcome.tld</taglib-location> </taglib> </jsp-config> Custom Tag API Custom tag API consists of classes and interfaces,of the javax.servlet.jsp.tagext package,that are used for creating custom tags.These classes and interfaces allow coomunication between a custom tag and its runtime environment. Tag:Defines the methods that are called by the JSP implementation class during the lifecycle of the tag.A tag handler implements these methods to perform custom actions.The methods are:doEndTag(),doStartTag(),release(),setPageContext(),setParent(),getParent().

IterationTag:Extends the Tag interface and defines the method,doAfterBody(),in addition to the methods defined in the Tag interface.This method allows re-evaluation of the body of the custom tag. BodyTag:Extends the IterationTag interface and defines methods that enable a tag handler to manipulate the body content of a custom tag.This interface defines the doInitBody() and setBodyContent() methods.The doInitBody() methods allow a tag handler to prepare the tag for evaluation.The setBodyContent() method allows the tag handler to manipulate the body content of the tag. Class BodyContent TagSupport BodyTagSupport TagData TagInfo TagLibraryInfo Description Is a subclass of the JSPWriter class and represents the body content of a tag. Act as a base class for tag handlers and implements empty tags. Implements the BodyTag interface.This class is used to develop custom tags with body. Represents the attributes and their values. Represents the information specified in the <tag></tag> elements of the TLD file. Represents the information of the TLD file,such as the tags it defines and versioning information.

Various Methods: Method Description

Public int doStartTag() Defined by the Tag interface.This method is invoked when the start tag of the custom tag is encountered.The doStartTag() method returns the SKIP_BODY value to specify that the processing of the body content should be skipped.This method can also return EVAL_BODY_INCLUDE value to specify that the body content of the tag should be processed. Public void release() Defined by the Tag interface.This method is invoked to allow the tag handler to release some of its resources. DoAfterBody() Implemented by the BodyTagSupport class.This method is invoked after the body tag is evaluated.The

doAfterBody() method returns the EVAL_BODY_AGAIN value to specify that the body content should be re-evaluated.This method also return SKIP_BODY value to specify that the evaluation of the body content should be skipped. Public int doEndTag() Defined by the Tag interface.This method is invoked when the end tag of a custom tag is encountered.The doEndTag() method returns the EVAL_PAGE value to process the remaining JSp page or the SKIP_PAGE value to skip the processing of the remaining page. To define a custom tag with attribute in a TLD file,you need to specify that the custom tags contain attribute in the attribute element of the TLD file.The attribute element has following sub-elements. 1.name-Specifies the name of the attribute 2.required specifies whether the attribute is requires or optional.The value,true indicates the attribute value must be present in the JSP page. 3.rtexprvalue:specifies whether a run-time expression ,such as JSP expression can be used for this attribute.Possible values include true,false,yes,no

You might also like