JSP caching lets you cache tag invocation results within the Java engine. Each can be cached using different cache criteria. For example, suppose you have invocations to view stock quotes, weather information, and so on. The stock quote result can be cached for 10 minutes, the weather report result for 30 minutes, and so on.
For more information about response caching as it pertains to servlets, see Caching Servlet Results.
JSP caching is implemented by a tag library packaged into the install-dir/lib/appserv-tags.jar file, which you can copy into the WEB-INF/lib directory of your web application. The appserv-tags.tld tag library descriptor file is in the META-INF directory of this JAR file.
Web applications that use this tag library are not portable.
To allow all web applications to share this tag library, change the following elements in the domain.xml file. Change this:
<jvm-options> -Dcom.sun.enterprise.taglibs=appserv-jstl.jar,jsf-impl.jar </jvm-options>
to this:
<jvm-options> -Dcom.sun.enterprise.taglibs=appserv-jstl.jar,jsf-impl.jar,appserv-tags.jar </jvm-options>
and this:
<jvm-options> -Dcom.sun.enterprise.taglisteners=jsf-impl.jar </jvm-options>
to this:
<jvm-options> -Dcom.sun.enterprise.taglisteners=jsf-impl.jar,appserv-tags.jar </jvm-options>
For more information about the domain.xml file, see the Sun Java System Application Server Enterprise Edition 8.2 Administration Reference.
Refer to these tags in JSP files as follows:
<%@ taglib prefix="prefix" uri="Sun ONE Application Server Tags" %>
Subsequently, the cache tags are available as <prefix:cache> and <prefix:flush>. For example, if your prefix is mypfx, the cache tags are available as <mypfx:cache> and <mypfx:flush>.
The tags are as follows:
The cache tag 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 it is run, the cached content is checked to see if it needs to be refreshed and if so, it is executed again, and the cached data is refreshed. Otherwise, the cached data is served.
The following table describes attributes for the cache tag.
Table 5–2 cache Attributes
Attribute |
Default |
Description |
---|---|---|
ServletPath_Suffix |
(optional) The name used by the container to access the cached entry. The cache key is suffixed 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. |
|
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, d for days. For example, 2h specifies two hours. |
|
false |
(optional) If set to true, the body content is executed and served as if there were no cache tag. This offers a way to programmatically decide whether the cached response is sent or whether the body has to be executed, though the response is not cached. |
|
false |
(optional) If set to true, the body content is executed and the response is cached again. This lets you programmatically refresh the cache immediately regardless of the timeout setting. |
The following example represents a cached JSP file:
<%@ taglib prefix="mypfx" uri="Sun ONE Application Server Tags" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <mypfx:cache key="${sessionScope.loginId}" nocache="${param.nocache}" refresh="${param.refresh}" timeout="10m"> <c:choose> <c:when test="${param.page == 'frontPage'}"> <%-- get headlines from database --%> </c:when> <c:otherwise> ... </c:otherwise> </c:choose> </mypfx:cache> <mypfx:cache timeout="1h"> <h2> Local News </h2> <%-- get the headline news and cache them --%> </mypfx:cache>
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.
The following table describes attributes for the flush tag.
Table 5–3 flush Attributes
Attribute |
Default |
Description |
---|---|---|
ServletPath_Suffix |
(optional) The name used by the container to access the cached entry. The cache key is suffixed 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. |
To flush the entry with key="foobar":
<mypfx:flush key="foobar"/>
To flush the entire cache:
<c:if test="${empty sessionScope.clearCache}"> <mypfx:flush /> </c:if>
Application Server provides the following ways of compiling JSP 2.0 compliant source files into servlets:
JSP files are automatically compiled at runtime.
The asadmin deploy command has a precompilejsp option. For details, see the Sun Java System Application Server Enterprise Edition 8.2 Reference Manual.
The sun-appserv-jspc Ant task allows you to precompile JSP files; see sun-appserv-jspc.
The jspc command line tool allows you to precompile JSP files at the command line. For details, see the Sun Java System Application Server Enterprise Edition 8.2 Reference Manual.