The document discusses creating custom tags in JSP. Custom tags allow encapsulation of complex code for reuse and segregation of business logic from presentation. A tag library consists of tag handlers that define tag functionality and a TLD file that describes tags. Tag handlers implement tag methods like doStartTag and doEndTag. The TLD file specifies tags and their attributes, and is referenced in JSP pages using taglib directives.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
92 views30 pages
1creating Custom Tags
The document discusses creating custom tags in JSP. Custom tags allow encapsulation of complex code for reuse and segregation of business logic from presentation. A tag library consists of tag handlers that define tag functionality and a TLD file that describes tags. Tag handlers implement tag methods like doStartTag and doEndTag. The TLD file specifies tags and their attributes, and is referenced in JSP pages using taglib directives.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30
Creating Custom Tags
Objectives In this lesson, you will learn to: Use JSP custom tags
Create a JSP page by using custom
tags Need for Custom Tag Library To segregate presentation and
business logic and detaching
designers from the intricacies of programming constructs Provide a mechanism to
encapsulate complex recurring
codes Reuse the codes or tasks in a The Tag Library ☛ Consists of a collection of functionally related, user- defined tags called custom tags ✓ Custom tags are user-defined and explicitly render information about the type of data ✓ Are very similar to XML tags ✓ Allows interactions between the user and the browser ✓ Facilitates reuse of existing codes to improve work productivity Comparing the HTML and XML code outputs ☛ The HTML code: <HTML> <BODY> <TABLE align=“center”> <TR><TD>Ms Anne Brown</TD> <TD>9,Sunley House,GunthorpeStreet </TD> <TD>London E1-7RW</TD> </TR </TABLE> <BODY> </HTML> The HTML code (Contd.) ☛ The output: ✓ Is presentation-centric ✓ Contains no additional information about the type and importance of each section of data input ✓ Use of predefined HTML tags results in loss of information about the type of data The XML code ☛ The XML code: <CUSTOMERDETAILS> <NAME> <TITLE>Ms</TITLE> <FIRSTNAME>Anne</FIRSTNAME> <LASTNAME>Brown</LASTNAME> </NAME> <ADDRESS The XML code (Contd.) <APTNAME>9, Sunley House</APTNAME> <STREETNAME>Gunthorpe Street</STREETNAME> <COUNTRY>London</COUNTRY> <ZIP>E1-7RW</ZIP> </ADDRESS> </CUSTOMERDETAILS> The XML code (Contd.) ☛ The output: The XML code (Contd.) ✓ Is more data- centric ✓ Data is presented in a format with emphasis on its type ✓ Specific custom tags such as firstname, lastname, street, and zip are used to differentiate various sections of the data Advantages of XML ☛ Easy to code ☛ Use of explicit custom tags allows exchange of data at different levels without having to decode or interpret its structure ☛ Exchange of data between organizations without the need to understand the intricacies of business ☛ The detailed rules and specifications followed in the XML code are written in Document Type Definition (DTD) Custom Tags ☛ Enable segregation of business complexities from the content presentation ☛ Incorporate features such as: ✓ Communication ✓ Object accessibility ✓ Nesting Structure of a Tag ☛ Contain the start/end tags and a body ☛ Can be categorized as body tags or empty tags ☛ Can also be nested to contain tags of various levels ☛ Can include attributes used as parameters to customize the tag behavior The tag library consists of: Tag handler Contain definitions of the classes and methods Define the functionality of the tag Tag Library Descriptor file (TLD) Is an XML file Describes the tag library The tag handler: Is used to define the working of custom tags Derives its methods from the javax.servlet.tagext package Implements the TagSupport interface for tags with an empty body Implements the BodyTagSupport interface for tags that use a body Also includes classes from other packages, such as javax.servlet.jsp and java.io Can be structured for a basic tag, a tag with attributes, and a tag with a body Based on its structure consists of the following Structure methods: of the Tag Methods to be implemented handler
Simple tag with no body and doStartTag, doEndtag, and release
no attributes Tag with attributes doStartTag, doEndtag, and the respective set() and get() methods for each of the tags defined Can define its functionality using the methods from the abstract class Tag such as: doStartTag() doEndTag() release() doAfterBody() doBeforeBody() The doStartTag() method returns the following Tag objects : SKIP_BODY EVAL_BODY_INCLUDE EVAL_BODY_TAG The doEndTag()method returns the following Tag objects : SKIP_PAGE EVAL_PAGE The tag handler also includes the following classes and methods : The JspWriter() method to write the output to a JSP page The getAttribute() and setAttribute() methods retrieve variable values from scriplets Methods of the TagExtraInfo class such getVariableInfo(), TagExtraInfo(), setTagInfo(),and getTagInfo() The diagrammatic representation of the execution of a JSP file containing custom tags: The sequence of the execution cycle of a JSP file containing custom tags: ✓ The JSP engine identifies the taglib directive in the JSP page ✓ The specified tag handler is initialized by using the uri and prefix as reference ✓ The get() and the set() methods for each tag is then executed ✓ The doStartTag() method is invoked ✓ The tag body is evaluated next, but is skipped if the SKIP_BODY field constant is specified ✓ The setBodyContent() method is invoked to store the output of the tag into a special PrintWriter called JspWriter ✓ Next, the doAfter() method is invoked to process the content generated after the evaluation of the tag body ✓ The doEndTag() method is invoked and all connections created earlier are closed to direct the output to the browser The TLD file: Is an XML file that contains the tag library description Contains a list and description of all the custom tags in the library Can be classified into two groups: The first group comprises the sub-elements of the root tag of the TLD or the <taglib> tag The second group within the taglib tag comprises of the element <tag> tag The sub-elements of the TLD file at the taglib level are:
Component Specifies
<TLIBVERSION> The version of the tag library such as
<TLIBVERSION>1.0</TLIBVERSION> <JSPVERSION> The version of JSP that the tag library depends on such as <JSPVERSION>1.2</JSPVERSION>
<SHORTNAME> The name for the tag library
Component Specifies
<URI> The universal resource identifier, an
optional component that is a unique id for the tag library <INFO> The detailed information about the tag library <NAME> Defines a name for the tag
<TAGCLASS> Specifies the tag handler class
The elements of the TLD file at the tag level are listed in the table below: Component Description
<INFO> Provides additional information about the
tag and its functionality <ATTRIBUTE> Specifies the attribute name and requirement specification for the tag <BODYCONTENT> Contains the definition for the body of the tag The steps to create the TLD file are: ✓ In the Notepad, include the definitions for the document type and its definition (DTD) as the header ✓ Add the tlibversion, jspversion, uri, and info tags, along with their relevant information within the <taglib> tag ✓ Add the tag definitions separating each element within the <tag> and </tag> tags ✓ Add </taglib> to end the taglib tag. The errors in JSP can be categorized as: Translation time errors that are handled by the JSP engine Request time errors that are runtime errors and throw exceptions Errors are trapped and handled in the calling JSP page . Errors not trapped are forwarded to the error page The error page: Is a separate JSP file Uses the following attributes of the page directive: errorPage: To specify the name of an error page in the basic JSP page <%@ page errorPage=”errorpage.jsp” %> isErrorPage: To specify a JSP page as an error page <%@ page isErrorPage=”true” %> A JSP page using custom tags specifies: The tag usage with the taglib directive <% @taglib uri=”taglib-examples” prefix=”example” %> uri is used to specify a unique identifier for the particular tag library prefix is used to specify a reference name for the particular tag library The inclusion of a new tag named first for the tag library example, the tag is written as: <example:first>