2025-04-27 07:49:33 -04:00

271 lines
22 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html dir=ltr><hEAD><TITLE>Sending Content to the Browser</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
TempString = navigator.appVersion
if (navigator.appName == "Microsoft Internet Explorer"){
// Check to see if browser is Microsoft
if (TempString.indexOf ("4.") >= 0){
// Check to see if it is IE 4
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
}
else {
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
}
}
else if (navigator.appName == "Netscape") {
// Check to see if browser is Netscape
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
}
else
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
//-->
</script>
<META NAME="DESCRIPTION" CONTENT="Explains how to control the response returned to a browser, including buffering the content. Briefly covers sending files from the browser to the server."><META HTTP-EQUIV="Content-Type" content="text/html; charset=Windows-1252">
<META HTTP-EQUIV="PICS-Label" CONTENT='(PICS-1.1 "<http://www.rsac.org/ratingsv01.html>" l comment "RSACi North America Server" by "inet@microsoft.com <mailto:inet@microsoft.com>" r (n 0 s 0 v 0 l 0))'>
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="MS-IT-LOC" Content="Internet Information Services">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<font face="Verdana,Arial,Helvetica">
<h1><a name="H1_37769139">Sending Content to the Browser</a></h1>
<p>As a script in an ASP page is processed, any text or graphics not enclosed within ASP delimiters or &lt;SCRIPT&gt; tags is simply returned to the browser. You can also explicitly send content to the browser by using the <strong>Response</strong> object.</p>
<h2><a name="H2_37769829">Sending Content</a></h2>
<p>To send content to the browser from within ASP delimiters or from a procedure, use the <strong>Write</strong> method of the <strong>Response</strong> object. For example, the following statement sends a different greeting to the user depending on whether the user has visited the page before:</p>
<pre>&lt;%
If blnFirstTime Then
Response.Write &quot;&lt;H3 ALIGN=CENTER&gt;Welcome to the Overview Page.&lt;/H3&gt;&quot;
Else
Response.Write &quot;&lt;H3 ALIGN=CENTER&gt;Welcome Back to the Overview Page.&lt;/H3&gt;&quot;
End If
%&gt;</pre>
<p>Outside of a procedure, you do not have to use <strong>Response.Write</strong> 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 the same output as the previous script:</p>
<pre>&lt;H3 ALIGN=CENTER&gt;
&lt;% If blnFirstTime Then %&gt;
Welcome to the Overview Page.
&lt;% Else %&gt;
Welcome Back to the Overview Page.
&lt;% End If %&gt;
&lt;/H3&gt;</pre>
<p>Intersperse 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 <strong>Response.Write</strong> 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:</p>
<pre>Response.Write &quot;&lt;TR&gt;&lt;TD&gt;&quot; &amp; Request.Form(&quot;FirstName&quot;) _
&amp; &quot;&lt;/TD&gt;&lt;TD&gt;&quot; &amp; Request.Form(&quot;LastName&quot;) &amp; &quot;&lt;/TD&gt;&lt;/TR&gt;&quot;</pre>
<p><strong>Request.Form</strong> returns the values sent from an HTML form (see <a href="iiwaform.htm">Processing User Input</a>).</P>
<p><strong>Note</strong>&nbsp;&nbsp;&nbsp;The ampersand character (&amp) is the string-concatenation character for VBScript. The underscore (_) is the VBScript line-continuation character.</p>
<h2><a name="H2_37772109">Setting the Content Type</a></h2>
<p>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 worksheet, 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 <EM>MIME</EM> (Multipurpose Internet Mail Extensions) types. For example, to start Microsoft Excel, the browser needs to recognize the application/vnd.ms-excel MIME type.</p>
<p>You can use the <A HREF="vbob67ol.htm"><strong>ContentType</strong></A> property of the <strong>Response</strong> 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:</p>
<pre>&lt;% Response.ContentType = &quot;application/x-cdf&quot; %&gt;</pre>
<p>For more information about channels, see <a href="#channels">Creating Dynamic Channels</a> in this topic.</p>
<p>Other common content types are text/plain (for content returned as text instead of interpreted HTML statements), image/gif (for GIF images), image/jpeg (for JPEG images), video/quicktime (for movies in the Apple QuickTime&#174; format), and text/xml (for XML documents). In addition, a Web server or Web browser may support custom MIME types. To see the content types already defined by your Microsoft Web server, use the <a href="javascript://" onclick="Activate(); return false">Internet Information Services snap-in,</a>
<script language="javascript">
<!--
function Activate() {
if (navigator.appName == "Microsoft Internet Explorer"){
window.open("../core/iisnapin.htm", "NewWin", 'toolbar=no, left=200, top=200, scrollbars=no, resizeable=no, width=350, height=350');
}
else if (navigator.appName == "Netscape") {
window.open("../core/iisnapin.htm", "NewWin", 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=0,width=350,height=350,top=200,left=200');
}
}
//-->
</script> to open the property sheets for your Web site, click the <strong>HTTP Headers</strong> tab, and then click the <strong>File Types</strong> tab. These file types may be used as a reference when you choose to manually set the content type with ASP.</p>
<h2><a name="H2_37773741">Redirecting the Browser</a></h2>
<p>Instead of sending content to a user, you can redirect the browser to another URL with the <strong>Redirect</strong> 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.</p>
<pre>&lt;%
If Session(&quot;CustomerID&quot;) = &quot;&quot; Then
Response.Redirect &quot;Register.asp&quot;
End If
%&gt;</pre>
<p>server-side scripts which are processed before any content is sent to the user are said to be <EM>buffered</EM>. ASP enables you to turn buffering on or off, and this configuration can greatly affect the behavior of the <STRONG>Redirect</STRONG> method. Specifically, if you have buffering turned off, then you must redirect the browser before your page's HTTP headers are returned to the browser.</P>
<P>Place the <strong>Response.Redirect</strong> statement at the top of the page, before any text or &lt;HTML&gt;&nbsp;tags, to ensure that nothing has been returned to the browser. If you use <strong>Response.Redirect</strong> after content or headers have been returned to the browser, you will see an error message. Also note that <strong>Response.Redirect</strong> does <EM>not</EM> need to be followed by <STRONG><A HREF="vbob2sis.htm">Response.End</A></STRONG>.</p>
<p>If you want to use <strong>Response.Redirect</strong> from the middle of a page, use it along with the <strong>Response.Buffer</strong> property, as explained in the <a href="#buffcnt">Buffering Content</a> section in this topic.</p>
<h2><A NAME="H2_633205286"></A>Transferring Between .ASP Files</h2>
<p>Using <strong>Response.Redirect</strong> to redirect a browser requires a <em>round-trip</em>, meaning that the server sends an HTTP response to the browser indicating the location of the new URL. The browser automatically leaves the server's request queue and sends a new HTTP request for this URL. The server then adds this request to the request queue along with other client's requests that arrived in the meantime. For a busy Web site, round-trips can waste bandwidth and reduce server performance&#151;especially when the browser is redirected to a file located on the same server.</p>
<p>You can use the <strong>Server.Transfer</strong> method to transfer from one .asp file to another file located on the same server instead of the <strong>Response.Redirect</strong> method. With <strong>Server.Transfer</strong> you can directly transfer requests for .asp files without ever leaving the server request queue, thus eliminating costly round-trips.</p>
<p>For example, the following script demonstrates how you could use <STRONG>Server.Transfer</STRONG> to jump between the pages of an application depending on state information:</p>
<pre>&lt;%
If Session(&quot;blnSaleCompleted&quot;) Then
Server.Transfer(&quot;/Order/ThankYou.asp&quot;)
Else
Server.Transfer(&quot;/Order/MoreInfo.asp&quot;)
End if
%&gt;
</pre>
<p><strong>Server.Transfer</strong> sends requests from one executing .asp file to another file. During a transfer, the originally requested .asp file immediately terminates execution without clearing the output buffer (for more information, see the <a href="#buffcnt">Buffering Content</a> section). Request information is then made available to the destination file when it begins execution. During execution, this file has access to the same set of intrinsic objects (<STRONG>Request</STRONG>, <STRONG>Response</STRONG>, <STRONG>Server</STRONG>, <STRONG>Session</STRONG>, and <STRONG>Application</STRONG>) as the originally requested file.</p>
<p>You can also use <strong>Server.Transfer</strong> to transfer between .asp files located in different applications. However, when you transfer to an .asp file located in another application, the file will behave as if it was part of the application that initiated the transfer (that is, the file has access only to variables scoped for the initiating application, not for the application where the file actually resides). For example, if you transfer from a file located in the Sales Application to a file located in the Personnel Application, then the Sales Application effectively borrows this file from the Personnel Application and runs it as if it were part of the Sales Application.</p>
<p>ASP also provides the <strong>Server.Execute</strong> command that you can use to transfer to a file, execute its content, and then return to the file that initiated the transfer. If you are familiar with VBScript, it will help you to think of <strong>Server.Execute</strong> as analogous to a procedure call, except that instead of executing a procedure, you are executing an entire .asp file. </p>
<p>For example, the following script demonstrates how you could use <strong>Server.Execute</strong> to do dynamic inclusion of .asp files:</p>
<pre>&lt;%
.
.
.
If blnUseDHTML Then
Server.Execute(&quot;DHTML.asp&quot;)
Else
Server.Execute(&quot;HTML.asp&quot;)
End If
.
.
.
%&gt;
</pre>
<p>As long as the destination file belongs to an application on the same server, the originating application will transfer to this file, execute its contents, and then resume executing the file that initiated the transfer. Just as with <STRONG>Server.Transfer</STRONG>, an executed .asp file behaves as if it were part of the originating application. <strong>Server.Execute</strong>, however, will <em>not</em> work across servers. For more information, see <a href="vbob9pid.htm"><STRONG>Server.Execute</STRONG></a>.</p>
<h2><a name="buffcnt">Buffering Content</a></h2>
<p>By default, the Web server processes all script commands on a page before any content is sent to the user. This process is known as <em>buffering</em>. You can use the <strong>Buffer</strong> property of the <strong>Response</strong> object to disable buffering, so that the Web server returns HTML and the results of scripts as it processes a page.</p>
<p>The advantage of buffering your .asp files is that you can abort sending a Web page if, for example, the script processing does not proceed correctly or if a user does not have appropriate security credentials. Instead, you can transfer the user to another page using <strong>Server.Transfer</strong>, or clear the buffer (using the the <strong>Clear</strong> method of the <strong>Response</strong> object) to send different content to the user. Depending on your application, you may want to use the <STRONG>Clear</STRONG> method before transferring. The following example uses both of these methods:</p>
<pre>
&lt;HTML&gt;
&lt;BODY&gt;
.
.
.
&lt;%
If Request(&quot;CustomerStatus&quot;) = &quot;&quot; Then
Response.Clear
Server.Transfer(&quot;/CustomerInfo/Register.asp&quot;)
Else
Response.Write &quot;Welcome back &quot; &amp; Request(&quot;FirstName&quot;) &amp; &quot;!&quot;
.
.
.
End If
%&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;</pre>
<p>You could also use <strong>Response.Buffer</strong> to prevent the Web server from returning the HTTP header before a script can modify the header. Certain properties and methods, such as <strong>Response.Expires</strong> and <strong>Response.Redirect,</strong> modify the HTTP header.</p>
<p>If the <strong>Buffer</strong> property in a script is set to TRUE without also calling the <strong>Flush</strong> method to immediately send buffered content to the browser, 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 the server's response from being sent to the user until the server has finished processing the entire script. For long or complicated scripts, users could experience long wait times before seeing the page.</p>
<p>Buffering is turned on by default for ASP applications. You can use the <a href="javascript://" onclick="Activate(); return false">Internet Information Services snap-in,</a>
<script language="javascript">
<!--
function Activate() {
if (navigator.appName == "Microsoft Internet Explorer"){
window.open("../core/../core/iisnapin.htm", "NewWin", 'toolbar=no, left=200, top=200, scrollbars=no, resizeable=no, width=350, height=350');
}
else if (navigator.appName == "Netscape") {
window.open("../core/../core/iisnapin.htm", "NewWin", 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=0,width=350,height=350,top=200,left=200');
}
}
//-->
</script> to turn off buffering for an entire ASP application. For more information, see <a href="../core/iiwarndh.htm">Configuring ASP Applications</a>.</p>
<h2><a name="H2_37777557">Allowing Proxy Servers to Cache Pages</a></h2>
<p>Your application may be sending pages to a client through a <em>proxy server</em>. 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 browsers. Having the proxy server process requests and cache pages reduces the load on the network and on the Web server.</p>
<p>Although caching works well for many HTML pages, it often does not work well for ASP pages that contain frequently updated information. For example, pages that report stock market prices or display inventory for a high-volume business must 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.</p>
<p>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 <strong>Response.CacheControl</strong> property to set the Cache-Control HTTP header field. The default value of <strong>Response.CacheControl</strong> is the string "Private", which prevents proxy servers from caching the page. To allow caching, set the Cache-Control header field to Public:</p>
<pre>&lt;% Response.CacheControl = &quot;Public&quot; %&gt;</pre>
<p>Because HTTP headers must be sent to the browser or proxy before any page content is sent, either put the <strong>Response.CacheControl</strong> property before any HTML tags or, if you have disabled buffering, use <strong>Response.Buffer</strong> to buffer the page.</p>
<p>The Cache-Control header field is part of the HTTP&nbsp;1.1 specification. ASP pages are not cached on proxy servers that support only HTTP&nbsp;1.0 because no Expires header field is sent.</p>
<h2><a name="H2_37779593">Preventing Browsers from Caching Pages</a></h2>
<p>Each browser version has its own rules for determining whether to cache pages. To prevent a browser from caching ASP pages, use <strong>Response.Expires</strong> to set the Expires header:</p>
<pre>&lt;% Response.Expires = 0 %&gt;</pre>
<p>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 <strong>Response.Expires</strong> property before any HTML tags or buffer the page.</p>
<h2><a name="H2_37780216"></a><a name="channels">Creating Dynamic Channels</a></h2>
<p>A <em>channel</em> is a Web technology available with Microsoft Internet Explorer&nbsp;4.0, or later, that you can use to automatically deliver new or updated Web content to users. The channel schedules the user's computer to periodically connect to a server and retrieve updated information. (This retrieval process is commonly referred to as <em>client pull</em> because information is "pulled" in, or gathered, from the server.) When new information is made available at a particular Web site, the content is downloaded to the browser cache for offline viewing. Clever use of channels for distributing Web based information (especially on intranets) can help to centralize information as well as reduce server traffic. For more information about channels, visit the <a href="http://www.microsoft.com/isapi/redir.dll?prd=ie&ar=inews" TARGET="_blank">Microsoft Internet Explorer Web site</a>.</p>
<p>Using ASP, you can write scripts that dynamically create channels by generating a <em>channel definition file</em>. An XML-based channel definition file (.cdf) describes the organization and update schedule of a channel's contents. Commands in the .cdf file use syntax similar to HTML tags, so they are easy to learn and to generate from a script. When you write a server-side script to create a channel definition file, give the file 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 <strong>Response.ContentType</strong>.</p>
<p>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>
<pre>&lt;P&gt; Choose the channels you want. &lt;/P&gt;
&lt;FORM METHOD=&quot;POST&quot; ACTION=&quot;Chan.cdx&quot;&gt;
&lt;P&gt;&lt;INPUT TYPE=CHECKBOX NAME=Movies&gt; Movies
&lt;P&gt;&lt;INPUT TYPE=CHECKBOX NAME=Sports&gt; Sports
&lt;P&gt;&lt;INPUT TYPE=&quot;SUBMIT&quot; VALUE=&quot;SUBMIT&quot;&gt;
&lt;/FORM&gt;</pre>
<p>The script in Chan.cdx builds the channel definitions based on the form values submitted with the request.</p>
<pre>&lt;% If Request.Form(&quot;Movies&quot;) &lt;&gt; &quot;&quot; Then %&gt;
&lt;CHANNEL&gt;
<em>channel definition statements for the movie pages</em>
&lt;/CHANNEL&gt;
&lt;% End If %&gt;
&lt;% If Request.Form(&quot;Sports&quot;) &lt;&gt; &quot;&quot; Then %&gt;
&lt;CHANNEL&gt;
<em>channel definition statements for the sports pages</em>
&lt;/CHANNEL&gt;
&lt;% End If %&gt;</pre>
<h2><a name="H2_37783243">Accessing Server Resources with WebDAV</a></h2>
<p>Distributed Authoring and Versioning (WebDAV), a powerful extension of the HTTP 1.1 protocol, exposes Web file storage media&#151;such as a local file system&#151;over an HTTP connection. WebDAV holds great promise for making the Web into seamless, collaborative authoring environment. With the IIS&nbsp;5.1 implementation of WebDAV, you can enable remote authors to create, delete, move, search, or apply attributes to files and directories on your Web server. For more information, see <A HREF="../core/wcwbdav.htm">WebDav Publishing</A>.
</p>
<hr class="iis" size="1">
<p align="center"><em><a href="/iishelp/common/colegal.htm">&copy; 1997-2001 Microsoft Corporation. All rights reserved.</a></em></p>
</font>
</font>
</body>
</html>