Chapter 20 Creating Java Servlets
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:
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.
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 3If 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:
Configuring page caching for a Web component
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 thecom.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 thecom.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.
|
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. |
![]() |