1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <title>Responses and Presentation</title> 5 <meta name="generator" 6 content="amaya 8.1a, see http://www.w3.org/Amaya/" /> 7 <link href="styles.css" rel="stylesheet" type="text/css" /> 8 </head> 9 <body> 10 <h1>Responses and Presentation</h1> 11 <p>After performing some kind of 12 processing on input information, an 13 application will then want to produce some kind of response to indicate 14 what 15 went on. Here are some examples of responses:</p> 16 <ul> 17 <li>Returning the contents of a 18 requested file.</li> 19 <li>Showing a message telling 20 the user that the requested operation succeeded or failed.</li> 21 <li>Presenting a view onto the 22 application with the results of the recent activity shown in a Web page.</li> 23 </ul> 24 <h2>Generating Responses</h2> 25 <p>The procedure involved in 26 generating a response usually involves the 27 following steps:</p> 28 <ol> 29 <li>Setting a response code to 30 signal whether the application performed the requested operation 31 successfully.</li> 32 <li>Setting a content type and a 33 <a>character encoding</a>.</li> 34 <li>Producing content and 35 sending it to the user.</li> 36 </ol> 37 <p>The kind of code involved may 38 well resemble the following:</p> 39 <pre>from WebStack.Generic import ContentType<br /><br />class MyResource:<br /> def respond(self, trans):<br /> [Perform the requested operations.]<br /><br /> if [the operation was successful]:<br /> trans.set_response_code(200)<br /> trans.set_content_type(ContentType("text/html", encoding="utf-8"))<br /> out = trans.get_response_stream()<br /> out.write([some data either as a plain string suitably encoded or as Unicode])<br /> else:<br /> trans.set_response_code(500) # or some other code<br /> trans.set_content_type(ContentType("text/html", encoding="utf-8"))<br /> out = trans.get_response_stream()<br /> out.write([some other data either as a plain string suitably encoded or as Unicode])</pre> 40 <p>As discussed in <a href="encodings.html">"Character Encodings"</a>, 41 care 42 must be taken generating the response so that it meets any expectations 43 that 44 browsers and other Web clients may have.</p> 45 <div class="WebStack"> 46 <h3>WebStack API - 47 Response-Related Methods</h3> 48 <p>Transaction objects have 49 various methods that can be used in generating 50 responses:</p> 51 <dl> 52 <dt><code>set_response_code</code></dt> 53 <dd>This accepts an integer 54 value denoting the response condition as described in the HTTP 55 specification. If this method is not used, WebStack sets a <code>200</code> 56 status condition on the response, meaning that the request was 57 processed successfully.</dd> 58 <dt><code>set_content_type</code></dt> 59 <dd>This accepts a content type 60 object (typically <code>WebStack.Generic.ContentType</code>) 61 which specifies both the media type and the character encoding (if 62 relevant) of the data sent to the user. The media type describes the 63 format of the data (eg. <code>text/html</code> 64 - a Web page), whereas the character encoding describes how any 65 character information on the page is encoded - see <a 66 href="encodings.html">"Character Encodings"</a> 67 for more information.</dd> 68 <dt><code>get_response_stream</code></dt> 69 <dd>This returns the output 70 stream through which data may be sent to the user.</dd> 71 </dl> 72 </div> 73 <h2>Integrating with Content Generators</h2> 74 <p>Just as applications might need to integrate with other systems in 75 order to fetch information or to perform operations on behalf of the 76 user, the generation of response content can also be made a lot easier 77 by using external libraries. In the above example code, the process of 78 obtaining and formatting the actual data to be written out has been 79 left unspecified, but for anything more complicated than "hello world" 80 it is usually advisable to consider using templating systems which 81 combine raw data and templates to produce formatted output that can be 82 displayed as a Web page (amongst other things).</p> 83 <p>See <a href="integrating.html">"Integration with Other Systems"</a> 84 for more information on the principles of using such external libraries.</p> 85 </body> 86 </html>