JSP Servlet call in tag Library

Source: Internet
Author: User
Tags tld

Taglib

Tags are defined and distributed in a structure called the tag library. A tag library is a set of metadata and classes:
1. Tag Processor: Java class that implements the custom tag function.
2. Tag additional information (TEI): provides edge information to JSP containers to confirm tag attributes and create variable classes.
3. Tag library Descriptor (TLD): an XML document that describes a single tag and the attributes of the entire tag library.

The tag processor and additional tag information must be located in a location that can be found in the JSP Container class loader. The tag library descriptor can be specified in the URL. The JSP1.1 specification requires the JSP Container to accept the label library of a JAR file with the packaging structure. TLD must be a file named taglib. tld in the/META-INF directory, and the JAR file is copied to the/WEB-INF/lib directory.

I. Tag implementation

1. Development steps
A. Define the tag name, attributes, declared variables, and tag body content.
B. Write the tag library descriptor TLD.
C. Compile the tag processor.
D. Use tags on the JSP page.

2. JSP page conversion steps in the JSP Container:

There are three forms of JSP pages: JSP files, java files, and class files.
A. Command elements, and provide the conversion information to the JSP Container.
B. the HTML line is converted to the out. print () language name in sequence in the _ JSPService () method.
C. The declaration of the script element is copied to the source code outside the _ JSPService () method.
D. The expression of the script element is converted to the out. print () language name in sequence in the _ JSPService () method.
E. The Scriptlet of the script element is copied to the _ JSPService () method.
F. The behavior element is converted to the runtime logic code that executes its functions.
G. custom labels are extended to Java statements that call methods in the corresponding tag processor.

3. Tag conversion steps in the JSP Container:
A. JSP containers use the taglib command element to locate the tag library Descriptor and match the custom tags used in the page with the TLD.
B. Read the tag LIST OF THE logstore Descriptor and the class names related to each tag.
C. When you encounter a tag on the page, find a tag library related to the tag prefix with the specified name.
D. The container uses the tag structure information found in the TLD to generate a series of Java statements to complete the tag function.

Ii. Tag library Descriptor (TLD)

The tag library descriptor is an XML document describing the tag information of the entire tag library and each tag processor and its attributes in the library.

The DTD of the tag library descriptor consists of a simple element, which contains the following child elements.

Tag Information of the entire tag Library
◆ Tlibversion: version number of the tag library. It is a dotted decimal number, which consists of up to four groups of digits separated by decimal places.
◆ The minimum JSP Specification Version Required by the JSPversion tag library. For example, JSP1.1
◆ Abbreviation of shortname tag library. JSP can use this name as the default prefix of tags in the library.
◆ Unique uri element of the URI tag library. The typical URL location is from the location where you can download taglib.
◆ Description of info tag library.
Each tag processor and Its Attribute tag are added to the TLD to describe each tag that constitutes the library. The name of the tag used together with the name prefix of the tag library is the unique tag ID of the JSP Container. The full name of the tag processor class that implements the tag. The full name of the teiclass tag additional information (TEI) class. The TEI class provides information about the tag processor creation variables and any validity verification of tag execution. Bodycontent describes how the label processor uses the content of the label body. Three values are available:
◆ Empty: indicates that the label body must be empty;
◆ JSP: the script element is evaluated like the template and other labels.
◆ Tagdependent: The body content is unblocked and written to BodyContent. Other script elements are in the source code format and are not interpreted by the JSP Container.

Manual readable descriptive information of info labels. Attribute information encoded when tags are used. Defines the attributes of a tag. Property name: name of the property.
◆ True | false: whether the attribute is encoded at the position used by the tag.
◆ True | false: whether the attribute value can be specified using an expression.

Iii. Tag Processor

The tag processor is a Java class that executes custom tag behaviors through a series of predefined methods called by JSP containers.

The tag processor implements the behavior of tags. The tag processor is a Java class.

1. How the tag processor works
A. Import the javax. Servlet. JSP and javax. Servlet. JSP. tagext packages.
B. Implement the Tag interface or BodyTag INTERFACE IN THE javax. Servlet. JSP. tagext package. BodyTag is a sub-interface of a Tag.
C. inherit from the TagSupport class or BodyTagSuppoert class. These are the default implementations of the above interfaces.
D. Reload the public int doStartTag () throws JSPException method.

2. interface and implementation of the Tag Processor
◆ Javax. Servlet. JSP. tagext. Tag is the most basic interface for implementing tags.
◆ Javax. Servlet. JSP. tagext. TagSupport is a specific class for implementing the Tag interface.

Generally, it is beneficial to inherit the tagSupport class without directly implementing the Tag interface. In addition to providing default implementation for all required methods, the pageContext object and support for nested labels are also saved.

The Tag Interface contains four constants, indicating the possible return codes of doStartTag () and doEndTag () methods.
When doStartTag () is returned, EVAL_BODY_INCLUDE indicates that the Servlet should evaluate the TAG body.
SKIP_BODY when doStartTag () is returned, it indicates that the Servlet should ignore the label body.
When doEndTag () is returned, EVAL_PAGE indicates that the rest of the page should be evaluated.
SKIP_PAGE when doEndTag () is returned, the rest of the page is skipped.

Tag Interface Method

The Servlet generated by public void setPageContext (PageContext ctx) calls this method before requesting the processor to execute other tasks. The implementation class should save the context object so that it can be used in the tag lifecycle. From the context of the page, the tag processor can access all JSP implicit objects.
Public void setParent (Tag p) uses a Tag to locate the Tag on the Operation stack. Call after setPageContext.
Public Tag getParent () returns the parent Tag.
Public int doStartTag () throws JSP is called after setting the encoding attributes in the page context, parent tag, and start tag. The returned code table shows whether the JSP implementation Servlet evaluates the TAG body.
Public int doEndTag () throws JSPException is called when the mark is disabled. Returns the code table indicating whether JSP is applicable to the rest of the new page.
Public void release () is called before the page exits. Release the resource and reset the tag processor status.

TagSupport Class Method
Public static Tag finAncestorWithClass (Tag thisTag, Class cls) is the required parent Tag processor to find the runtime Tag stack. A tag processor can provide methods for calling sub-tags within its range.
Public void setId (String id) saves and retrieves the name specified in the id attribute.
Public void setValue (String name, Object o) is used to set the value of the specified name in the local hash table.
Public Object getValue (String name) obtains the value of the specified name from the local hash table.
Public void removeValue (String name) deletes the value of the specified name from the local hash table.
Public Enumeration getValues () returns an Enumeration of keywords in the hash table.

3. Life Cycle of the Tag Processor
A. to generate a Servlet, you need to create an instance of the tag processor class. The implementation is usually a method that calls the factory class of the JSP Container. The factory class contains a tag processor instance pool so that it can be reused for objects that are no longer in the active state.
B. initialize the tag processor to let the Servlet know its existence. Servlet implements this process by calling two methods of the Tag Processor: setPageContext (PageContext ctx) and setParent (Tag parent ).
C. If the tag has attributes, the attribute value is passed to the object through the setter method provided by the processor. The property setter method is the only method required by a tag to support the property.
D. The context and parent labels of the page have been set and attributes are available. Call the doStartTag () method of the tag processor to read these variables and perform the calculations and operations required to implement the Q & A function. The doStartTag () method must return an integer. If EVAL_BODY_INCLUDE is returned, the TAG body is processed normally. If SKIP_BODY is returned, the content at the tag end is ignored from the initial JSP page.
E. after the TAG body is evaluated or ignored, the doEndTag () method of the tag processor is called. If EVAL_PAGE is returned, the rest of the page is evaluated. If SKIP_PAGE is returned, the Servlet code is immediately returned from _ JSPService.

4. Interface and Implementation of the Body Tag Processor
Javax. Servlet. JSP. tagext. BodyTag is the sub-interface of the Tag.
Javax. Servlet. JSP. tagext. BodyTagSupport is the implementation of the BodyTag class.
BodyContent is a subclass of javax. Servlet. JSP. JSPWriter, but it is different from its parent class.
The content of the BodyContent object is accumulated in a String cache instead of automatically writing the Servlet output stream. After the TAG body is complete, its object can still be applied in the doEndTag () method, operated by the getString () or getReader () method. Modify and write the restored JSPWriter output stream when necessary.
BodyContent Class Method
Public void flush () throws IOException rewrites the JSPWrite. flush () method so that it always produces overflow. The refresh write is invalid because it is not connected to the actual output stream to be written.
Public void clearBody () resets the BodyContent cache to null.
Public Reader getReader () returns the body content read by Reader.
Public void writeOut (Write w) writes the body content to the specified output.
Public JSPWrite getEnclosing Write () returns the next higher writer object in the stack (possibly another BodyContent object ).

The BodyTag interface defines a new integer constant.

When doStartTag () is returned, the new BodyContent object is created and associated with the tag processor. When doAfterBody () is returned, the JSP Servlet re-evaluates the body after modifying any variable controlled by this tag.

BodyTag Interface Method
Public void setBodyContern (BodyContent out) has been written in the current JSPWriter. A new BodyContent is called by the JSP Servlet after it is created. It occurs after doStartTag.
Public void doInitBody () throws JSPException setBodyContent () is called before evaluation by the body. If the body is evaluated multiple times, this method is called only once.
The life-cycle method called when the BodyContent writer is still active after the public init doAfterBody () throws JSPException body is evaluated. This method must return EVAL_BODY_TAG or SKIP_BODY. If EVAL_BODY_TAG is returned, the body is evaluated again.

BodyTagSupport Class Method
Public int doStartTag () throws JSPException rewrites the doStartTag () method in TagSupport.
Public int doEndTag () throws JSPException calls the doEndTag () method in TagSupport and returns the result.
Public void setBodyContent (BodyContent out) saves the new body content object in a protected member variable bodyContent. This object can be directly accessed by subclass.
Public void doInitBody () throws JSPException does nothing by default. It is rewritten by the subclass that needs initialization.
Public int doAfterBody () throws JSPException is called by the JSP Servlet after each evaluation, and the body content object is still active. If SKEP_BODY or EVAL_BODY_TAG is returned, the body is evaluated again.
Public void release () sets the bodyContent object to null, and then calls super. release ().
Public BodyContent getBodyContent () returns the bodyContent variable. Subclass can access protection variables, but this method allows irrelevant tag processing classes to send output to this body.
Public JSPWriter getPreviousOut () is an easy way to call getEnclosingWriter () on the bodyContent variable and return results.

5. Life Cycle of the Body Tag Processor
A. to generate a Servlet, you need to create an instance of the tag processor class. The implementation is usually a method that calls the factory class of the JSP Container. The factory class contains a tag processor instance pool so that it can be reused for objects that are no longer in the active state.
B. initialize the tag processor to let the Servlet know its existence. Servlet implements this process by calling two methods of the Tag Processor: setPageContext (PageContext ctx) and setParent (Tag parent ).
C. If the tag has attributes, the attribute value is passed to the object through the setter method provided by the processor. The property setter method is the only method required by a tag to support the property.
D. The context and parent labels of the page have been set and attributes are available. Call the doStartTag () method of the tag processor to read these variables and perform the calculations and operations required to implement the Q & A function.
The doStartTag () method must return an integer.
◆ If EVAL_BODY_TAG is returned, the TAG body is processed normally (jump to e );
◆ If SKIP_BODY is returned, content from the initial JSP page until the end of the tag is ignored. (Skip to f)
E. If EVAL_BODY_TAG is returned, the TAG body is processed normally.
E1. Save the current JSPWriter object in the stack, create a new BodyContent object, and set it to the out object of the JSP page and save it in the attribute named name within the context range. Use its setBodyContent () method.
E2. call the doInitBody () method for initialization.
E3. process the TAG body. Write the output to the BodyContent object. This process depends on the Tag Element of the TLD. There are three possible values.
E4. call the doAfterBody () method to write the body content to JSPWriter, which can be implemented as follows:
◆ JSPWriter out = bodyContent. getEnclosingWriter ();
◆ Out. println (bodyContent. getString (); // bodyContent. writeOut (out );
◆ BodyContent. clear ();
The e5.doAfterBody () method returns two possibilities:
◆ When EVAL_BODY_TAG is returned, the TAG body is evaluated. This is a typical case where arrays and enumerations are processed cyclically.
◆ When SKIP_PAGE is returned, the rest of the page is continued.
The e6. body content is complete, so the process of creating it is reversed:
Call the pageContent. popBody () method to retrieve the previous JSPWriter object. Set the writer back to the out implicit object.
F. Call the doEndTag () method after the TAG body is evaluated or ignored to allow the tag processor to send back content like an output stream.
If EVAL_PAGE is returned, the rest of the page is evaluated;
If SKIP_PAGE is returned, the Servlet code immediately returns from _ JSPService.
G. The body content is still available in the protected bodyContent object. You can write it into the Servlet output stream:
◆ JSPWriter out = pageContext. getOut ();
◆ Out. println (bodyContent. getString (); or
◆ BodyContent. WriteOut (pageContext. getOut ());

6. Tag Additional information

Iv. Tag commands

The purpose of the taglib command element is to specify the location of the TLD and set a short alias separated from the tag on the page.

Syntax:

Attribute: prefix: the unique identifier used to identify the tag library. Uri: The URI of the tag library.

Uri does not need to point to an actual file. It is the unique identifier of the JSP Container that can search for the actual file location in web. xml.

  1. Future Response Servlet features
  2. Brief Introduction to Servlet instance running
  3. Analysis of ServletQueryForConnPool Test
  4. Tomcat Java Servlet Engine Security Settings
  5. Jetty-Additional Servlet container Functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.