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