beautypg.com

Client-side page caching – Apple WebObjects 3.5 User Manual

Page 139

background image

Controlling Component State

139

// example Application.java
public Component pageWithName(String aName) {

Component aPage;

if (aName == null)

aName = "Main";

aPage = ((Session)session()).pageWithName(aName);
if (aPage == null) {

aPage = super.pageWithName(aName);
((Session)session().storePage(aName, aPage);

}
return aPage;

}

Note that we store pages in the session object because we want to cache
these pages on a per-session basis. (Implementing the dictionary in the
application object would cache pages on a per-application basis.) Our
override of WOApplication’s

pageWithName:

first attempts to retrieve the page

from the current session’s dictionary before creating a new copy of the page.

Client-Side Page Caching

When accessing a web page, the user’s browser associates the URL with the
HTML page it downloads from the server and stores this information on
the user’s machine. If the browser is asked to display the URL again at a
later date, it fetches the cached page rather than emitting another request.
In many cases, this short-circuit is desirable because it reduces network
traffic and increases a web site’s perceived responsiveness.

Sometimes, however, you need to make sure the user is seeing the most up-
to-date information. You must therefore disable client-side caching.
WOApplication provides the

setPageRefreshOnBacktrackEnabled:

method for this

purpose. In general, you send this message in the application object’s
initialization method:

// WebScript example
- init {

[super init];

[self setPageRefreshOnBacktrackEnabled:YES];

return self;

}

The

setPageRefreshOnBacktrackEnabled:

method adds a header to the HTTP

response. This header sets the expiration date for an HTML page to the
date and time of the creation of the page. Later, when the browser checks
its cache for this page, it finds that the page is no longer valid and so
refetches it by resubmitting the request URL to the WebObjects
application.