ASP Lecture Series (12) Sending Content to the Browser
Author:Eve Cole
Update Time:2009-05-30 19:59:00
When processing ASP script, any text or graphics that are not contained within ASP delimiters or <SCRIPT> tags will simply be returned to the browser. Content can be sent to the browser explicitly by using the Response object.
Sending Content To send content to the browser from within an ASP delimiter or procedure, you can use the Write method of the Response object. For example, the following statement can send a different greeting depending on whether the user has visited this page:
<%
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 the procedure, you don't have to use Response.Write to send content back to the user. Content that is not inside a script delimiter is sent directly to the browser, which formats and displays it. For example, the following script process has the same output as the script above:
<H3 ALIGN=CENTER>
<% If FirstTime Then %>
Welcome to the Overview Page.
<%Else%>
Welcome Back to the Overview Page.
<% End If %>
</H3>
Use mixed script commands and HTML when you need to return output only once or when it is convenient to add statements to existing HTML text. Use Response.Write when you don't want to separate a statement with delimiters or when you want to create a string that is returned to the browser. For example, you can construct a string of text to create a table row using values returned from an HTML table:
Response.Write "<TR><TD>" & Request.Form("FirstName") _
& "</TD><TD>" & Request.Form("LastName") & "</TD></TR>"
Setting the type of content When the web server returns the file to the browser, it also tells the browser the type of content contained in the file. This enables the browser to decide whether it can display the file itself or must call another application. For example, if the network server returns a Microsoft Excel table, the browser must call a copy of Microsoft Excel to display the page. Web servers identify file types by mapping the file's extension to a MIME type table.
You can use the Response object's ContentType property to set the HTTP content type string for content sent to the user. For example, the following command sets the content type for a channel definition:
<% 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 returned as text content rather than interpreted HTML statements), text/gif (for GIF images), and video/quicktime (for movies in the Apple QuickTime® format). A standard set of MIME types has been defined and is supported by either a Web server or a Web browser. To see what content types your Microsoft Web server supports, use Internet Services Manager to open your Web site's property page, click the HTTP Headers tab, and then click the File Types tab.
Redirect the browser Use the Redirect method to redirect the browser to another URL instead of sending the content to the user. For example, if you want to confirm that users have entered your application from the home page so they can receive a customer ID, you can check whether they have a customer ID number; if not, you can redirect them to the home page.
<%
If Session("CustomerID") = 0 Then
Response.Redirect "homepage.asp"
End If
%>
Unless the buffer is already open, you must redirect the browser before any content or headers are returned to the browser. Placing the Response.Redirect statement at the top of the page and before the <HTML> tag ensures that no content is returned to the browser. If you use Response.Redirect after returning content or headers to the browser, you will see an error message.
If you are using Response.Redirect in the middle of the page, use it with the Response.Buffer property, as explained in the following section.
Buffered Content By default, the Web server returns HTML and script processing results when processing ASP pages. However, you can set the Buffer property of the Response object to process all server script commands on the page before sending anything to the user.
You can use buffering techniques to determine a point in the page processing process that you do not want to send content before that point to the user. You can also use the Response object's Redirect method to redirect the user to another page, or use the Response object's Clear method to clear the buffer and send different content to the user. The example below uses both 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 can also use Response.Buffer to prevent the Web server from returning the HTTP headers before the script modifies them. Some properties and methods, such as Response.Expires and Response.Redirect, modify HTTP headers.
When the Buffer property is set in a script and the Flush method is not called, the server will maintain Keep-Alive requests issued by the user. Developing this scripting habit can help improve server performance because the server does not have to create a new connection for each user request (assuming that the server, user, and any proxy servers support Keep-Alive requirements). However, one potential disadvantage of this approach is that the buffer does not display any response to the user until they have processed all the scripts in the current ASP file. For longer and more complex scripts, users may be forced to wait for a long time before seeing this page.
By default, buffering is turned off for ASP applications. You can use Internet Services Manager to turn on buffers for the entire ASP application.
Allows proxy servers to cache pages Through the proxy server, applications can send pages to users. A proxy server requests web pages from a Web site on behalf of the user's browser. Proxy servers cache HTML pages so that repeated requests for the same page can be returned to the browser quickly and efficiently. Proxy servers perform requests and cache web pages, and relieve the load on the network and web servers.
Although caching works well for HTML pages, it does not work well for ASP pages that contain dynamically generated information. For example, reporting on stock market conditions or inventory lists showing large volumes of business require immediate information. Information from an hour ago is very inaccurate at the moment. If the application returns personal information, for example, a customized homepage, it is expected that the user will not be able to see another user's personal information.
By default, the ASP Command Proxy server itself cannot cache ASP pages (although it caches images, bitmaps, small applications, and other references on cached pages). You can use the Response.CacheControl property to set the Cache Control HTTP header field to allow caching of a certain page. The default value of Response.CacheControl is the string "Private", which prevents proxy servers from caching this page. To allow caching, set the cache control header field to Public:
<% Response.CacheControl = "Public" %>
Because the HTTP headers must be sent to the browser or proxy server before any page content is sent, you can set the Response.CacheControl property or use a Response.Buffer to cache the page before any HTML tags.
The Cache Control header field is part of the HTTP 1.1 specification. ASP pages cannot be cached on proxy servers that only support HTTP 1.0 because no expired header fields are sent.
Prevent browsers from caching pages. Each browser version has its own specifications for whether to cache pages. To prevent the browser from caching ASP pages, use Response.Expires to set the expiration header:
<% Response.Expires = 0 %>
A value of 0 forces cached pages to expire. Because the HTTP headers must be sent to the browser before any page is sent, the page can be cached by placing the Response.Expires attribute before the HTML tag or by using a Response.Buffer.
Create dynamic channels
Internet Explorer 4.0 has a new feature that Web planners can use to combine Web pages with common themes into a channel. In the browser, the channel is displayed on the channel title bar; users access the channel by clicking on the icon. Channels update automatically in the background; users do not have to visit the site to download the latest pages in their browser. Channels provide users with a fast and direct path to browse a set of related Web pages, and these Web pages are automatically updated.
Using ASP, you can write scripts to collect the user's preferences and then dynamically create channels. A channel definition file (.cdf) establishes the organization and sequence of channel content. Commands in .cdf files use the same syntax as HTML markup, making them easier to grasp and generate from scripts. When writing an ASP script to create a channel definition file, the script uses a .cdx extension. When ASP reads a file with a .cdx extension, the application/x-cdf content type is automatically sent, which tells the browser to use the channel definition to interpret the bytes. If you do not use .cdx as the extension, the script must manually set the content type to application/x-cdf using Response.ContentType.
Here's an example of how to use channels. The following HTML form asks the user to select a channel. When submitted, the form calls a script in the .cdx file to create the channel definition.
<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 creates a channel definition based on the values of the table 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 %>
Sending files to a server A browser can use the Posting Acceptor application to send files to a Web server. When the Posting Acceptor uploads files, it sends URL-encoded form fields listing the name and location of each received file. The postal processing URL can be added to a script that uploads the file to call the ASP script to process these field names. For example, you can write a script that automatically sends an e-mail to the system administrator with the name and location of the file.