Documentation Archive Developer
Search
PATH  Documentation > WebObjects 4.5 > WebObjects Developer's Guide


Table of Contents Previous Section

Generating a Response

The appendToResponse:inContext: method is invoked in the final phase of the request-response loop, during which the application generates HTML for the response page.

Note: Unlike, takeValuesFromRequest:inContext: and invokeActionForRequest:inContext:, the appendToResponse:inContext: method may be invoked by the direct action request-response loop. If the direct action method returns a WOComponent object, that component's appendtoResponse:inContext: method is invoked to generate the response.

You can override this method to add to the response content or otherwise manipulate the HTTP response. For example, you can add a cookie to the response as in the following example:

- appendToResponse:aResponse inContext:aContext
{
id aCookie = [WOCookie cookieWithName:@"myCookie"
value:@"important information goes here"];

[super appendToResponse:aResponse inContext:aContext];
[aResponse addCookie:aCookie];
}
In a similar manner, you can use appendToResponse:inContext: to add text to the response content. In the following example, a component's appendToResponse:inContext: method adds 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 appendContentHTMLString:@"<p>"];
if (isBold) {
[aResponse appendContentHTMLString:@"<b>"];
}
if (isItalic) {
[aResponse appendContentHTMLString:@"<i>"];
}

if (escapeHTML) {
[aResponse appendContentString:aString];
} else {
[aResponse appendContentHTMLString:aString];
}

if (isItalic) {
[aResponse appendContentHTMLString:@"</i>"];
}
if (isBold) {
[aResponse appendContentHTMLString:@"</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 request. For example, the following implementation terminates the current session:

public void appendToResponse(WOResponse response, 
WOContext context) {
super.appendToResponse(response, context);
session().terminate();
}
For more details on each phase of the request-response loop, read the chapter "WebObjects Viewed Through Its Classes".

Table of Contents Next Section