Monday, March 22, 2010

Response Object

The ASP Response object is used to send output to the user from the server. Its collections, properties, and methods are described below: Collections Collection Description Cookies Sets a cookie value. If the cookie does not exist, it will be created, and take the value that is specified Properties Property Description Buffer Specifies whether to buffer the page output or not CacheControl Sets whether a proxy server can cache the output generated by ASP or not Charset Appends the name of a character-set to the content-type header in the Response object ContentType Sets the HTTP content type for the Response object Expires Sets how long (in minutes) a page will be cached on a browser before it expires ExpiresAbsolute Sets a date and time when a page cached on a browser will expire IsClientConnected Indicates if the client has disconnected from the server Pics Appends a value to the PICS label response header Status Specifies the value of the status line returned by the server Methods Method Description AddHeader Adds a new HTTP header and a value to the HTTP response AppendToLog Adds a string to the end of the server log entry BinaryWrite Writes data directly to the output without any character conversion Clear Clears any buffered HTML output End Stops processing a script, and returns the current result Flush Sends buffered HTML output immediately Redirect Redirects the user to a different URL Write Writes a specified string to the output ASP Cookies Collection ________________________________________ The Cookies collection is used to set or get cookie values. If the cookie does not exist, it will be created, and take the value that is specified. Note: The Response.Cookies command must appear before the tag. Syntax Response.Cookies(name)[(key)|.attribute]=value variablename=Request.Cookies(name)[(key)|.attribute] Parameter Description name Required. The name of the cookie value Required for the Response.Cookies command. The value of the cookie attribute Optional. Specifies information about the cookie. Can be one of the following parameters: • Domain - Write-only. The cookie is sent only to requests to this domain • Expires - Write-only. The date when the cookie expires. If no date is specified, the cookie will expire when the session ends • HasKeys - Read-only. Specifies whether the cookie has keys (This is the only attribute that can be used with the Request.Cookies command) • Path - Write-only. If set, the cookie is sent only to requests to this path. If not set, the application path is used • Secure - Write-only. Indicates if the cookie is secure key Optional. Specifies the key to where the value is assigned ________________________________________ Examples The "Response.Cookies" command is used to create a cookie or to set a cookie value: <% Response.Cookies("firstname")="Alex" %> In the code above, we have created a cookie named "firstname" and assigned the value "Alex" to it. It is also possible to assign some attributes to a cookie, like setting a date when a cookie should expire: <% Response.Cookies("firstname")="Alex" Response.Cookies("firstname").Expires=#May 10,2002# %> Now the cookie named "firstname" has the value of "Alex", and it will expire from the user's computer at May 10, 2002. The "Request.Cookies" command is used to get a cookie value. In the example below, we retrieve the value of the cookie "firstname" and display it on a page: <% fname=Request.Cookies("firstname") response.write("Firstname=" & fname) %> Output: Firstname=Alex A cookie can also contain a collection of multiple values. We say that the cookie has Keys. In the example below, we will create a cookie-collection named "user". The "user" cookie has Keys that contains information about a user: <% Response.Cookies("user")("firstname")="John" Response.Cookies("user")("lastname")="Smith" Response.Cookies("user")("country")="Norway" Response.Cookies("user")("age")="25" %> The code below reads all the cookies your server has sent to a user. Note that the code checks if a cookie has Keys with the HasKeys property: <% dim x,y for each x in Request.Cookies response.write(" ") if Request.Cookies(x).HasKeys then for each y in Request.Cookies(x) response.write(x & ":" & y & "=" & Request.Cookies(x)(y)) response.write(" " next %> %> Output: firstname=Alex user:firstname=John user:lastname=Smith user: country=Norway user: age=25 Properties: ASP Buffer Property The Buffer property specifies whether to buffer the output or not. When the output is buffered, the server will hold back the response to the browser until all of the server scripts have been processed, or until the script calls the Flush or End method. Note: If this property is set, it should be before the tag in the .asp file Syntax response.Buffer[=flag] Parameter Description flag A boolean value that specifies whether to buffer the page output or not. False indicates no buffering. The server will send the output as it is processed. False is default for IIS version 4.0 (and earlier). Default for IIS version 5.0 (and later) is true. True indicates buffering. The server will not send output until all of the scripts on the page have been processed, or until the Flush or End method has been called. Examples Example 1 In this example, there will be no output sent to the browser before the loop is finished. If buffer was set to False, then it would write a line to the browser every time it went through the loop. <%response.Buffer=true%> <% for i=1 to 100 response.write(i & " ") next %> Example 2 <%response.Buffer=true%>

I write some text, but I will control when the text will be sent to the browser.

The text is not sent yet. I hold it back!

OK, let it go!
<%response.Flush%> Example 3 <%response.Buffer=true%>

This is some text I want to send to the user.

No, I changed my mind. I want to clear the text.
<%response.Clear%> ASP CacheControl Property The CacheControl property sets whether a proxy server can cache the output generated by ASP or not. By default, a proxy server will not keep a cache copy. Syntax response.CacheControl[=control_header] Parameter Description control_header A cache control header that can be set to "Public" or "Private". Private is default and indicates that only private caches may cache this page. Proxy servers will not cache pages with this setting. Public indicates public caches. Proxy servers will cache pages with this setting. Examples <%response.CacheControl="Public"%> or <%response.CacheControl="Private"%> ASP Charset Property The Charset property appends the name of a character-set to the content-type header in the Response object. Default character set is ISO-LATIN-1. Note: This property will accept any string, regardless of whether it is a valid character set or not, for the name. Syntax response.Charset(charsetname) Parameter Description charsetname A string that specifies a character set for the page Examples If an ASP page has no Charset property set, the content-type header would be: content-type:text/html If we included the Charset property: <%response.Charset="ISO-8859-1"%> the content-type header would be: content-type:text/html; charset=ISO-8859-1 ASP ContentType Property ________________________________________ The ContentType property sets the HTTP content type for the response object. Syntax response.ContentType[=contenttype] Parameter Description contenttype A string describing the content type. For a full list of content types, see your browser documentation or the HTTP specification. Examples If an ASP page has no ContentType property set, the default content-type header would be: content-type:text/html Some other common ContentType values: <%response.ContentType="text/HTML"%> <%response.ContentType="image/GIF"%> <%response.ContentType="image/JPEG"%> <%response.ContentType="text/plain"%> <%response.ContentType="image/JPEG"%> This example will open an Excel spreadsheet in a browser (if the user has Excel installed): <%response.ContentType="application/vnd.ms-excel"%>
1 2 3 4
5 6 7 8
ASP Expires Property The Expires property sets how long (in minutes) a page will be cached on a browser before it expires. If a user returns to the same page before it expires, the cached version is displayed. Syntax response.Expires[=number] Parameter Description number The time in minutes before the page expires Examples Example 1 The following code indicates that the page will never be cached: <%response.Expires=-1%> Example 2 The following code indicates that the page will expire after 1440 minutes (24 hours): <%response.Expires=1440%> ASP ExpiresAbsolute Property ________________________________________ The ExpiresAbsolute property sets a date and time when a cached page on a browser will expire. If a user returns to the same page before this date/time, the cached version is displayed. Syntax response.ExpiresAbsolute[=[date][time]] Parameter Description date Specifies the date on which the page will expire. If this parameter is not specified, the page will expire at the specified time on the day that the script is run. time Specifies the time at which the page will expire. If this parameter is not specified, the page will expire at midnight of the specified day. Examples The following code indicates that the page will expire at 4:00 PM on October 11, 2009: <%response.ExpiresAbsolute=#October 11,2009 16:00:00#%> ASP IsClientConnected Property ________________________________________ The IsClientConnected property indicates if the client has disconnected from the server. Syntax response.IsClientConnected Examples <% If response.IsClientConnected=true then response.write("The user is still connected!") else response.write("The user is not connected!") end if %> ASP PICS Property ________________________________________ The PICS property appends a value to the PICS label response header. Note: This property will accept any string value, regardless of whether it is a valid PICS label or not. What is PICS? The PICS (Platform for Internet Content Selection) rating system is used to rate the content in a web site. It looks something like this: PICS-1.1 "http://www.rsac.org/ratingsv01.html" by "your@name.com" for "http://www.somesite.com" on "2002.10.05T02:15-0800" r (n 0 s 0 v 0 l 0) Part Description PICS-1.1 PICS version number "http://www.rsac.org/ratingsv01.html" Rating organization by "your@name.com" Author of the label for "http://www.somesite.com" The URL or the document that has been rated on "2002.10.05T02:15-0800" Expiration date r (n 0 s 0 v 0 l 0) Rating One of the most popular rating system is RSACi (Recreational Software Advisory Council on the Internet). RSACi rating system uses four categories: violence, nudity, sex, and language. A number between 0 to 4 is assigned to each category. 0 means that the page does not contain any potentially offensive content and 4 means that the page contains the highest levels of potentially offensive content. Level Violence Rating Nudity Rating Sex Rating Language Rating 0 None of the below or sports related None of the below None of the below or innocent kissing; romance None of the below 1 Injury to human being Revealing attire Passionate kissing Mild expletives 2 Destruction of realistic objects Partial nudity Clothed sexual touching Moderate expletives or profanity 3 Aggressive violence or death to humans Frontal nudity Non-explicit sexual acts Strong language or hate speech 4 Rape or wanton, gratuitous violence Frontal nudity (qualifying as provocative display) Explicit sexual acts or sex crimes Crude, vulgar language or extreme hate speech There are two ways you can obtain rating for your site. You can either rate your site yourself or use a rating provider, like RSACi. They'll ask you fill out some questions. After filling out the questions, you will get the rating label for your site. Microsoft IE 3.0 and above and Netscape 4.5 and above support the content ratings. You can set the ratings in IE 5, by selecting Tools and Internet Options. Select the Content tab and click the Enable. When the rating exceeds the defined levels the Content Advisor will block the site. You can set the ratings in Netscape 4.7, by selecting Help and NetWatch. We can use the META tag or the response.PICS property to add a rating to our site. Syntax response.PICS(picslabel) Parameter Description picslabel A properly formatted PICS label Examples For an ASP file that includes: Note: Because PICS labels contain quotes, you must replace quotes with " & chr(34) & ". <% response.PICS("(PICS-1.1 by " & chr(34) & "your@name.com" & chr(34) & " for " & chr(34) & "http://www.somesite.com" & chr(34) & " on " & chr(34) & "2002.10.05T02:15-0800" & chr(34) & " r (n 2 s 0 v 1 l 2))") %> the following header is added: PICS-label:(PICS-1.1 by "your@name.com" for "http://www.somesite.com" on "2002.10.05T02:15-0800" r (n 2 s 0 v 1 l 2)) ASP Status Property ________________________________________ The Status property specifies the value of the status line returned by the server. Tip: Use this property to modify the status line returned by the server. Syntax response.Status=statusdescription Parameter Description statusdescription A three-digit number and a description of that code, like 404 Not Found. Note: Status values are defined in the HTTP specification. Examples <% ip=request.ServerVariables("REMOTE_ADDR") if ip<>"194.248.333.500" then response.Status="401 Unauthorized" response.Write(response.Status) response.End end if %> Methods: ASP AddHeader Method ________________________________________ The AddHeader method adds a new HTTP header and a value to the HTTP response. Note: Once a header has been added, it cannot be removed. Note: In IIS 4.0 you have to call this method before any output is sent to the browser. In IIS 5.0 you can call the AddHeader method at any point in the script, as long as it precedes any calls to the response.Flush method. Syntax response.AddHeader name,value Parameter Description name Required. The name of the new header variable (cannot contain underscores) value Required. The initial value of the new header variable ________________________________________ Examples <%Response.AddHeader "WARNING","Error message text"%> ASP AppendToLog Method ________________________________________ The AppendToLog method adds a string to the end of server log entry for this request. You can call this method multiple times in a script. Each time it is called it will append the specified string to the log entry. Syntax response.AppendToLog string Parameter Description string Required. The text to append to the log file (cannot contain any comma characters) ________________________________________ Examples <%Response.AppendToLog "My log message"%> ASP BinaryWrite Method ________________________________________ The BinaryWrite method writes data directly to the output without any character conversion. Tip: This method is used to write image data (BLOB) from a database to a browser. Syntax response.BinaryWrite data Parameter Description data Required. The binary information to be sent ________________________________________ Example If you have an object that generates an array of bytes, you can use BinaryWrite to send the bytes to an application: <% Set objBinaryGen=Server.CreateObject("MyComponents.BinaryGenerator") pic=objBinaryGen.MakePicture response.BinaryWrite pic %> ________________________________________ ASP Clear Method ________________________________________ The Clear method clears any buffered HTML output. Note: This method does not clear the response headers, only the response body. Note: If response.Buffer is false, this method will cause a run-time error. Syntax response.Clear Examples <% response.Buffer=true %>

This is some text I want to send to the user.

No, I changed my mind. I want to clear the text.
<% response.Clear %> Output: (nothing) ASP End Method ________________________________________ The End method stops processing a script, and returns the current result. Note: This method will flush the buffer if Response.Buffer has been set to true. If you do not want to return any output to the user, you should call Response.Clear first. Syntax Response.End Examples

I am writing some text. This text will never be <% Response.End %> finished! It's too late to write more!
Output: I am writing some text. This text will never be ASP Flush Method ________________________________________ The Flush method sends buffered HTML output immediately. Note: If response.Buffer is false, this method will cause a run-time error. Syntax Response.Flush Example <% Response.Buffer=true %>

I write some text, but I will control when the text will be sent to the browser.

The text is not sent yet. I hold it back!

OK, let it go!
<% Response.Flush %> Output: I write some text, but I will control when the text will be sent to the browser. The text is not sent yet. I hold it back! OK, let it go! ASP Redirect Method ____________________________________