beautypg.com

The session object and session state – Apple WebObjects 3.5 User Manual

Page 115

background image

Objects and State

115

The WOComponent class defines a method

application

, which provides

access to the component’s application object. So any component can access
application state this way:

//Java
public boolean isLuckyWinner() {

Number sessionCount = application().statisticsStore().get(

"Total Sessions Created");

if (sessionCount == 1000) {

return true;

return false;

}

// WebScript
- isLuckyWinner {

id sessionCount = [[[self application] statisticsStore]

objectForKey:@"Total Sessions Created"];

if (sessionCount == 100])

return YES;

return NO;

}

Sessions can access application state using the same method defined in
WOSession.

Application state persists for as long as the application is running. If your
site runs multiple instances of the same application, application state must
be accessible to all instances. In this case, application state might be best
stored in a file or database, where application instances could easily access
it. This approach is also useful as a safeguard against losing application state
(such as the number of visitors to the site) if an application instance crashes.

The Session Object and Session State

A more interesting type of state that web applications can store is the state
associated with a user’s session. This state might include the selections a
user makes from a catalog, the total cost of the selections so far, or the user’s
billing information.

You typically store session state as instance variables in your application’s
session object. It’s also possible to store session state within a special
dictionary provided by the session object, as we’ll see shortly.

Session state is directly accessible to any component within the application
(although those components can access only the state stored for their
current session). The WOComponent class defines a

session

method that

provides this access. For example, the component can access a session’s
instance variable in this way: