《Javaweb---用自定义标签在浏览器中输出hello world!》
//com.fenghuo.tag.TagTest.java
package com.fenghuo.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class TagTest extends SimpleTagSupport {
//继承SimpleTagSupport
//覆盖里面的doTag方法,在方法内做相应的处理,即可实现不同的功能
@Override
public void doTag() throws JspException, IOException {
//向浏览器输出字符串
this.getJspContext().getOut().write("hello world!");
super.doTag();
}
}
<!--tld配置文件-->
<?xml version="1.0" encoding="UTF-8" ?>
<taglib 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-jsptaglibrary_2_1.xsd"
version="2.1">
<description>fenghuo 1.1 core library</description>
<display-name>fenghuo</display-name>
<tlib-version>1.0</tlib-version>
<short-name>fh</short-name>
<!-- 标签的uri,指向下面定义的各种标签 -->
<uri>/fenghuo/com/fenghuotag</uri>
<tag>
<!-- 标签名 -->
<name>hw</name>
<!-- 标签对应执行的java类 -->
<tag-class>com.fenghuo.tag.TagTest</tag-class>
<!-- 是否存在标签体 -->
<body-content>empty</body-content>
</tag>
</taglib>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 引入自定义的标签 -->
<%@taglib uri="/fenghuo/com/fenghuotag" prefix="fh"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>测试自定义标签</title>
</head>
<body>
<!-- 自定义的标签 -->
<fh:hw/>
</body>
</html>