Sun Java System Web Server 7.0 Developer's Guide to Java Web Applications

JSP Cache Tags

JSP cache tags enable you to cache JSP page fragments within the Java engine. Each fragment can be cached using different cache criteria. For example, suppose you have page fragments to view stock quotes and weather information. The stock quote fragment can be cached for 10 minutes, the weather report fragment for 30 minutes, and so on.

For more information about response caching as it pertains to servlets, see Caching Servlet Results.

JSP caching uses the custom tag library support provided by JSP 2.0. JSP caching is implemented by a tag library packaged into the install_dir/lib/.jar file, which is always on the server classpath. The sun-web-cache.tld tag description file is in the install_dir/lib/tlds directory and can be copied into the WEB-INF directory of your web application.

JSP caching is available in three different scopes: request, session, and application. The default is application.

To use a cache with the request scope, a web application must specify the com.sun.appserv.web.taglibs.cache.CacheRequestListener in its deployment descriptor, as follows:

<listener>
   <listener-class>
      com.sun.appserv.web.taglibs.cache.CacheRequestListener
   </listener-class>
</listener>
  

To use a cache in session scope, a web application must specify the com.sun.appserv.web.taglibs.cache.CacheSessionListener in its deployment descriptor, as follows:

<listener>
   <listener-class>
      com.sun.appserv.web.taglibs.cache.CacheSessionListener
   </listener-class>
</listener>
  

To use a cache with the application scope, a web application does not need to specify any listener. The com.sun.appserv.web.taglibs.cache.CacheContextListener is already specified in the sun-web-cache.tld file.

The JSP cache tags are as follows:

cache Tag

The cache tag enables you to cache fragments of your JSP pages. It caches the body between the beginning and ending tags according to the attributes specified. The first time the tag is encountered, the body content is executed and cached. Each subsequent time is run, the cached content is checked to see whether it needs to be refreshed. If so, it is executed again, and the cached data is refreshed. Otherwise, the cached data is served.

Attributes

The following table describes attributes for the cache tag.

Table 5–3 cache Attributes

Attribute  

Default  

Description  

key

servletPath_suffix

(Optional) The name used by the container to access the cached entry. The cache key is added to the servlet path to generate a key to access the cached entry. If no key is specified, a number is generated according to the position of the tag in the page. 

timeout

60s

(Optional) The time in seconds after which the body of the tag is executed and the cache is refreshed. By default, this value is interpreted in seconds. To specify a different unit of time, add a suffix to the timeout value as follows: s for seconds, m for minutes, h for hours, and d for days. For example, 2h specifies two hours.

nocache

false

(Optional) If set to true, the body content is executed and served as if no cache tag is active. This offers a way to programmatically decide whether the cached response should be sent or whether the body must be executed, even if the response is not cached.

refresh

false

(Optional) If set to true, the body content is executed and the response is cached again. This setting enables you to lets you programmatically refresh the cache immediately, regardless of the timeout setting.

scope

application

Specifies the scope of the cache. Valid values are request, session, and application.

Example

The following example represents a cached JSP page.

<%@ taglib prefix="mypfx" uri="/com/sun/web/taglibs/cache" %>
			<%
   		String cacheKey = null;
    		if (session != null)
      	cacheKey = (String)session.getAttribute("loginId");
   		// check for nocache
			   boolean noCache = false;
   		String nc = request.getParameter("nocache");
   		if (nc != null)
      	noCache = "true";
			   // force reload
			   boolean reload=false;
			   String refresh = request.getParameter("refresh");
			   if (refresh != null)
     		reload = true;
			 %>
			 <mypfx:cache key="<%= cacheKey %/>" nocache="<%= noCache %/>" 
       refresh="<%= reload %/>" timeout="10m"/>
			<%
		  	   String page = request.getParameter("page");
   		if (page.equals("frontPage") {
       	// get headlines from database
		    } else {
       .....
			 %>
				</mypfx:cache/>
							<mypfx:cache timeout="1h"/>
				<h2> Local News </h2>
			<%
			   // get the headline news and cache them
			%>
				</mypfx:cache/>

flush Tag

This tag forces the cache to be flushed. If a key is specified, only the entry with that key is flushed. If no key is specified, the entire cache is flushed.

Attributes

The following table describes attributes for the flush tag. The left column lists the attribute name, the middle column indicates the default value, and the right column describes what the attribute does.

Table 5–4 flush Attributes

Attribute  

Default  

Description  

key

servletPath_suffix

(Optional) The name used by the container to access the cached entry. The cache key is added to the servlet path to generate a key to access the cached entry. If no key is specified, a number is generated according to the position of the tag in the page. 

scope

application

Specifies the scope of the cache. Valid values are request, session, and application.

Examples

To flush the entry with key="foobar":


<mypfx:flush key="foobar"/>

         

To flush the entire cache:


<% if (session != null && session.getAttribute("clearCache") != null) { %/>
     <mypfx:flush />
 <% } %/>