JSP tags replace Java programs in JSP and can be reused to facilitate web designers who are unfamiliar with Java programming.
First create a new Dynamic Web project under Eclipse
1, join
before </web-app>
<jsp-config>
<taglib>
<taglib-uri>/tld/helloworld</taglib-uri>
<taglib-location>/WEB-INF/tlds/helloworld.tld</taglib-location>
</taglib>
</jsp-config>
2. Under Web-inf, create a new TLDs directory, under TLDs new helloworld.tld.helloworld.tld content for
<?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/dtd/web-jsptaglibrary_1_2.dtd" >
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>mytag</short-name>
<tag>
<name>helloworld</name>
<tag-class>mytag. Helloworldtag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
3. New Class Helloworldtag.java
under MyTag package of SRC
package mytag;
}
public int doStartTag () throws jsptagexception ... {
return eval_body_include;
}
public int Doendtag () throws jsptagexception ... {
Try ... {
pagecontext.getout (). Write ("This is a JSP tag test!");
}catch (IOException ex) ... {
throw new Jsptagexception ("error");
}
return eval_page;
}
}
test page hello.jsp
import java.io.IOException;
import javax.servlet.jsp.JspTagException;
import Javax.servlet.jsp.tagext.TagSupport;
public class Helloworldtag extends TagSupport ... { public helloworldtag () ...
<%...@ page language= "java" contenttype= "text/html; charset=gb2312"
< pageencoding= "gb2312"%>
<%...@ page import= "java.util.*"%>
<%...@ page import= "jstl.*%> " <%...@ taglib prefix= "MyTag" uri= "/tld/helloworld"%>< /c8>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312" >
<title>insert title here</title>
<body>
<br>
<br>
<mytag:helloworld></mytag:helloworld>
<br>
</form>
</body>