1creating Custom Tags

  • Uploaded by: api-3775545
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 1creating Custom Tags as PDF for free.

More details

  • Words: 1,250
  • Pages: 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, userdefined 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:

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: <TITLE>Ms Anne Brown
The XML code (Contd.) <APTNAME rel="nofollow">9, Sunley House <STREETNAME>Gunthorpe Street London E1-7RW


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 methods: Structure of the Tag Methods to be implemented



handler Simple tag with no body and no attributes

doStartTag, doEndtag, and release

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 tag The second group within the taglib tag comprises of the element tag



The sub-elements of the TLD file at the taglib level are: Component

Specifies



The version of the tag library such as 1.0

<JSPVERSION>

The version of JSP that the tag library depends on such as <JSPVERSION>1.2

<SHORTNAME>

The name for the tag library

Component

Specifies



The universal resource identifier, an optional component that is a unique id for the tag library



The detailed information about the tag library



Defines a name for the tag



Specifies the tag handler class



The elements of the TLD file at the tag level are listed in the table below: Component Description



Provides additional information about the tag and its functionality



Specifies the attribute name and requirement specification for the tag



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 tag Add the tag definitions separating each element within the and tags Add 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>

Related Documents

1creating Custom Tags
November 2019 7
07 Basic Custom Tags
November 2019 8
07-basic-custom-tags
July 2020 8
Jsp 1.2 Custom Tags
July 2020 7
Custom Tags Tutorial
November 2019 11
Ms Anne Brown 9,Sunley House,GunthorpeStreet London E1-7RW