Share

November 26, 2008

The Java Tag Lifecycle and Data Persistence Bug

If you create your own Java Tag by extending Interface Tag or any of the classes that implent it. Be aware of data persistence between tag usages... The Java VM tend to release the same Tag objects over and over.

Check this out from the Tag JavaDoc

Once all invocations on the tag handler are completed, the release method is invoked on it. Once a release method is invoked all properties, including parent and pageContext, are assumed to have been reset to an unspecified value. The page compiler guarantees that release() will be invoked on the Tag handler before the handler is released to the GC.

so if you declare private Objects in your Tag class like the following

public class MyPageTag extends BodyTagSupport {
    private String pageName; 

make sure you call release() as follows

@Override
public void release() {
  super.release();
  pageName = null;
}