gpt4 book ai didi

java - 如何防止jsp标签在类加载后被重用

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:57:33 24 4
gpt4 key购买 nike

我遇到了一个问题,即标签文件中的某些属性在下次使用标签时仍然存在。

我认为这是因为正在加载 Tag 类,然后每次调用都会重复使用同一个实例。因此,我没有在以后的调用中设置的属性并不像我期望的那样为 null,并且包含陈旧的值!

我希望这种事情不再发生。有谁知道在 tomcat 6 中是什么设置控制的?

最佳答案

Tomcat 7.0 使用标签池:

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.

该页面还说 web.xml 可以为此包含一个“enablePooling”选项,并且它的默认值为 true。

所以我想说禁用标签重用不是一个好主意,因为它会导致一些性能损失。

Tomcat 7.0保证标签类的状态在doStartTag()和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

但同一段话还在括号中说,该对象预计将在以下时间后保留其属性:

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

所以我所做的是在 doEndTag() 返回之前将所有局部变量重置为其默认值。我没有找到关于如何实现 Tomcat 标签池和重用的解释(我猜是 TagHandlerPool.java),所以我认为这是最安全的选择。

关于java - 如何防止jsp标签在类加载后被重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2481159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com