1.编写一个普通的java类,继承SimpleTagSupport类(标签处理器)
public class showIpTag extends SimpleTagSupport {
//1
// private JspContext context;
//
// @Override
// public void setJspContext(JspContext pc) {
// this.context = pc;
// }
@Override
public void doTag() throws JspException, IOException {
PageContext page = (PageContext)this.getJspContext();
HttpServletRequest request = (HttpServletRequest)page.getRequest();
String ip = request.getRemoteHost();
JspWriter out = page.getOut();
out.write(ip);
}
}
2.在web项目的目录下WEB-INF建立itcast.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">
<!-- 标签库版本 -->
<tlib-version>1.1</tlib-version>
<!-- 标签库前缀 -->
<short-name>c</short-name>
<!-- tld文件的唯一标识 -->
<uri>http://java.sun.com/jsp/jstl/core</uri>
<!--标签的声明-->
<tag>
<!--标签名-->
<name>catch</name>
<!--标签处理器类全面(包名+“.”+项目名)-->
<tag-class>org.apache.taglibs.standard.tag.common.core.CatchTag</tag-class>
<!--输出标签体内容格式-->
<body-content>JSP</body-content>
</tag>
</taglib>
3.在jsp页面的头部导入自定义标签库
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
4.在jsp中使用自定义标签
<c:catch></c:catch>