beautypg.com

Apple WebObjects 3.5 User Manual

Page 124

background image

Chapter 7

Managing State

124

Page uniquing by implementing

pageWithName:

in the session object (see

“pageWithName: and Page Caching” (page 138))

A significant consequence of storing state in memory is the effect on load-
balanced applications. When an application is load balanced, there are several
instances of that application running on different physical machines to reduce
the load on a particular server. (The online book Serving WebObjects describes
how to set this up.) WebObjects can route any request to any application
instance running on any machine as long as that instance doesn’t store state in
memory in the server (that is, as long as the application is stateless or uses one
of the other state-storage mechanisms described in this chapter). When state is
stored in the server, however, it is stored in the application instance. Because
state is stored in the application instance, all requests made by one session must
return to that instance.

State in the Page

To store state in the page, you must do two things:

Specify page session store as the application’s state storage mechanism.

Include a WOStateStorage element inside of a form on each page.

You specify the page session store this way:

// Application.java
public Application() {

super();
this.setSessionStore(SessionStore.pageSessionStore());

}

// Application.wos.
- init {

[super init];
[self setSessionStore:[WOSessionStore pageSessionStore]];
return self;

}

Next, you must add a form to each page of the application and place a
WOStateStorage object within the form. You do this in WebObjects Builder by
adding a Custom WebObject to the form and then using the Inspector panel to
specify that the type of element is “WOStateStorage”.)

The WOStateStorage element maps to an HTML input element of type
“hidden.” The “hidden” input type contains text that is not displayed in the
user’s browser. For example, using state in the page, the HTML source for the
Guess page of the SessionStores example would look something like this: