Sending Information to a User

You can use the ASP built-in object Response to control the information you send to a user by using the:

Sending Text to a User

The Write method is the most commonly used method of the Response object. You can use the Write method to send information to a user from within ASP delimiters.

Response.Write variant

Where variant can be any data type supported by your default primary scripting language.

For example, the following statement sends a greeting to the user:

<% 
If user_has_been_here_before Then 
  Response.Write "<H3 ALIGN=CENTER>Welcome Back to the Overview Page</H3>" 
Else 
  Response.Write "<H3 ALIGN=CENTER>Welcome to the Overview Page</H3>"
End If 
%> 

The Response.Write method is especially useful if you want to send content back to the user from within a procedure.

You do not have to use Response.Write to send content back to the user. Content that is not within scripting delimiters is sent directly to the browser, which formats and displays this content accordingly. For example, the following script produces exactly the same output as the previous script:

<H3 ALIGN=CENTER> 
<% If user_has_been_here_before Then %> 
Welcome Back to the Overview Page. 
<% Else %> 
Welcome to the Overview Page. 
<% End If %> 
</H3> 

Redirecting a User to Another URL

Instead of sending content to a user, you can redirect the browser to another URL with the Redirect method.

Response.Redirect URL

For example, if you want to make sure users have entered your application from a particular page, you can check to see if they have been to that page; if they have not, you can send them there.

<%
If Not Session("Been_to_Home_Page") Then 
  Response.Redirect "homepage.asp" 
End If
%> 

Note   If you use Response.Redirect from an .asp file after content has already been sent back to the user, an error message is generated.

Setting the HTTP Content Type

You can use the ContentType property of the Response object to set the HTTP content type string for the content you send to a user. The general syntax is

Response.ContentType = ContentType

where ContentType is a string describing the content type. For a full list of supported content types, see your Web browser documentation or the current HTTP specification.

For example, if you want to send source (that is, the .asp file from which an ASP page is generated) to a browser, set ContentType to text/plain:

<% Response.ContentType = "text/plain" %> 

The browser then displays the page as text, rather than interpreting the page as HTML.

Using the Cookies Collection with the Response Object

A cookie is a token that either a client browser sends to a Web server or a Web server sends to a client browser. Cookies allow a set of information to be associated with a user. ASP scripts can both get and set the values of cookies by using the Cookies collection.

This section discusses how to set the value of cookies your Web server sends to a client browser. For information about accessing cookies a browser sends to your Web server, see Using the Cookies Collection with the Request Object.

To set the value of a cookie, use Response.Cookies. If the cookie does not already exist, Response.Cookies creates a new one:

<% Response.Cookies("animal")="elephant" %> 

Similarly, to set the value of a cookie key:

<% Response.Cookies("animal")("elephant")="African" %> 

If an existing cookie has key values but Response.Cookies does not specify a key name, then the existing key values are deleted. Similarly, if an existing cookie does not have key values but Response.Cookies specifies key names and values, the existing value of the cookie is deleted and new key-value pairs are created.

Buffering Response

The default setting for buffering of all ASP pages is off. However, you can set the Buffer property of the Response object to True to process all of the script on a page before sending anything to the user:

<% Response.Buffer = True %> 

You can use buffering to determine at some point in the processing of a page that you do not want to send previous content to a user. You can, instead, redirect the user to another page with the Redirect method of the Response object, or clear the buffer with the Clear method of the Response object and send different content to the user. The following example uses both of these methods.

<% Response.Buffer = True %>
<html>
<body>
.
.
.
<%
If Request("FName") = "" Then
  Response.Clear
  Response.Redirect "/aspsamp/samples/test.html"
  Response.End
Else
  Response.Write Request("FName")
End If
%>
</body>
</html>

When you call the Buffer method in a script and do not call the Flush method in the same script, the server will maintain Keep-Alive requests made by the client. The benefit of writing scripts in this manner is that server performance is improved because the server does not have to create a new connection for each client request (assuming that the server, client, and any proxies all support keep-alive requests). However, a potential drawback to this approach is that buffering prevents any of the response from being displayed to the user until the server has finished all script processing for the current .asp file. For long involved scripts, the user might be forced to wait a considerable amount of time before the script is processed.

Buffering is turned off by default for all ASP pages in all applications by setting the BufferingOn registry setting to 0. For more information about this registry setting, see Configuring Registry Entries.


© 1996 Microsoft Corporation. All rights reserved.