Custom tags are mainly used to remove java code from Jsp pages.
Execution Process of custom JSP labels
When a JSP page containing custom tags is translated into Servlet by the JSP Engine (Web Container), the JSP Engine encounters custom tags, this custom tag is converted into a call called "tag processing class. Then, when the JSP page is executed, the JSP Engine will call the "tag processing class" object and execute the corresponding operation methods defined internally to complete the corresponding functions.
Custom JSP tag Development Process
When using the Java processing class to develop custom JSP labels, there are mainly the following steps.
Tag Handle Class ). This class implements the Tag interface, which is used to define the behavior of tags and is called and executed when the JSP Engine encounters a custom Tag.
Create a Tag Library description File (tld) and describe the Tag processor class in the tld File.
Use the taglib command in the JSP file to introduce the tag library, and then use the tag name specified in the tag library description file.
Tag interface Execution Process
When the JSP Engine encounters a custom tag, it first creates the Instance Object of the tag processor class, and then calls its method according to the communication rules defined in the JSP specification.
1. public void setPageContext (PageContext pc). After the JSP Engine instantiates the tag processor, it will call the setPageContext method to pass the pageContext object of the JSP page to the tag processor, the tag processor can communicate with the JSP page through this pageContext object.
2. public void setParent (Tag t). After the setPageContext method is executed, the WEB Container then calls the setParent method to pass the parent Tag of the current Tag to the current Tag processor, if the current tag does not have a parent tag, the parameter value passed to the setParent method is null.
3. public int doStartTag (). After the setPageContext method and setParent method are called, The doStartTag method of the tag processor is called when the WEB Container runs the start tag of the custom tag.
4. public int doEndTag (): After the WEB Container executes the label body of the custom tag, it then executes the end tag of the custom tag, the WEB Container calls the doEndTag method of the tag processor.
5. public void release (). Generally, after a WEB Container executes a custom tag, the tag processor will reside in the memory and be another request server until the web application is stopped, the web Container will call the release method.
When writing Jsp pages, developers often need to introduce some logic in the pages, such:
Controls whether to execute a part of the content on the jsp page.
Determines whether the entire jsp page is executed.
Control repeated execution of jsp page content.
Modify jsp page content output.
In addition to java code removed from the jsp page, the custom tag can also implement the preceding functions.
Four tag types in tld files
Empty jsp scriptless tagdepentend
Tag library Descriptor
The tag library descriptor file is a standard XML document ending with ". tld". It is used to record the tags in a tag Library and the attributes of each tag.
The following is the content of a JSP 2.0 standard tag library description file:
<Taglib> an element is the root element of the tag library descriptor. It contains 12 sub-elements. The details are as follows.
(1) <description>: a text description of the tag library.
(2) <tlib-version>: Specifies the version of the tag library.
(3) <short-name>: defines a brief name for the tag. It can be used as the preferred prefix name in the taglib command.
(4) <uri>: defines a URI that uniquely identifies the tag library.
(5) <tag>: used to specify information about custom tags.
(6) <display-name>: Specifies a brief alias for the tag library.
(7) <small-icon>: Specifies a 16 × 16 small icon (in gif or jpeg format) for the tag library, which can be displayed in the graphic interface tool.
(8) <large-icon>: specify a 32 × 32 icon (in gif or jpeg format) for the tag library. This icon can be displayed in the graphic interface tool.
(9) <validator>: provides a validator for the tag library.
(10) <listener>: provides a listener for the tag library.
(11) <tag-file>: used to describe the tag file.
(12) <function>: used to specify the function used in the expression language.
Lifecycle of the TagSupport class
Life cycle of the BodyTagSupport class