jsp标签取代了JSP中的Java程序并且可以重复使用方便不熟悉Java编程的网页设计人员
首先在eclipse下新建一个动态web工程
在</web
app>之前加入
<jsp
config>
<taglib>
<taglib
uri>/tld/helloworld</taglib
uri>
<taglib
location>/WEB
INF/tlds/helloworld
tld</taglib
location>
</taglib>
</jsp
config>
在WEB
INF下新建tlds目录
在tlds下新建helloworld
tld
helloworld
tld的内容为
<?xmlversion="
"encoding="ISO
"?>
<!DOCTYPEtaglibPUBLIC"
//SunMicrosystems
Inc
//DTDJSPTagLibrary
//EN"
"
<taglib>
<tlib
version>
</tlib
version>
<jsp
version>
</jsp
version>
<short
name>mytag</short
name>
<tag>
<name>helloworld</name>
<tag
class>mytag
HelloWorldTag</tag
class>
<body
content>empty</body
content>
</tag>
</taglib>
在src的mytag包下新建类HelloWorldTag
java
packagemytag;
}
publicintdoStartTag()throwsJspTagException
{
returnEVAL_BODY_INCLUDE;
}
publicintdoEndTag()throwsJspTagException
{
try
{
pageContext
getOut()
write("Thisisajsptagtest!");
}catch(IOExceptionex)
{
thrownewJspTagException("错误");
}
returnEVAL_PAGE;
}
}
测试页面hello
jsp
importjava
io
IOException;
importjavax
servlet
jsp
JspTagException;
importjavax
servlet
jsp
tagext
TagSupport;
publicclassHelloWorldTagextendsTagSupport
{publicHelloWorldTag()
<%
@pagelanguage="java"contentType="text/html;charset=gb
"
<pageEncoding="gb
"%>
<%
@pageimport="java
util
*"%>
<%
@pageimport="jstl
*"%><%
@taglibprefix="mytag"uri="/tld/helloworld"%>
<!DOCTYPEhtmlPUBLIC"
//W
C//DTDHTML
Transitional//EN""
<html>
<head>
<metahttp
equiv="Content
Type"content="text/html;charset=gb
">
<title>Inserttitlehere</title>
</head>
<body>
<h
>自定义标签:</h
>
<br>
<br>
<mytag:helloworld></mytag:helloworld>
<br>
</form>
</body>
</html>