JSP自定义标签的类实例化时难道是单例?

我写的自定义标签的类,继承自TagSupport
jsp页里引入了2次这个标签,可类里就执行了一次构造函数,实例化了一次
自定义标签类是有属性的,如果是单例属性就乱了

请问这是怎么回事

你有没有看过jsp转化为的servlet。无论几个taglib,只要是相同的,最终都会转化为一个方法
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
最终在servlet只转化为了下面一个方法
[code="java"] private boolean _jspx_meth_c_005fout_005f0(PageContext _jspx_page_context)
throws Throwable {
PageContext pageContext = _jspx_page_context;
JspWriter out = _jspx_page_context.getOut();
// c:out
org.apache.taglibs.standard.tag.rt.core.OutTag _jspx_th_c_005fout_005f0 = (org.apache.taglibs.standard.tag.rt.core.OutTag) _005fjspx_005ftagPool_005fc_005fout_0026_005fvalue_005fnobody.get(org.apache.taglibs.standard.tag.rt.core.OutTag.class);
_jspx_th_c_005fout_005f0.setPageContext(_jspx_page_context);
_jspx_th_c_005fout_005f0.setParent(null);
// /index.jsp(21,8) name = value type = null reqTime = true required = true fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null
_jspx_th_c_005fout_005f0.setValue((java.lang.Object) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${param.d}", java.lang.Object.class, (PageContext)_jspx_page_context, null, false));
int _jspx_eval_c_005fout_005f0 = _jspx_th_c_005fout_005f0.doStartTag();
if (_jspx_th_c_005fout_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
_005fjspx_005ftagPool_005fc_005fout_0026_005fvalue_005fnobody.reuse(_jspx_th_c_005fout_005f0);
return true;
}
_005fjspx_005ftagPool_005fc_005fout_0026_005fvalue_005fnobody.reuse(_jspx_th_c_005fout_005f0);
return false;
}[/code]

或许我举错例子了,taglib中只在init方法中生成了一次

同一个类中,就是JSP转译成的Servlet中,一个标签类只会实例化一次,对应到JSP中的概念,就是一个PageContext范围只会有一个标签类实例,虽然你使用了两次同样的标签,
但不同jsp中,有不同的标签类实例,
页面标签的每一次使用,本质上就是标签处理类一组方法的顺序调用,如果像楼主出现的这种情况,就要保证每一个方法的无状态性,不要用可变的实例变量,而是将对象的状态保存到JSP的pageContext中,可以避免第一次标签调用是从初始状态化验室开始,而第二次标签调用不是从初始化状态开始的情况.