Apple WebObjects 3.5 User Manual

Page 128

Advertising
background image

Chapter 7

Managing State

128

NSData object is then asked for its ASCII representation. WebObjects pairs this
data with names it generates and creates the Set-Cookie headers of the response
page.

The process is reversed when a user submits a request containing cookies. The
ASCII archive from the Set-Cookie headers is converted to its binary, NSData,
representation. The session object and the components it contains are then
unarchived from the NSData object, thus restoring the session state.

One of the big advantages of using cookies over state in the page is that you
don’t have to design your application around forms. As you recall, storing state
in the page implies using hidden field elements, which must be located in
HTML forms. Cookies, however, are stored in the HTTP header and so are
independent of the HTML elements in the page. With a cookie session store
you could, for example, let users navigate from page to page by using hyperlinks
rather than by submitting forms. In addition (and for similar reasons), storing
state in cookies works better with frames than does storing state in the page.

However, the cookie mechanism has a size restriction that limits its usefulness.
Currently, cookie data is passed from the HTTP server to the WebObjects
application either through environment variables that typically are limited to
4KB or through a server’s own API that in some cases is even more restrictive.
We recommend that cookie state data (that is the ASCII representation of the
state data) be kept to 2KB or less. Given these limitations, cookies can be best
used for such things as storing keys used to fetch information from a database.

Custom State-Storage Options

If the provided state-storage strategies are insufficient for your needs, you can
implement your own state storage. For example, you might want to store state
in a file or database. The SessionStores application provides an example of a
state-storage mechanism that uses the file system. Let’s take a look at how it’s
done.

In WebObjects, an application saves and restores sessions by sending the
session store object these messages:

saveSession:

restoreSession

This is the minimum interface that a custom session store must present to the
application object. In the WebScript version of the SessionStores example, the
custom storage class FileSessionStore presents this interface:

Advertising