I. Basic concepts:
1. Tag: a Tag is an XML element. Through tags, JSP web pages can be simplified and easy to maintain, and the same JSP file can be conveniently implemented in multiple languages. Because tags are XML elements, their names and attributes are case sensitive.
2. Tag library: a set of tags that have similar functions and are logically associated with each other is called a Tag library.
3. Tag Library description file (Tag Library Descriptor): The Tag Library description file is an XML file that provides the ing between the class in the Tag Library and the Tag reference in JSP. It is a configuration file, which is similar to web. xml.
4. tag Handle Class: The Tag processing Class is a Java Class that inherits the TagSupport or extends the SimpleTag interface. This Class can be used to customize JSP tags.
Ii. Custom JSP tag format:
To enable the JSP Container to use the custom behavior in the tag library, the following two conditions must be met:
1. Identify the tag representing this custom row from a specified tag Library
2. find the first required condition for a specific class that implements these custom behaviors-find a custom behavior that belongs to the tag library-it is completed by the Taglib Directive's Prefix attribute of the tag command, therefore, all elements with the same prefix on the same page belong to this tag library. Each tag library defines a default prefix, which is used to insert custom tags in the document or page of the tag library. Therefore, you can use prefixes such as jsp, jspx, java, servlet, sun, and sunw (they are all reserved words specified in the JSP White Paper.
The uri property meets the second requirement. Find the corresponding class for each custom behavior. This uri contains a string that the container uses to locate the TLD file. In the TLD file, you can find the names of all tag processing classes in the tag library.
When a web application starts, the container searches for all files ending with. tld from the WEB-INF of the directory structure of the META-INF folder. That is to say, they will locate all TLD files. For each TLD file, the container first obtains the URI of the tag library, and then creates a ing relationship between each TLD file and the corresponding URI. On the JSP page, we only need to use the tag library command with the URI property value to match the specific tag library.
3. Process custom JSP labels:
1. Introduce the tag library in JSP:
2. Use the tag library tag in JSP:
3. The Web Container obtains the uri attribute value of taglib declared in the first step based on the prefix in the second step.
4. The Web Container finds the corresponding element in web. xml based on the uri attribute.
5. Obtain the value of the corresponding element from the element
6. Web containers find the corresponding. tld file from the WEB-INF/directory based on the element value
7. Find the corresponding tagname element from the. tld file.
8. Obtain the value of the corresponding element from the elements.
9. The Web Container creates the corresponding tag handle class instance based on the element value.
10. The Web Container calls the doStartTag/doEndTag method of this instance to complete corresponding processing.
4. Basic Steps for creating and using a Tag Library:
1. Tag Handler Class)
2. Create a Tag Library description File (Tag Library Descrptor File)
3. Configure elements in the web. xml file
4. Add a tag Library to the JSP file
V. Introduction to TagSupport:
1. The class for processing tags must extend javax. servlet. jsp. TagSupport.
2. main attributes of the TagSupport class:. parent attribute: processing class B of the Upper-layer labels nested with the current tag. pageContex attribute: Indicates javax in the Web application. servlet. jsp. pageContext object
3. before calling the doStartTag or doEndTag method, JSP containers call the setPageContext and setParent methods to set pageContext and parent. Therefore, you can directly access the pageContext variable in the tag processing class.
4. The pageContext member variable cannot be accessed in the TagSupport constructor, because the JSP Container has not yet called the setPageContext method to initialize pageContext.
6. How TagSupport handles tags:
1. The TagSupport class provides two methods to process tags: public int doStartTag () throws JspException public int doEndTag () throws JspException
2. doStartTag: but when the JSP Container encounters the starting flag of the custom tag, it will call the doStartTag () method. The doStartTag () method returns an integer to determine the subsequent process of the program. A. Tag. SKIP_BODY: indicates...
The content between tags is ignored. B. Tag. EVAL_BODY_INCLUDE: indicates that the content between tags is executed normally.
3. doEndTag: however, if the JSP Container encounters the end mark of the custom tag, the doEndTag () method is called. The doEndTag () method also returns an integer to determine the subsequent process of the program. A. Tag. SKIP_PAGE: indicates that the webpage is stopped immediately. Unprocessed static content and JSP programs on the webpage are ignored. Any existing output content is immediately returned to the client's browser. B. Tag_EVAL_PAGE: indicates that the JSP page continues to be executed according to the normal process.
7. custom tag attributes: If the tag also contains custom attributes, such :...
This attribute should be used as a member variable in the tag processing class, and the methods for setting and reading attributes are provided respectively.
8. Steps for creating a tag processing class: 1. create a file containing static JSP webpage text (that is, the text to replace the custom JSP tag) 2. load Static text at Web application startup 3. create tag processing class
9. How to create a file containing static JSP webpage text:
1. Use the java. util. Properties class to store static text to replace custom JSP labels on webpages
2. The Properties class represents a set of attributes. Its instances can be saved to the stream or loaded from the stream. These texts are stored in the WEB-INF directory as key/value, such as key = value, which are of the String type in the property list
10. Common Properties APIs:
1. setProperty (String key, String value): Call the put method of the Hashtable class to add attributes.
2. getProperty (String key): Get the property value corresponding to the key in the property list
3. load (InputStream in): Read the Properties list from the InputStream object)
4. store (OutputStream out, String coMMent): Write the attribute pairs of the attribute list to the output stream object in the appropriate format, by default, using ISO-88590-1 encoding format to process input in rows. The key/value pairs of the attribute are paired with "=" and ":", separated by carriage return and line feed.
11. Common APIs of the ServletContext class:
1. getContext (String uripath): returns the ServletContext object represented by uripath on the server.
2. getInitParameter (String name): returns the value of the name parameter in the ServletConfig object.
3. getMineType (String file): returns the MIME type of the object represented by the file parameter.
4. getRequestDispatcher (String path): returns the RequestDispacher object represented by path.
5. getResourceAsStream (String path): The resource corresponding to the path is returned in the form of an input stream. Objects in the input can be in any form of data. The path parameter must start with "/" and be relative to the Context Root.
12. How to Use ServletContxt to read and save attribute files:
1. Create a java. util. Properties Class Object
2. Get the ServletContext object
3. Read the attribute file into an input stream object as an input stream.
4. Load the input stream object to the Properties object
5. Save the Properties object to the ServletContext object.
13. How to load static text at Web application startup:
1. Create a subclass that inherits the HttpServlet class. When configuring this Servlet in web. xml, set the load-on-startup attribute: someclass somepackage. SomeClass1
2. Create the java. util. Properties class in the init () method of the Servlet.
3. Obtain the ServletContext object of the current Web Application
4. Read the properties file under the WEB-INF Directory into the InputStream input stream: InputStream in = context. getResourceAsString ("WEB-INF/someproperties. properties ");
5. load the input stream to the property object ps. load (in );
6. Save the property object to the context. Context. setAttribute ("attributeName", ps );
14. How to Create a tag processing class:
1. Introduce required resources: import javax. servlet. jsp. *; import javax. servlet. http. *; import java. util. *; import java. io .*;
2. inherit the TagSupport class and overwrite the doStartTag ()/doEndTag () method
3. Obtain the java. util. Properties object from the ServletContext object.
4. Get the property value corresponding to the key from the Properties object
5. process the obtained attributes and output the results.
15. Create a Tag Library description file (Tag Library Descriptor ):
1. The tag library description file, TLD for short, defines the user's tag library in XML format. The elements in the TLD file can be divided into three categories: A.: Tag library Element B.: Tag Element C.: Tag attribute Element
2. the tag library element is used to set information about the tag library. Its common attributes include:. shortname: Specify the default prefix name (prefix) B of the Tag Library. uri: Set the unique access identifier of the Tag Library.
3. the tag element is used to define A tag. Its common attributes include:. name: Set the Tag name B. tagclass: Set the Tag processing class C. bodycontent: Set the subject (body) content of the tag 1 ). empty: indicates that the tag does not contain body 2 ). JSP: indicates that JSP code can be added to the TAG body. 3 ). tagdependent: indicates that the content in the tag is processed by the tag itself.
4. the tag attribute element is used to define the attributes of A tag. Its common attributes include:. name: attribute name B. required: required or not. The default value is false. rtexprvalue: whether the attribute value can be a request-time expression, that is, a similar expression
16. Use tags in Web applications:
1. if a custom JSP tag is used in a Web application. add an element to the xml file, which is used to declare the tag library where the referenced tag is located/sometaglib/WEB-INF/someTLD. tld
2. Set the unique identifier of the Tag Library, which will be used to reference the Tag Libray in the Web application.
3.: specify the location of the TLD file corresponding to the Tag Library
4. Add commands to the JSP file to declare references to the tag library.
5. prefix indicates the prefix when the Tag of the Tag Library is referenced in the JSP webpage. The uri is used to specify the Tag Library identifier, which must be consistent with the attributes in web. xml.
- Analysis on single processing and multi-task processing of JSP Technology
- Full introduction to JSP SmartUpload upload/download
- Simple Style Web architecture, secondary restoring of JSP Jdbc
- Install and configure the Web server and JSP Engine
- Detailed explanation of JSP technology methods