1. Custom JSP Tags overview
A custom JSP tag is a programmer-defined JSP tag that encapsulates the information display logic in a separate Java class and describes its use through an XML file. When you need to use a similar display logic on a page, you can insert the label in the page to complete the function. using a custom label, you can detach the program logic and presentation Logic , Peel Java code from the HTML and make it easier to maintain the page; Custom tags also provide reusable functional components that improve the efficiency of engineering development.
a custom label is primarily used to remove Java code from a JSP page .
2. Custom JSP label Execution process
When a JSP page with a custom label is translated into a servlet by a JSP engine (Web container), the JSP engine encounters a custom label that converts the custom label into a call to a label processing class. After that, when the JSP page is executed, the JSP engine invokes the "tag handler class" Object and executes its internal defined method of operation to complete the function.
Customizing the development process for JSP tags
When you use Java processing classes to develop custom JSP tags, you are primarily divided into the next few steps.
– create a processing class for the label (tag Handle Class). This class implements the tag interface to define the behavior of the label and invoke execution when the JSP engine encounters a custom label.
– Create a tag library description (TLD) file (Tag Library descriptor file) that describes the label processor class in a TLD file.
– introduce the tag library with the taglib instruction in the JSP file and use it using the tag name specified in the tag library description file.
3. The execution process of the tag interface
When the JSP engine encounters a custom label, it first creates an instance object of the label processor class, and then calls its method in turn according to the communication rules defined by the JSP specification.
1), public void Setpagecontext (PageContext pc), after the JSP engine instantiates the label processor, the Setpagecontext method is invoked to pass the PageContext object of the JSP page to the label processor. The label processor can then communicate with the JSP page through this PageContext object.
2), public void setparent (Tag t), after the Setpagecontext method is executed, the SetParent method that the Web container then invokes passes the parent tag of the current label to the current label processor, if the current label has no parent tag. The parameter value passed to the SetParent method is null.
3), public int doStartTag (), after the Setpagecontext method and the SetParent method are invoked, the label processor's doStartTag method is invoked when the Web container executes the start tag of the custom label.
4, public int doendtag (), the Web container executes the label body of the custom label and then executes the end tag of the custom label, at which point the Web container invokes the Doendtag method of the label processor.
5), public void release (), usually when the Web container finishes the custom label, the label processor resides in memory, and the Web container calls the release method only when the Web application is stopped for other requesting servers.
4. JSP Label API
The main interfaces commonly used in the JSP 1.1 and 1.2 specifications are the following 3.
– tag: This interface defines a method that needs to be implemented for all tag-handling classes.
– Iterationtag: This interface extends the tag interface, adding a way to control the repeated execution of the label body.
– Bodytag: This interface extends the Iterationtag interface and increases the way to access and manipulate the contents of the tag body.
5. Tag Library Descriptor
The tag Library descriptor file is a standard XML document that ends with ". TLD" , which is used to record which tags are in a tag library, and what properties each tag contains.
The following is the contents of the tag library description file for a JSP 2.0 specification the:<taglib> element is the root element of the tag library descriptor, which contains 12 child elements, which are described in detail as follows (bold elements are common elements, remember).
– (1) <description>: A text description of the tag library.
– (2) <tlib-version>: Specifies the version of the tag library.
– (3)<short-name>: Define a short name for the label, which can be used as the preferred prefix in the taglib instruction.
– (4) <uri>: Defines a URI that uniquely identifies this tag library.
– (5) <tag>: Used to specify information about a custom label.
– (6) <display-name>: Specify a short alias for the tag library .
– (7) <small-icon>: Specifies a small icon (GIF or JPEG) of size 16x16 for the tag library, which can be displayed in the graphical interface tool.
– (8) <large-icon>: Specifies a large icon (GIF or JPEG) of size 32x32 for the tag library, which can be displayed in the graphical interface tool.
– (9) <validator>: Provides a validator for the tag library.
– (a) <listener>: Provides a listener for the tag library.
– (one) <tag-file>: Used to describe a label file.
– <function>: Used to specify a function to be used in an expression language.
6. Development of traditional label
Before developing a traditional label, you need to understand the lifecycle of two support classes (TagSupport and Bodytagsupport).
the life cycle of the TagSupport class can be represented by a diagram
the life cycle of the Bodytagsupport class can be represented by a diagram .
7. Develop custom tags with bodytagsupport class
There are two additional methods in the Bodytagsupport class:
–setbodycontent (bodycontent BC): When the container executes the instance of the tag-handling class, the method is invoked to cache the contents returned by the label body in an instance of the Bodycontent class. Bodycontent provides a way to write text to the response body, in addition to inheriting from the parent class JspWriter, and provides the label body content that is used to get its buffering.
–doinitbody (): This method is called before the label body is evaluated to initialize the total, and the initial content can be written to the Bodycontent object, which is placed before the label body content.
The Bodycontent class is designed specifically for caching the content returned by the label body, including static text and dynamic content created by nested tags or script elements.
The role or application of a traditional custom label :
– Controls whether a portion of a JSP page is executed.
– Controls whether the entire JSP page executes.
– Control the content of the JSP page for repeated execution.
– Modify JSP page content output.
• Custom tags can also be implemented in addition to the JSP page Java code can be removed.
Four types of tag bodies in TLD files
–empty JSP scriptless Tagdepentend