When creating JSP-Tags I create sometimes an attribute "id" to set the element-id of the element created. For that id I also create a setter. Now I've found out that TagSupport already has an attribute "id" with it's dedicated setter and I override that method.
Until now it didn't have negative impact on my applications, but can anybody tell me what that id is for and what could go bad when overriding it?
Interesting question
If you look into source code of TagSupport you will find that the original setId/getId methods are just getters and setters:
protected String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
In that class there is only one method which directly calls id field:
public void release() {
parent = null;
id = null;
if( values != null ) {
values.clear();
}
values = null;
}
This method comes from Tag interface:
public interface Tag extends JspTag {
...
/**
* Called on a Tag handler to release state.
* The page compiler guarantees that JSP page implementation
* objects will invoke this method on all tag handlers,
* but there may be multiple invocations on doStartTag and doEndTag in between.
*/
void release();
...
}
If we assume that other internal classes use get/set methods (instead field acces) to acces this id property then there is only one place to worry about (this release method).
The JSP specifications state that : "The JSP container may reuse classic tag handler instances for multiple occurrences of the corresponding custom action, in the same page or in different pages, but only if the same set of attributes are used for all occurrences."
I think that the only case that can cause some problems is when you have two tags one with id and other without id and container reuse instances of tag.
Test case:
Tag class:
public class TestTag extends TagSupport {
protected String id;
static int count = 0;
int instanceNuber=0;
public TestTag() {
super();
System.out.println("TestTag.TestTag() - new Instance");
instanceNuber = ++count;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print("id = " + getIdWrapper());
} catch (IOException e) {
}
return (SKIP_BODY);
}
public String getIdWrapper() {
return getId();
}
@Override
public String getId() {
System.out.println("Instance " + instanceNuber + " of TestTag.getId() = " + id);
return id;
}
@Override
public void setId(String id) {
System.out.println("Instance " + instanceNuber + " of TestTag.setId(" + id + ")");
this.id = id;
}
}
JSP:
<my:test id="dog"/>
<br/>
<my:test/>
<br/>
<my:test id="cat"/>
<br/>
<my:test/>
<br/>
Prints:
id = dog
id = null
id = cat
id = null
I tested this under tomcat 7 and clonsole output is:
TestTag.TestTag() - new Instance <- new instance for first occurance of tag with id
Instance 1 of TestTag.setId(dog)
Instance 1 of TestTag.getId() = dog
TestTag.TestTag() - new Instance <- new instance for first occurance of tag without id
Instance 2 of TestTag.getId() = null
Instance 1 of TestTag.setId(cat) <- reuse of instance 1
Instance 1 of TestTag.getId() = cat
Instance 2 of TestTag.getId() = null <- reuse of instance 2
Answering your question
It looks like tomcat reuse classic tag handler instances only if the same set of attributes are used for all occurrences. Other containers may reuse instances for all tags on the same page no matter how set of attributes looks like.
So under Tomcat there is no negative impact on your application because you have 2 instances (in my case). One for tags with id and another for the same tag but with no id (one instance of tag for given set of attributes). So there is no worry that some tag on page will inherit some "old" id from previously used instance.
But on other container instead of output:
id = dog
id = null
id = cat
id = null
You can get:
id = dog
id = dog
id = cat
id = cat
So if your code depends on those ids. Overriding this setId/getId methods may cause problems on some containers and hard to find bugs.
So dont overide this setId/getId methods.
One more thing. Dont use clasic tags, this is old API. Use SimpleTagSupport.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With