This section looks at handling requests in a typical application, beginning where the application itself begins, with the code that sets up the request-response loop. A WebObjects application begins just as a C program does, by calling its main() function. In a WebObjects application, main() is usually very short. Its job is to set up a WOApplicationAdaptor object and turn over control of the program to it. Generally, main() is just a few lines of code:
void main(int argc, char *argv[]) {
NSAutoreleasePool *pool;
WOApplicationAdaptor *adaptor;
WOWebScriptApplication *application;
pool = [[NSAutoreleasePool alloc] init];
adaptor = [[[WOApplicationAdaptor alloc] init] autorelease];
application = [[[WOWebScriptApplication alloc]
initWithArgC:argc argV:argv]
autorelease];
[adaptor runWithApplication:application];
[pool release];
exit(0);
}
The main() functions you write should look identical or much the same.
This version of the main() function creates an autorelease pool that's used for the automatic deallocation of objects that receive an autorelease message. Next, it creates a WOApplicationAdaptor object that handles communication between an HTTP server and the WOWebScriptApplication object, which is created in the next statement.
The runWithApplication: method initiates the request-response loop. As shown in Figure 1, in each cycle of the loop the WOApplicationAdaptor receives an incoming request, forwards it to the WOWebScriptApplication object, and returns the outgoing response from the WOWebScriptApplication.
The last statement in this version of main() releases the autorelease pool, which sends release messages to any object that has been added to the pool since the application began.
Most of an application's time is spent in the request-response loop started by runWithApplication:, getting and responding to requests. You can participate in request and response handling by taking advantage of hooks into the request-response loop. For example, you can determine what page to return based on user input and modify the header lines of a generated HTTP response.
There are three types of hooks:
To understand when these methods are invoked and what you can use them for, you need to understand the sequence of events in a cycle of the request-response loop. The following section describes the sequence. Information on using the request-response loop hooks is presented later in this chapter.