First, we will introduce some trivial concepts.
JavaScript and JSP/Servlet
Common dynamic web page technologies include CGI (Common Gateway Interface), PHP (Hypertext Preprocessor), ASP (Active Server Pages), and Servlet/jsp (Java Server Pages ). Pure HTML that is not processed by server programs is a static webpage. In the introduction to Javascript, JavaScript is often defined as a dynamic web page. In fact, JavaScript code in HTML is executed by the browser rather than by the server. Javascript is actually irrelevant to Java. Javascript is used to communicate with the browser. You can also use JavaScript to request the browser to send a request to the server.
Therefore, JavaScript and Servlet/jsp cannot interact directly. They must interact with each other through HTTP, exchange data, request, and response. The server returns the JavaScript code embedded in the JSP webpage to the browser, which is executed by the browser. For servers that process JSP content, the embedded Javascript is no different from the static html tag.
Javaee
Java EE (Java platform, Enterprise Edition ). Javase (Java platform, Standard Edition) and javame (Java platform, Micro Edition ). Both Java EE, Java SE, and Java me are standards jointly developed by the industry. Industry representatives can participate in JCP (Java Community process) to participate, review, and vote on the platform's components, java specification
As a standard document, different technical solution standard specifications are assigned a number. Under the standard of the JSR specification, each vendor can implement their respective finished products. Therefore, different vendors may implement products for the same Web container, and JSR usually provides reference implementations.
Now let's continue to discuss the tag library, which is the main content of this article. Using it allows us to define our own labels. We discussed JSP action elements in servlet & JSP (10). In fact, Action elements are essentially a piece of Java code, when the JSP page is converted to a servlet, the JSP Container encounters an Action Element tag and replaces it with the pre-defined corresponding Java code. Custom labels are actually Java classes that implement specific interfaces.
Tag library API
The tag library API is defined in the javax. servlet. jsp. tagex package. The main interfaces and Class 1 are shown in.
Figure 1 Inheritance implementation relationship of main interfaces and classes in the tag Library
Custom tags are the same as other existing tags in four forms.
Empty tag <Hello/>
Tag with attributes <Max attr1 = "attr1" attr2 = "attr2"/>
Label with content <greeting> Hello </greeting>
Label with content and attributes <greeting name = "Shan"> Hello </greeting>
Tag Interface
The javax. servlet. jsp. tagext. Tag interface defines the basic implementation methods of all traditional tag processors. Including setpagecontext (), setparent (), and getparent. The lifecycle of the tag processor that implements the tag interface is as follows:
1) After the container creates a tag processor instance, call the setpagecontext () method to set the tag context, and then call setparent () to set the tag's parent tag. If not, set it to null.
2) Call the setxxx () method of the tag processor to set the tag attributes (skip this step without attributes)
3) Call the dostarttag () method and return an eval_body_include or skip_body. If the former is returned, the label is output to the current output stream. If the latter is returned, the label body is ignored.
4) Call the doendtag () method and return eval_page or skip_page. If you are eager to execute the remaining part of the JSP page, the latter indicates that the remaining part of the JSP page is omitted.
5) The container caches the tag processor instance. Once the same tag is encountered, the cached Tag Processor instance is reused.
6) Call the release () method to release a tag processor instance.
Iterationtag Interface
The javax. servlet. jsp. tagext. iterationtag interface inherits from the tag interface. It adds a method and a constant used as the return value to control repeated processing of the TAG body. The Declaration cycle of the Tag Processor Implementing this interface is as follows:
The first three steps are the same as the tag processor that implements the tag interface.
4) after the label body is specified, the doafterbody () method is called. This method returns eval_body_again or skip_body. If the former is returned, the TAG body is repeatedly executed. If the latter is returned, the TAG body is no longer executed. Note that the label body has been executed once before the method is executed. To ignore the TAG body, you must return the skip_body in the dostarttag () method.
The last three steps are the same as those of the tag processor.
Bodytag Interface
The javax. servlet. jsp. tagext. bodytag interface inherits from the iterationtag interface, and adds two methods and a constant used as the return value. The lifecycle of the tag processor that implements this interface is as follows:
The first three steps are the same as the label processor that implements the iterationtag interface.
4) Call the setbodycontent () method to set the bodycontent attribute of the tag processor, and then call the doinitbody () method to prepare for the execution of the TAG body.
The last four steps are the same as those of the label processor that implements the iterationtag interface.
Tag library Descriptor
After the tag processor class is compiled, You need to configure the tag information in the tag library descriptor file to use the tag.Tag library Descriptor (TLD) is an XML document. The extension used by the tag library descriptor file is ". TLD ". When the tag library is deployed in the jar file, the tag library descriptor file must be placed in either the META-INF directory or its subdirectory. When a tag library is deployed directly in a web application, the tag library descriptor file must be placed in the WEB-INF directory or its subdirectories, but not in the WEB-INF \ Classes or WEB-INF \ lib directory.
<Taglib> an element is the root element of the tag library descriptor. It contains 13 sub-elements. The specific content is 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.
(13) <taglib-extension> contains one or more <extension-element> elements to provide extension information for the tag library.
The content of the custom tag library is described above. Now we can use some instances to deepen our understanding.
Create a project testtag, as described above.
A custom <Hello> label is used to output welcome information to users. First, write the label processor class. The Code is as follows:
Package COM. shan. tag; import Java. io. *; import javax. servlet. JSP. *; import javax. servlet. JSP. tagext. *; public class hellotag implements tag {private pagecontext; private tag parent; Public void setpagecontext (pagecontext PC) {This. pagecontext = pc;} public void setparent (TAG t) {This. parent = T;} public tag getparent () {return parent;} public int dostarttag () throws jspexception {return skip_bod Y;} public int doendtag () throws jspexception {// use the getout () method of the pagecontext object to obtain the jspwriter object jspwriter out = pagecontext. getout (); try {// use the jspwriter object to send a welcome message out to the client. println ("Welcome to tag's world! ");} Catch (ioexception e) {system. Out. println (E. tostring ();} return eval_page;} public void release (){}}
Switch to the project directory in the DOS environment and execute the following statement to compile the testtag. Java file.
javac -classpath D:\apache-tomcat-7.0.33\lib\jsp-api.jar;classes -d classes src\com\shan\tag\HelloTag.java
After compilation, the corresponding. Class file is generated in the classes \ com \ Shan \ tag folder under the project directory.
Configure the <Hello> tag in the TLD File
Create the mytaglib. TLD file with the following content:
<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"><taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>mytag</short-name> <display-name>MyTag</display-name> <description>My Tag library.</description> <tag> <name>hello</name> <tag-class>com.shan.tag.HelloTag</tag-class> <description> Display JSP sources </description> <body-content>empty</body-content> </tag></taglib>
Edit the Web. xml file. The content is as follows:
<?xml version='1.0' encoding='utf-8'?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"><jsp-config><taglib><taglib-uri>/mytag</taglib-uri><taglib-location>/WEB-INF/MyTaglib.tld</taglib-location></taglib></jsp-config></web-app>
Compile a test. jsp test page with the following content:
<%@ page contentType="text/html;charset=gb2312" %><%@taglib uri="/mytag" prefix="mytag"%>
Create the testtag directory under the webapps directory under the tomcat installation folder, create a WEB-INF folder in it, the compiled classes directory and mytaglib. TLD file, Web. copy the XML file to the WEB-INF folder and. the directory testtag that copies JSP files. Open the Tomcat server and enter localhost: 8080 \ testtag \ test. jsp in the URL bar. The running result is as follows:
Reprinted please indicate the source: http://blog.csdn.net/iAm333