Chapter 20 Creating Java Servlets


Page caching

To improve the performance of servlet and JSP requests, you can use the page caching feature. When page caching is enabled for a servlet or JSP Web component, EAServer checks the cache before invoking the Web component. If an appropriate response is found in the cache, EAServer returns the contents of the cache, instead of calling the servlet. If a response is not found in the cache, EAServer invokes the servlet, then saves the response and the response headers in the cache. To cache servlet content, EAServer uses the PageCache component with the object cache manager. For information about object caching, see Chapter 25, "Managing Persistent Component State"

To provide flexibility, EAServer enables users to define the key that is used to look up entries in the cache. By default, a key consists of only the servlet's or JSP's location on disk. You can further define a key by adding up to six optional parameters. In addition, each servlet or JSP entry in the cache has a timeout property associated with it, which invalidates the entry once it has expired.

The optional key parameters are stored in a Web component's property file, so to use page caching, a component must have a property file. Although a JSP can be invoked even if it is not installed as a Web component, to use page caching, there must be a corresponding Web component that is mapped to the JSP. For example, to create and map a Web component for a JSP defined in myJSP.jsp:

  1. Create a JSP Web component called "myJSP".
  2. Set the file property to "myJSP.jsp".
  3. Create a Web application servlet mapping where servlet = "myJSP" and URL Pattern = "/myJSP.jsp".
See "Creating JSPs in Web applications" for more infomation.

You can also configure page caching at the Web application-level. EAServer first checks the component-level page caching properties, and if none exists, checks the application-level properties. See "Page Caching properties" for more information.

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
<HTML>
<H1>This is page 1</H1></p>
<jsp:include page="/page2.jsp" />
</HTML>

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

// page3.jsp
<HTML>
<H3>This is page 3</H3></p>
</HTML>
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.

Steps Configuring page caching for a Web component

  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.

    Note   The 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.
  7. Optionally, edit the timeout value, and enter the parameters that you want to use in the key:
    Table 20-2: Page caching properties
    Parameter Description
    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 Request Parameters 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 Session Attributes 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 29, "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.

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 21, "Using Filters and Event Listeners"

 


Copyright © 2002 Sybase, Inc. All rights reserved.