Understanding HTTP response caching options  Using the servlet Java cache

Chapter 5: Web Application Tuning

Dynamic page caching

Dynamic page caching decreases a servlet’s or JSP’s response times by caching the output with a multi-part, user-configured key value. This caching mechanism stores responses in their entirety. For pages that return both volatile content and content that rarely changes, use partial page caching instead—see “Using partial page caching”.

When page caching is enabled for a servlet or JSP, EAServer checks the cache before invoking the Web component, looking for an entry that matches the key that you have defined for the servlet or JSP. If an appropriate response is found in the cache, EAServer returns the contents of the cache, instead of calling the servlet. If the cache contains no matching key, EAServer invokes the servlet, and caches the response and response headers while returning them to the client.

You can define the key that EAServer uses to store and retrieve cached entries. By default, a key consists of only the servlet’s or JSP’s location on disk. You can further refine key values by adding up to six optional parameters. Doing so allows caching of separate responses from the same servlet or JSP, based on request characteristics such as locale or HTTP session ID. In addition, you can configure a timeout for cache entries associated with the JSP or servlet to prevent the use of stale data. If you do not require this level of refinement in the cache key, consider using the servlet Java cache instead—see “Using the servlet Java cache”.

Configuring page caching for servlets and JSPs

Use EAServer Manager to enable caching for your servlets and JSPs. To enable caching for JSPs, you must have a Web component defined for the JSP in EAServer Manager. To configure default values for the Web component caching properties, see “Configuring Web application page caching properties”.

StepsTo configure page caching for a servlet or JSP:

  1. Expand the Web application folder and highlight the servlet or JSP.

  2. Choose File | Web Component Properties.

  3. Select the Caching tab.

  4. Check Enable Cache. By default, page caching is disabled.

    NoteThe first time you configure page caching for a Web component, the page caching property values default to the same as those in the Web application.

  5. To replace the current property values with those in the Web application, select Get WebApp Settings.

  6. To include the output from all the pages that the Web component includes or forwards requests to, select Cache Entire Tree. For more information, see “Caching an entire tree”.

  7. Optionally, edit the timeout value, and enter the parameters that you want to use in the key as listed in Table 5-1.

    Table 5-1: Page caching properties



    Cache Timeout

    Enter the number of seconds to keep the Web component’s content in the cache; the default is 600; a value of 0 indicates no timeout. The timeout value is stored in the com.sybase.jaguar.servlet.cache.timeout property.

    Session Local

    To include the session ID in the key, select this option; by default, it is not included. The session ID can identify session specific items, such as shopping carts. The value is saved in the com.sybase.jaguar.servlet.cache.use-sessionid property.

    Locale Sensitive

    Select to include the accept-languages header in the key; by default, it is not included. The value is saved in the com.sybase.jaguar.servlet.cache.locale-sensitive property.

    Request Parameters

    To include request parameters in the key, enter the parameter names as a comma-separated list. To include all the request parameters, select the Enable All box; it is selected by default. A single servlet can produce different responses based on which parameters it receives. A key that includes the request parameters allows different responses from the same servlet to be cached separately. The request parameters are saved in the com.sybase.jaguar.servlet.cache.request-parameters property.

    Session Attributes

    To include session attributes in the key, enter the attributes as a comma-separated list. To include all the session attributes, check the Enable All box. The session attributes list is saved in the com.sybase.jaguar.servlet.cache.session-attributes property.

    Request Headers

    To include request headers in the key, enter the header names as a comma-separated list. For example, if you include the date header, EAServer looks for cache entries whose date headers match the request’s date header. The request headers list is saved in the com.sybase.jaguar.servlet.cache.request-headers property.

    Message Topics

    For a key that includes the number of times a message has been published to a particular topic, enter the message topic. To use this as a key parameter, the message service must be configured; for details, see Chapter 31, “Using the Message Service.”

    This option is useful when a servlet constructs its response based on the values in a database table. A database trigger can be used to call the message service and publish a message to the topic each time the database table is updated. When a servlet or JSP is requested, EAServer can call the message service’s getStatistics method to get the total number of messages published to the topic and compare the value to those in the cache entries. The message topics are stored in the com.sybase.jaguar.servlet.cache.message-topics property.

Configuring Web application page caching properties

You can configure page caching for a Web application, as well as for a Web component. EAServer first checks the component-level page caching properties, and if none exists, checks the application-level properties. In other words, the Web application settings serve as defaults for the Web component properties described in “Configuring page caching for servlets and JSPs”.

StepsConfiguring page caching for a Web application

  1. Display the Page Caching tab in the Web Application Properties dialog box.

  2. Optionally, edit the timeout value, and enter the parameters that you want to use in the key. The parameters you enter here are the default settings for all the servlets and JSPs in the Web application. See Table 5-1 for a description of the page caching properties.

NoteBy default, page caching is disabled, and you cannot enable page caching at the Web application-level.

Caching an entire tree

To use page caching for a JSP that forwards request to, or dynamically includes, other JSPs or static files, consider these factors. By default, when you enable page caching for a JSP Web component, only its content is cached. If a JSP includes, or forwards requests to, other pages or static files, their output is not cached. To include the output of all the pages or files that are invoked, you can select to cache a Web component’s entire tree. This example illustrates portions of three JSP files; two use the <jsp:include> tag to include other JSPs:

// page1.jsp
<H1>This is page 1</H1></p>
<jsp:include page="/page2.jsp" />

// page2.jsp
<H2>This is page 2</H2></p>
<jsp:include page="/page3.jsp" />

// page3.jsp
<H3>This is page 3</H3></p>

If you enable page caching for the Web component mapped to page1.jsp and choose to cache the entire tree, the cached entry displays this in the browser:

This is page 1
This is page 2
This is page 3

If you enable page caching for page1.jsp but not for the entire tree, the cached entry displays this in the browser:

This is page 1

When a client requests the Web component mapped to page1.jsp and it is configured to cache the entire tree, the output from page1.jsp, page2.jsp, and page3.jsp is cached as a single entry. EAServer creates a separate cache entry for a single page when:

For example, if the Web component “Page2” is mapped to page2.jsp and it is not configured to cache the entire tree, its output is cached as a separate entry when a client specifically requests Page2.

Using page caching with filters that modify a response

When using page caching with filters that wrap a servlet response, EAServer must have access to the response. To provide access to the response, the wrapper must implement a getResponse method that returns a ServletResponse object. To implement the wrapper, you can either:

Also, EAServer caches a servlet response immediately after the servlet executes. Therefore, if a filter modifies a response after the servlet executes, the modifications are not saved to the cache.

For more information about creating and using filters, see Chapter 23, “Using Filters and Event Listeners,” in the EAServer Programmer’s Guide.

Copyright © 2005. Sybase Inc. All rights reserved. Using the servlet Java cache