开发者

how to prevent jsp tags from being reused after being classloaded

I have a problem where certain attributes in tag files stick around for the next time the tag is used.

I think this is because the Tag class is being classloaded, and then that same instance is reused for every invocation. So attributes that i do not set in later invocations are not null like i would expect them t开发者_运维知识库o be, and contain stale values!

I want this to not happen any more. Does anyone know what setting controls that in tomcat 6?


Tomcat 7.0 uses tag pooling:

http://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.html

JSP Custom Tag Pooling - The java objects instantiated for JSP Custom Tags can now be pooled and reused. This significantly boosts the performance of JSP pages which use custom tags.

That page also says that web.xml can contain an "enablePooling" option for that, and that its default value is true.

So I would say that disabling tag reuse is not a good idea as it would lead to some performance loss.

Tomcat 7.0 guarantees that the state of the tag class will remains unchanged between doStartTag() and doEndTag():

http://tomcat.apache.org/tomcat-7.0-doc/jspapi/javax/servlet/jsp/tagext/Tag.html

doStartTag and doEndTag methods can be invoked on the tag handler. Between these invocations, the tag handler is assumed to hold a state that must be preserved

But the same paragraph also says between parenthesis that the object is expected to have its properties retained after:

After the doEndTag invocation, the tag handler is available for further invocations (and it is expected to have retained its properties).

So what I do is reset all local variables to their default value just before doEndTag() returns. I found no explaination on how Tomcat tag pooling and reusing is implemented (TagHandlerPool.java I guess) so I think it is the most secure option.


You need to clear the state of the tag between the calls. You should do that in the doEndTag() method of your class, just before returning. At that point you should set all the state variables to null.


In fact, only one tag instance is been created everytime. Maybe you declared the attributes static?


Maybe a little late, but I had the same problem. It raises when I set a Tag-attributes with a null-value to a value. Changing a value doesnt provide any error, only seting.

So I thing the implementation for tag-reusing does something like remember which attributes were set and unset those after the tag finished its work. If you set that attribute in your tag-code the tag-pool doesnt know to reset taht attrbiute and it keeps its value.

Not sure if thats true, but it fits to my observations

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜