The response object represents the outgoing response to the user's browser. You can use the response object to write output directly from scripts, change the content type that will be displayed, and set cookies and response headers.
Normally the output to the user's browser is buffered and the buffer's contents will be sent only when the request completes or the buffer is full. A flush() method is provided that allows the buffer's current contents to be sent immediately to the client browser.
Headers including the content type, cache headers, and cookies must be sent to the browser before any of the rest of the page content. As a result attempts to change these details after the buffer has been flushed will be ignored.
The default display method invokes the renderer object's own display method to send the page template's output to the browser. By using the additional response methods to generate output directly the display method can be rewritten to avoid the use of the renderer object entirely, as the following page script example shows:
function display() {
response.setContentType('text/plain');
response.addCookie('X-Chrome-Cookie-Test','TEST1');
response.addHeader('X-Chrome-Header-Test','TEST2');
response.write('Hello ');
response.writeln('World!');
response.flush();
}
The response object's methods can also be used to set the content type and add cookies and headers independently while leaving the rest of the page rendering logic as it stands.
Stores a value as a cookie on the user's browser against the specified name. Cookie values can be retrieved by scripts using the request object's getCookie(name) method.
In order for the browser to know how to display a rendered document the Content-type header is sent along with the response body. While URLs typically include some indication of the file type with a file extension (e.g. http://example.com/index.html), this is not in fact required. The file extension can be omitted or can be entirely misleading - for example a document with an extension of html can in fact return an image, a text file, or any other content.
The setContentType(type) method therefore allows you to explicitly set a file type for the outgoing content.
Note that the default content type for pages created with the Chrome Server web administration interface is always html, so you will often want to use this method to override the default. Content uploaded via the WebDAV API will be given a content type guessed from the extension, and will thus more often be apropriate but may still need to be overruled on occasion.
The following code shows an example of changing the outgoing content type:
function display() {
// The page template contains CSS rather than HTML content, so
// we must adjust the content type header accordingly before rendering
// the template:
response.setContentType('text/css');
renderer.display();
}
The internet standards document RFC1341 specifies the MIME content type mechanism in full.
Allows an HTTP header to be added to the outgoing content. This method will have no effect if the buffer has overflowed or the buffer has been explicitly flushed.
Typical usage of this method would be to add a cache header to the outgoing content to reduce the number of requests to the server. Note that Chrome Server architecture includes a "Reverse Proxy" cache that will honour your cache headers - therefore you need to be aware that content may be cached within the Chrome Server network as well as within users' browsers if you make use of the ability to set cache headers.
Permits arbitrary content to be written in response to the incoming request independently of the page template, or in anticipation of a partial page template.
Chrome Server templating operations do not take effect in output written in this manner - so for example if the command response.write('${foo}'); is issued the otuput seen in the browser will be literally ${foo} rather than the contents of any scripting variable named "foo".
The following example shows the use of the write(text) function to completely replace the normal use of the page template.
function display() {
response.write('<html>');
response.write('<head>');
response.write('<title>Hello World!</title>');
response.write('</head>');
response.write('<body>');
response.write('<h1>Hello World!</h1>');
response.write('</body>');
response.write('</html>');
response.flush();
}
In order to write non-html content, including text and CSS files, the setContentType(type) method must be used.
Exactly as for the write(text) function this permits arbitrary content to be written in response to the incoming request independently of the page template, or in anticipation of a partial page template. The difference is that this appends a line terminator to the written output.
In practice this method should be used when the output is to be made readily human readable - for example if you want the source code of a directly written html page to be cleanly formatted, or if you are sending a text file to the browser as in the following example:
function display() {
response.setContentType('text/plain');
response.writeln('Shopping');
response.writeln('--------');
response.writeln('Eggs: 4');
response.writeln('Milk');
response.writeln('Bananas: 6');
response.writeln('Sugar: 1/2 lb');
response.writeln('');
response.writeln(' Mmm... pancakes!');
response.flush();
}
Content written using the normal write(text) function will effectively be written on a single line unless line terminator characters are explicitly included in the text passed to as the parameter.
The line terminator characters including by the writeln command are always the Carriage Return, Line Feed pair (expressed as the escape symbols \r\n in Javascript strings).
Forces output held in the buffer to be sent to the client browser.