Apple WebObjects 3.5 User Manual

Page 117

Advertising
background image

Objects and State

117

//Java example
String componentName;
Context context;
String contextID;
String elementID;
String uniqueKey;

context = this.context();
componentName = context.component().name();
contextID = context.contextID();
elementID = context.elementID();
uniqueKey = componentName + "-" + contextID + "-" + elementID;
this.session().setObject(

someState

, uniqueKey);

// WebScript example
id componentName;
id context;
id contextID;
id elementID;
id uniqueKey;

context = [self context];
componentName = [[context component] name];
contextID = [context contextID];
elementID = [context elementID];
uniqueKey = [NSString stringWithFormat:@"%@-%@-%@", componentName,

contextID, elementID];

[[self session] setObject:

someState

forKey:uniqueKey];

Since, for a given context, each element in a page has its own element ID,
combining the context and element IDs yields a unique key. The
component name is added to the key for readability during debugging.

As described in the chapter “WebObjects Viewed Through Its Classes”
(page 63), the URLs that make up the requests to a WebObjects application
contain an identifier for a particular session within the application. Using
this identifier, the application can restore the state corresponding to that
session before the request is processed. If the request is that of a user
contacting the application for the first time, a new session object is created
for that user.

As you can imagine, storing data for each session has the potential of
consuming excessive amounts of resources, so WebObjects lets you set a
time-out for the session objects and lets you terminate them directly.

In summary, session state is accessible only to objects within the same
session and persists only as long as the session object persists.

Advertising