JSP labels replace Java programs in JSP and can be reused to facilitate web designers who are not familiar with Java programming.
First, create a dynamic web project under eclipse
1. Before </Web-app>, add
<JSP-config>
<Taglib>
<Taglib-Uri>/TLD/helloworld </taglib-Uri>
<Taglib-location>/WEB-INF/TLDs/helloworld. TLD </taglib-location>
</Taglib>
</JSP-config>
2. Create a TLDs directory under the WEB-INF and create helloworld. TLD. helloworld. TLD under TLDs
<? 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. Create the class helloworldtag. javapackage mytag under the SRC mytag package;
Import java. Io. ioexception;
Import javax. servlet. jsp. jsptagexception;
Import javax. servlet. jsp. tagext. tagsupport;
Public class helloworldtag extends tagsupport ...{
Public helloworldtag ()...{
}
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 <%... @ page Language = "Java" contenttype = "text/html; charset = gb2312"
Pageencoding = "gb2312" %>
<%... @ Page import = "Java. util. *" %>
<%... @ Page import = "jstl. *" %>
<%... @ Taglib prefix = "mytag" uri = "/TLD/helloworld" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> insert title here </title>
</Head>
<Body>
<H1> custom tag: <Br>
<Br>
<Mytag: helloworld> </mytag: helloworld>
<Br>
</Form>
</Body>
</Html>