Class CacheTag

  • All Implemented Interfaces:
    jakarta.servlet.jsp.tagext.BodyTag, jakarta.servlet.jsp.tagext.IterationTag, jakarta.servlet.jsp.tagext.JspTag, jakarta.servlet.jsp.tagext.Tag, Serializable

    public class CacheTag
    extends jakarta.servlet.jsp.tagext.BodyTagSupport
    CacheTag is a JSP tag that allows server-side caching of JSP page fragments. It lets you specify a timeout for how long the cached data is valid. It also gives you programmatic control over key generation, refreshing of the cache and whether the cached content should be served or not. Usage Example: <%@ taglib prefix="ias" uri="Sun ONE Application Server Tags" %> ... expensive operation ...
    See Also:
    Serialized Form
    • Field Summary

      • Fields inherited from class jakarta.servlet.jsp.tagext.BodyTagSupport

        bodyContent
      • Fields inherited from class jakarta.servlet.jsp.tagext.TagSupport

        id, pageContext
      • Fields inherited from interface jakarta.servlet.jsp.tagext.BodyTag

        EVAL_BODY_BUFFERED, EVAL_BODY_TAG
      • Fields inherited from interface jakarta.servlet.jsp.tagext.IterationTag

        EVAL_BODY_AGAIN
      • Fields inherited from interface jakarta.servlet.jsp.tagext.Tag

        EVAL_BODY_INCLUDE, EVAL_PAGE, SKIP_BODY, SKIP_PAGE
    • Constructor Summary

      Constructors 
      Constructor Description
      CacheTag()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int doAfterBody()
      doAfterBody is called only if the body was evaluated.
      int doEndTag()
      doEndTag just resets all the valiables in case the tag is reused
      int doStartTag()
      doStartTag is called every time the cache tag is encountered.
      void setKey​(String key)
      This is used to set a user-defined key to store the response in the cache.
      void setNocache​(boolean noCache)
      This attribute is used to programmatically enable or disable the use of the cached response.
      void setRefresh​(boolean refresh)
      This attribute is used to programmatically refresh the cached response.
      void setScope​(String scope)
      Sets the scope of the cache.
      void setTimeout​(String timeout)
      This sets the time for which the cached response is valid.
      • Methods inherited from class jakarta.servlet.jsp.tagext.BodyTagSupport

        doInitBody, getBodyContent, getPreviousOut, release, setBodyContent
      • Methods inherited from class jakarta.servlet.jsp.tagext.TagSupport

        findAncestorWithClass, getId, getParent, getValue, getValues, removeValue, setId, setPageContext, setParent, setValue
      • Methods inherited from interface jakarta.servlet.jsp.tagext.Tag

        getParent, setPageContext, setParent
    • Constructor Detail

      • CacheTag

        public CacheTag()
    • Method Detail

      • doStartTag

        public int doStartTag()
                       throws jakarta.servlet.jsp.JspException
        doStartTag is called every time the cache tag is encountered. By the time this is called, the tag attributes are already set, but the tag body has not been evaluated. The cache key is generated here and the cache is obtained as well
        Specified by:
        doStartTag in interface jakarta.servlet.jsp.tagext.Tag
        Overrides:
        doStartTag in class jakarta.servlet.jsp.tagext.BodyTagSupport
        Returns:
        EVAL_BODY_INCLUDE when nocache is specified so that the tag body is just evaluated into the output stream SKIP_BODY if the cached response is valid in which case it is just written to the output stream, hence there is nothing more to be done. EVAL_BODY_BUFFERED is the default return value which ensures that the BodyContent is created and the tag body is evaluated into it.
        Throws:
        jakarta.servlet.jsp.JspException - the standard exception thrown
      • doAfterBody

        public int doAfterBody()
                        throws jakarta.servlet.jsp.JspException
        doAfterBody is called only if the body was evaluated. This would happen if nocache is specified in which case this should do nothing if there was no cached response in which case the response data is obtained from the bodyContent and cached if the response has expired in which case the cache is refreshed
        Specified by:
        doAfterBody in interface jakarta.servlet.jsp.tagext.IterationTag
        Overrides:
        doAfterBody in class jakarta.servlet.jsp.tagext.BodyTagSupport
        Returns:
        always returns SKIP_BODY since we dont do any iteration
        Throws:
        jakarta.servlet.jsp.JspException - the standard exception thrown
      • doEndTag

        public int doEndTag()
                     throws jakarta.servlet.jsp.JspException
        doEndTag just resets all the valiables in case the tag is reused
        Specified by:
        doEndTag in interface jakarta.servlet.jsp.tagext.Tag
        Overrides:
        doEndTag in class jakarta.servlet.jsp.tagext.BodyTagSupport
        Returns:
        always returns EVAL_PAGE since we want the entire jsp evaluated
        Throws:
        jakarta.servlet.jsp.JspException - the standard exception thrown
      • setKey

        public void setKey​(String key)
        This is used to set a user-defined key to store the response in the cache.
      • setTimeout

        public void setTimeout​(String timeout)
        This sets the time for which the cached response is valid. The cached entry is invalid after this time is past. If no unit is specified, then the timeout is assumed to be in seconds. A different unit can be specified by postfixing the timeout value with the desired unit: s=seconds, m=minutes, h=hours, d=days
      • setNocache

        public void setNocache​(boolean noCache)
        This attribute is used to programmatically enable or disable the use of the cached response. If noCache is true, then the cached response is not sent, instead the tag body is evaluated and sent out, the cache is not refreshed either.
      • setRefresh

        public void setRefresh​(boolean refresh)
        This attribute is used to programmatically refresh the cached response. If refresh is true, the cached response is not sent, instead the tag body is evaluated and sent and the cache is refreshed with the new response.
      • setScope

        public void setScope​(String scope)
        Sets the scope of the cache.
        Parameters:
        scope - the scope of the cache
        Throws:
        IllegalArgumentException - if the specified scope is different from request, session, and application