Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 to be, and contain stale values!

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

like image 224
mkoryak Avatar asked Mar 19 '10 23:03

mkoryak


People also ask

What is JSP tag pooling?

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.

Are custom tags available in JSP?

A custom tag is a user-defined JSP language element. When a JSP page containing a custom tag is translated into a servlet, the tag is converted to operations on a tag handler. The web container then invokes those operations when the JSP page's servlet is executed.

What is the purpose of JSP tag extensions?

JSP tag extensions lets you create new tags that you can insert directly into a JavaServer Page. The JSP 2.0 specification introduced the Simple Tag Handlers for writing these custom tags.


4 Answers

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.

like image 170
Walid Avatar answered Oct 06 '22 04:10

Walid


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.

like image 24
Roland Illig Avatar answered Oct 06 '22 04:10

Roland Illig


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

like image 28
BalusC Avatar answered Oct 06 '22 04:10

BalusC


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

like image 26
mibutec Avatar answered Oct 06 '22 05:10

mibutec