Apple WebObjects 3.5 User Manual

Page 123

Advertising
background image

State Storage Strategies

123

applications—changing storage strategies midsession can cause errors. For
example, imagine an application that stores state in the page during the first
half of a session and stores state in cookies for the second. Now, suppose
that the user backtracks from a page in the second half to one in the first and
resubmits the page. The application’s strategy and the actual storage
mechanism won’t match, and state will be lost.

In a normal WebObjects application, you should set the session storage
mechanism as early as possible, usually in the application object’s
initialization method. You set the mechanism by sending the application
object a

setSessionStore:

message. This method takes a WOSessionStore (or

SessionStore in Java) object as an argument. WOSessionStore declares
these methods to create specific types of session stores:

serverSessionStore

pageSessionStore

cookieSessionStoreWithDistributionDomain:secure:

The following sections describe each state-storage option in detail and
show examples of setting the session store.

State in the Server

Storing state in memory on the application server is the default behavior, so
no setup is necessary. However, if you want access to the session store
object, you can include the following code in the application object’s
initialization method:

// WebScript example
id sessionStore = [WOSessionStore serverSessionStore];
[self setSessionStore:sessionStore];

// Java example
SessionStore sessionStore = SessionStore.serverSessionStore();
this.setSessionStore(sessionStore);

When state is stored in the server, the application keeps a cache of session
objects in the session store, and each session keeps a cache of component
objects. Because these objects can take up a lot of memory, WebObjects
gives you ways to control the amount of memory this state storage
mechanism consumes:

Setting session time-outs (see “Controlling Session State” (page 133))

Setting the size of the page cache (see “Adjusting the Page Cache Size”
(page 136))

Advertising