tag to write a tag involves three steps,
(1) Write JSP
(2) Write tag Java program
(3) Write tag library description file TLD (actually an XML file)
There is no sequential constraint between these three steps, and here is a simple example:
1 Writing hellotag.jsp
<%@page contentType="text/html"%>
<body>
<%@ taglib uri="/WEB-INF/classes/tags/helloTag.tld" prefix="hello" %>
</body>
2 Write Tag
Hellotag.java
package tags; //注意:必须放在一个包中
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class HelloTag extends TagSupport {
public int doStartTag() {
try {
//使用JspWriter获得JSP的输出对象
JspWriter jspWriterOutput = pageContext.getOut();
jspWriterOutput.print("Hello Tag!");
} catch (IOException ioEx) {
System .out.println("IOException in HelloTag " + ioEx);
}
return (SKIP_BODY);
}
}
3 Writing Hellotag.tld
This is the description section of the tag library:
<?xml version= "1.0" encoding= "UTF-8"
<! DOCTYPE taglib
Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.2//en"
"http://java.sun.com/dtd/ Web-jsptaglibrary_1_2.dtd "
<taglib>
<tlib-version>1.0</tlib-version>
< Jsp-version>1.2</jsp-version>
<shorttag-name>hellotag</short-name>
<uri>/ Web-inftagshellotag</uri>
<display-name>hellotag</display-name>
<small-icon>< /small-icon>
<large-icon></large-icon>
<description>simple Hello tags tag
</ Description>
<tag>
<name>hellotag</name>
<tag-class >tags. Hellotag</tag-class
<body-content>empty</body-content>
<small-icon></ Small-icon>
<large-icon></large-icon>
<description></description>
< Example></example>
</tag>
</taglib>
4 Note:
The XML file is usually written manually, but the Sun Tutorial recommends using the IDE tool to write custom tags, such as NetBeans
In general, put the TLD file directly into the Web-inf directory.