自定义jsp标签步骤:
1、定义标签类:继承TagSupport类,实现doStartTag方法
在该方法中,用pageContext获取request和responst,如下:
HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
用pageContext获取writer对象,往页面写东西,如下:
JspWriter out = pageContext.getOut();
通常我们是写一段拼接html的字符串,然后out.write输出字符串
记得关闭out
2、配置一个tld文件,定义标签的名称,属性,以及实现类
3、在web.xml中,引入标签
如例:
<taglib>
<taglib-uri>authLink</taglib-uri>
<taglib-location>/WEB-INF/taglib/authLinkTag.tld</taglib-location>
</taglib>
4、在页面使用自定义标签:
<%@ taglib uri="authLink" prefix="auth" %>