Does Tomcat create new instances of a Custom Tag every time ?

Discussions

Web tier: servlets, JSP, Web frameworks: Does Tomcat create new instances of a Custom Tag every time ?

  1. Hi all,
    I'm facing a big doubt.
    I have defined several Custom Tag libraries deployed on Tomcat 5.0.16.
    In some of this tags, I use class instance variables.

    For example I have an instance variable that controls the style
    of the page depending on the jsp name:

    public class BaseCustomTag extends javax.servlet.jsp.tagext.TagSupport
                                 {

        // Any race condtion for this variable ?????????
        Style tagStyle = StyleManager.getStyle(getJspName());
        .....
        
         public int doStartTag() throws JspException {
            ....use tagStyle
         }
        
    }

    now the matter is: will Tomcat instantiate a custom tag every time
    that the page is invoked ?
    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."

    From some raw testing I have observed that EVERY time the jsp page is invoked a new
    instance of the tag is ALWAYS created.

    Does anybody know it for sure ?????????

    As this software will be mission-critical (in fact for a Bank)
    I must be 100% sure I won't meet any Thread-related problem with global variables.
    Thanks a lot
    Francesco
  2. I think generally, it is no good idea to use instance variables in tags, since the servlet container may or may not pool tag instances. Some, for example WebSphere (when I last worked with it about two years ago), create new instances on every occurence of a tag, others like Orion and Tomcat, keep a pool of tag instances which will be reused. So you even might have several instances of a tag, even though they are cached.

    Perhaps you better consider keeping your "global" variables (user's style settings and such) within the user's session or within the application in case they're not user specific.

    Regards,
    Thomas
  3. Hi
    Is safe to use instance variables inside the taglib between the doStartTag() and doEndTag() method invocations. The servlet container guarantees that the doStartTag() and doEndTag() will be called in this order by the same thread. When the doEndTag() mothod ends, the taglib instance must be left in a clean state, so that it can be further used in another doStartTag() -- doEndTag() cycle. Although not recomended, you can disable tag instance pooling in Tomcat by setting this in your Tomcat/conf/web.xml file:
     <servlet>
            <servlet-name>jsp</servlet-name>
            <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
             ...............
            <init-param>
                <param-name>enablePooling</param-name>
                <param-value>false</param-value>
            </init-param>

    Best regard, Mircea
  4. I could observe the same behaviour. Calling the release() method at the end of the doEndTag()-method ensure that the tags are reused correct. All other behaviour of Tomcat was from my point of view not guranteed, which results in funny behaviour.
    This did't happen for tomcat 4.1.X!