Table of Contents Previous Section

appendToResponse:inContext:

This method is invoked in the phase of the request-response loop during which the application generates HTML for the response page. You can implement this method to add to the response content or otherwise manipulate the HTTP response. For example, you can add or modify the HTTP headers. The following code excerpt sets the "Expires" header in the HTTP response to "0."

- appendToResponse:aResponse inContext:aContext
{
  [super appendToResponse:aResponse inContext:aContext];
  [aResponse setHeader:@"0" forKey:@"Expires"];
}

The first argument to appendToResponse:inContext: is a WOResponse object. A WOResponse object encapsulates information contained in the generated HTTP response such as the status, response headers, and response content. The second argument is a WOContext object. A WOContext object contains references to application-specific information such as the path to the request component's directory, the version of WebObjects that's running, the name of the application, and the name of the request page.

In a similar manner, you can use appendToResponse:inContext: to append text to the response content. In the following example, a component's appendToResponse:inContext: method appends bold and italic markup elements around a string's value as follows:

id value;
id escapeHTML;
id isBold;
id isItalic;
- appendToResponse:aResponse inContext:aContext
{
    id aString = [value description];
    [super appendToResponse:aResponse inContext:aContext];
    [aResponse appendContentHTMLAttributeValue:@"<p>"];
    if (isBold) {
        [aResponse appendContentHTMLAttributeValue:@"<b>"];
    }
    if (isItalic) {
        [aResponse appendContentHTMLAttributeValue:@"<i>"];
    }
    if (escapeHTML) {
        [aResponse appendContentString:aString];
    } else {
        [aResponse appendContentHTMLString:aString];
    }

    if (isItalic) {
        [aResponse appendContentHTMLAttributeValue:@"</i>"];
    }
    if (isBold) {
        [aResponse appendContentHTMLAttributeValue:@"</b>"];
    }
}

After you invoke super's appendToResponse:inContext:, the application generates the response page. At this point you could do something appropriate for the end of the transaction. For example, the following implementation terminates the current session:

- appendToResponse:response inContext:context {
    [super appendToResponse:response inContext:context];
    [[self session] terminate];
}

The WOSession method terminate schedules the destruction of state associated with the current session, but termination is deferred until the current transaction concludes. You can explicitly terminate a session anytime, anywhere in a WebObjects application.

Table of Contents Next Section