Sending Content to the Browser

As an ASP script is processed, any text or graphics not enclosed within ASP delimiters or <SCRIPT> tags is simply returned to the browser. You can also explicitly send content to the browser by using the Response object.

Sending Content

To send content to the browser from within ASP delimiters or from a procedure, use the Write method of the Response object. For example, the following statement sends a different greeting to the user depending on whether the user has visited the page before:

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

Outside of 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 FirstTime Then %> 
Welcome to the Overview Page. 
<% Else %> 
Welcome Back to the Overview Page. 
<% End If %> 
</H3> 

Use intermingled script commands and HTML when you just need to return output once or when it is more convenient to add statements to existing HTML text. Use Response.Write when you do not want to break up a statement with delimiters or when you want to build the string that is returned to the browser. For example, you could construct a string of text that builds a table row with values sent by an HTML form:

Response.Write "<TR><TD>" & Request.Form("FirstName") _
 & "</TD><TD>" & Request.Form("LastName") & "</TD></TR>"

Request.Form returns the values sent from an HTML form (see Working with HTML Forms). The ampersand character (&) is the string continuation character for VBScript.

Setting the Content Type

When the Web server returns a file to a browser, it tells the browser what type of content is contained in the file. This enables the browser to determine whether it can display the file itself or whether it has to call another application. For example, if the Web server returns a Microsoft Excel spreadsheet, the browser must be able to start a copy of Microsoft Excel to display the page. The Web server recognizes file types by mapping the file name extension to a list of MIME types.

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. For example, the following command sets the content type for channel definitions:

<% Response.ContentType = "application/x-cdf" %> 

For more information about channels, see “Creating Dynamic Channels,” in this topic.

Other common content types are text/plain (for content returned as text instead of interpreted HTML statements), text/gif (for GIF images), and video/quicktime (for movies in the Apple QuickTime® format). A set of standard MIME types has been defined; in addition, a Web server or Web browser may support custom MIME types. To see the content types supported by your Microsoft Web server, use Internet Service Manager to open the property sheets for your Web site, click the HTTP Headers tab, and then click the File Types tab.

Redirecting the Browser

Instead of sending content to a user, you can redirect the browser to another URL with the Redirect method. For example, if you want to make sure users have entered your application from a home page so that they receive a customer ID, you can check to see if they have a customer ID; if they do not, you can redirect them to the home page.

<%
If Session("CustomerID") = 0 Then
  Response.Redirect "homepage.asp" 
End If
%> 

Unless you have buffering turned on, you must redirect the browser before any content or headers are returned to the browser. Place the Response.Redirect statement at the top of the page, before the <HTML> tag, to ensure that nothing has been returned to the browser. If you use Response.Redirect after content or headers have been returned to the browser, you will see an error message.

If you want to use Response.Redirect from the middle of a page, use it along with the Response.Buffer property, as explained in the following section.

Buffering Content

By default, the Web server returns HTML and the results of processing scripts as it processes the ASP page. However, you can set the Buffer property of the Response object so that all of the server script commands on a page are processed before any content is sent to the user.

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.

<% 
'Turn on buffering.  This statement must appear before the <HTML> tag.
Response.Buffer = True %>
<html>
<body>
.
.
.
<%
If Request("FName") = "" Then
  Response.Clear
  Response.Redirect "/samples/test.html"
Else
  Response.Write Request("FName")
End If
%>
</body>
</html>

You could also use Response.Buffer to prevent the Web server from returning the HTTP header before a script can modify the header. Certain properties and methods, such as Response.Expires and Response.Redirect, modify the HTTP header.

When you set the Buffer property 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 proxy servers 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 or complicated scripts, the user might be forced to wait a considerable amount of time before seeing the page.

Buffering is turned off by default for ASP applications. You can use Internet Service Manager to turn on buffering for an entire ASP application.

Allowing Proxy Servers to Cache Pages

Your application may be sending pages to a client through a proxy server. A proxy server acts on behalf of client browsers to request pages from Web sites. The proxy server caches HTML pages so that repeated requests for the same page can be returned quickly and efficiently to the browsers. Having the proxy server process requests and cache pages reduces the load on the network and on the Web server.

While caching works well for many HTML pages, it often does not work well for ASP pages that contain dynamically generated information. For example, pages that report stock market prices or display inventory for a high-volume business need to provide timely information. Information that is even one hour old might not be accurate enough. If your application returns personalized information, such as a custom home page, you want to ensure that no user sees another user's personal information.

By default, ASP instructs proxy servers not to cache the ASP page itself (although images, image maps, applets, and other items referenced from the page are cached). You can allow caching for certain pages by using the Response.CacheControl property to set the Cache-Control HTTP header field. The default value of Response.CacheControl is the string "Private", which prevents proxy servers from caching the page. To allow caching, set the Cache-Control header field to Public:

<% Response.CacheControl = "Public" %>

Because HTTP headers must be sent to the browser or proxy before any page content is sent, either put the Response.CacheControl property before any HTML tags or use Response.Buffer to buffer the page.

The Cache-Control header field is part of the HTTP 1.1 specification. ASP pages are not cached on proxies that support only HTTP 1.0 because no Expires header field is sent.

Preventing Browsers from Caching Pages

Each browser version has its own rules for determining whether to cache pages. To prevent a browser from caching ASP pages, use Response.Expires to set the Expires header:

<% Response.Expires = 0 %>

A value of 0 forces cached pages to expire immediately. Because HTTP headers must be sent to the browser before any page content is sent, either put the Response.Expires property before any HTML tags or use Response.Buffer to buffer the page.

Creating Dynamic Channels

Internet Explorer 4.0 introduces a new feature that Web designers can use to group Web pages with a common theme into channels. In the browser, channels are presented on a channel bar; users access a channel by clicking its icon. Channels are automatically updated in the background; users do not need to visit your site again to get the latest pages downloaded to their browsers. Channels provide a quick and intuitive way for users to see groups of related Web pages that are automatically updated.

Using ASP, you can write scripts to gather user preferences and then dynamically create channels. A channel definition file (.cdf) establishes the organization and scheduling of the channel contents. Commands in the .cdf file use a syntax similar to HTML tags, so they are easy to learn and to generate from a script. When you write an ASP script to create a channel definition file, give the script a .cdx extension. When ASP reads a file with a .cdx extension, it automatically sends the application/x-cdf content type, which tells the browser to interpret the bytes as channel definitions. If you do not use the .cdx extension, your script must manually set the content type to application/x-cdf by using Response.ContentType.

Here is an example of how you might use channels. The following HTML form asks the user to select channels. When submitted, the form calls a script in a .cdx file to create the channel definitions.

<P> Choose the channels you want. </P>
<FORM METHOD="POST" ACTION="chan.cdx">
<P><INPUT TYPE=CHECKBOX NAME=Movies> Movies
<P><INPUT TYPE=CHECKBOX NAME=Sports> Sports
<P><INPUT TYPE="SUBMIT" VALUE="SUBMIT">
</FORM>

The script in Chan.cdx builds the channel definitions based on the form values submitted with the request.

<% If Request.Form("Movies") <> "" Then %>
  <CHANNEL>
    channel definition statements for the movie pages
  </CHANNEL>
<% End If %>

<% If Request.Form("Sports") <> "" Then %>
  <CHANNEL>
    channel definition statements for the sports pages
  </CHANNEL>
<% End If %>

For information on channels and channel definition files, see the Internet Explorer page on microsoft.com (http://www.microsoft.com/ie). For more information on working with forms, see Working with HTML Forms. If you have installed the Exploration Air sample site, you can see an example of using ASP to create dynamic channels.

Sending Files to the Server

A browser can use the Posting Acceptor application to send files to the Web server. When Posting Acceptor uploads files, it sends URL-encoded form fields that list the name and location of each received file. You can add a postprocessing URL to the scripts that upload files to call an ASP script to process these field names. For example, you could write a script that automatically sends e-mail with the names and locations of the files to the system administrator.