# Response

The ASP Response object deals with server responses to the client

TAGS: ASP, Programming, TECH

## Basic Usage

The most common method of the ASP Response object is Write(). It is a way to write into the client-side code/HTML from the server-side.

<% Response.Write "<p>Hello!" %> world!</p>

It is common practice to process everything then send it to the user all at once. You may decide to scrap the buffer and start putting things in the buffer again:

<%
Response.Buffer = True
Response.Write "<p>Hello world!</p>"
Response.Clear
Response.Write "<p>Goodbye world!</p></body></html>"
Response.End 'Flush and stop processing immediately.


However, you may want tighter control over when items get sent. EG: You may want to modify an item in the HTTP header and make sure it gets sent before other items:

<%
Response.Buffer = False
Response.Status = "401 Unauthorized"
Response.Flush 'Send immediately but continue processing.
Response.Write "<p>Cruel world!</p>" %>

Typically ASP page content is dynamic. EG: Dependent on the user or the data in the database. In such a case you'd want the cache to expire immediately. You may want to modify this with AddHeader() (see CacheControl).

Response.Expires = -1 'Any negative integer.


However, if the content generated by the ASP is going to be the same for a time period, then you will want it to cache and then expire at the end of that time period.

Response.Expires = 10
'10 minutes. Response.ExpiresAbsolute = #February 28, 1969 4:35:17 PM#
'Expiration time is converted to GMT before sending.

After processing client-side code, it may be prudent to go to a different URL instead of sending content from the current ASP page:

Response.Redirect "http://www.georgehernandez.com"

You may want to send just binary, like a picture, instead of the default text/html:

Response.ContentType = "image/gif"
Set objBinaryMaker = Server.CreateObject("ComponentX.ClassY")
vntBytes = objBinaryMaker.MakeBytes
Response.BinaryWrite vntBytes


For long processes, you may want to periodically make sure the client is still there before continuing:

If Not Response.IsClientConnected Then SShutdown(Session.SessionID)

You write to an existing cookie or create a new one:

Response.Cookie("CookieX")("key1") = "value1"

## Collections

Response.Cookie(cookie) = value 'Sets the whole cookie to a single value.
Response.Cookie(cookie)(key) = value 'Sets key-value pairs for the cookie.
Response.Cookies(cookie).HasKeys


The Response.Coookie collection can be looped through like most collections, eg:

For Each Key In Response.Coookie... Next
For intItem = 1 To Response.Coookie.Count ... Next

Here some write-only attributes that can be set for the cookie:

Response.Cookie(cookie).ExpiresAbsolute = date
Response.Cookie(cookie)(key).ExpiresAbsolute = date 'Sets the expiration date.
Response.Cookies(cookie).Domain = domain Response.Cookies(cookie).Path = path
'Respond only if request was to the named domain or path.
Response.Cookies(cookie).Secure
'Mark the cookie as secure (true).

## Properties

### Buffer

Set to true to hold the response until the entire processing is done or the Flush or End method is called. In IIS 4.0 and down, false was the default. In IIS  5.0 and up, true is the default.

### CacheControl

The default value is Private, meaning that the page can only be cached in private caches. The Response.CacheControl property can be set to Public so it can be cached in public caches such as proxy servers.

If you don't want the users to cache a page, then you could also (or alternatively) do it on the client side with this little bit of code:

<!-- The line below uses any past date -->
<meta http-equiv="expires" content="Fri, 28 Feb 1969 04:35:17 GMT">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">


(Don't forget to tell the users to try refreshing with F5 or a forced refresh with CTRL+F5.)

### Charset

Normally the HTTP header of the results contains content-type:text/html. Setting this property will change the HTTP header to content-type:text/html; charset=value. Typical values for Request.Charset include the following: windows-1252, iso-8859-1, and utf-8.

### ContentType

Normally the HTTP header of the results contains content-type:text/html. Setting this property will change the HTTP header to content-type:value. Typical values for value include the following: image/gif, image/jpeg, text/plain, and text/xml.

One nifty thing you can do is make content type Excel so you can have a table display in Excel. EG:

<% Response.ContentType = "application/vnd.ms-excel" %>
<table>
<tr>
<td>1</td><!--- Cell : A1 --->
<td>2</td><!--- Cell : B1 --->
</tr>
<tr>
<td>2</td><!--- Cell : A2 --->
<td>3</td><!--- Cell : B2 --->
</tr>
</table>


If you are returning raw XML to the client, don't forget that there must not be any spaces between the content type tag and the XML declaration.

<%Response.ContentType = "text/xml"%><?xml version="1.0"?>
<myXML>
...
</myXML>


### Expires

Sets minutes before users cached response expires on the browser. If set to a negative number, then the response expires immediately. If multiple calls are made to Response.Expires, then the shortest expiration period is used.

### ExpiresAbsolute

Sets the date before the users cache expires. date is usually enclosed in #s or "s. Expiration time is converted to GMT before sending. (See also CacheControl above.)

Response.ExpiresAbsolute = date

### IsClientConnected

Read-only. Returns true if the client is still connected to the server.

### PICs

Sets the value for the HTTP header: PICS-label: value.

### Status

Sets the value for the HTTP header: Status: value. HTTP Status Codes are 3-digit numbers followed by a short description. Here are some common values for value:

1xx Informational (reserved)
2xx Success
204 No Response
3xx Redirection: needs more action to complete
301 Moved Permanently
307 Temporary Redirect
4xx Client Error: request had incorrect syntax
401 Unauthorized
403 Forbidden
5xx Server Error
501 Not Implemented


## Methods

Adds a new HTTP header of HTTPHeaderName with the indicated value. In IIS 4.0 and down, if Response.AddHeader() was used, then it had to occur before any output was generated such as via Response.Write.

### AppendToLog(LogEntry)

Appends to the Web server log file. The string cannot include commas (,).

Sends bytes.

### Clear()

Clears/deletes the buffer cache without sending it. To use this, Response.Buffer must be True.

### End()

Stop processing the ASP page. If Response.Buffer was set to True, then Response.End() also executes Response.Flush() before it stops the processing.

This method combined with checkpoint usage of Response.Write() is quite handy for debugging.

### Flush()

Sends the buffer cache. To use this, Response.Buffer must be True.

### Redirect(URL)

Tells the user to get a different URL. Unlike the similar Server.Transfer(PathToASP), the URL for Response.Redirect may have query string. EG:

Response.Redirect("MyPage.asp?parameter1=6&parameter2=9")
Server.Transfer("sameServer.asp", True)


There are many reasons to redirect but one of the most common reason is because you have moved the page. In such cases, there is often a need to update the spiders and robots used by search engines and the like. You can leave code like the following at the old location that will not only redirect the user, but will also tell spiders and such to update their index.

Response.Status="301 Moved Permanently";
//Fully qualifed URLs like "http://site.com" work too
Response.Write("<p>As of 2008-06-27, the page you requested has moved to <a href=\"/NewPage.asp\">NewPage.asp</a>.</p>");


For reference, here are other ways to redirect that are not necessarily related to ASP:

#### HTML

<meta http-equiv="refresh" content="0;URL=http://www.google.com/" />
//The number just before the URL is the delay in seconds.


#### JavaScript

<script type="text/javascript">
<!--
//<![CDATA[
//]]>
-->
</script>


The following code will delay before redirecting:

<html><head>
<script type="text/javascript"><!--
function redirect () { setTimeout("go_now()",10000); }
function go_now ()   { window.location.href = "http://www.google.com/"; }
//--></script>
<p>Hit the 'ESC' key or click <a href="javascript:history.back()">BACK</a> to cancel the redirect.</p>
</body></html>


The redirecting page with the following code will not show in the client's history of sites visited:

<script type="text/javascript"><!--
--></script>


It's also possible to use the the window.navigate() method. Note that the window.open() method will open a new window.

#### PHP

<?php
?>


### Write(output)

Writes into the HTTP output. Response.Write() is probably the most commonly used method of the Response object.

In this example the desired output is 100%> but instead there is an unintentional %>, which is seen as an ASP tag by IIS.

<% Response.Write "100%>" %>

Here are two ways to do it correctly. The first escapes the >.

<% Response.Write "100%\>" %>
<% Response.Write "100%" & ">" %>


GeorgeHernandez.comSome rights reserved