Data Buffering

When ASP code is executed, the output of the code is sent in one of two ways:

  1. output is sent as the code is executed, or
  2. output is sent after the complete code (the entire ASP file) has finished executing

How is that of any significant? It is very significant if you have a ASP file that takes longer to execute perhaps because it has to access data from multiple databases with complex queries. While the code is being executed and no output is generated for some time, what will the user think of your page? They are likely to think the page is broken and may attempt reloading or may simply leave the page (or website). Buffering is an easy way to control how and when the output is sent to the browser.

What is buffering? When the server sends the output after completely evaluating the entire ASP file, it is known as buffering. If the output is buffered, the server temporarily holds the output (i.e., does not send a response to the browser) until all of the code has been processed, or until the Flush or End method is called. Simply put, buffering prevents the server from sending any response to the browser until the server is completely done processing the code.

If you have pages that minimally use ASP code and the code executes faster, your user may not notice any difference whether or not you are using buffering. In this case, you could care less about how the data is buffered. If, however, as mentioned before your script is accessing data with complex queries from multiple databases, the user may be frustrated if no output is shown for a while. Consider buffering for better control of the output. On Windows 2000 in IIS 5, buffering is turned ON by default; however, on Windows NT 4 In IIS 4, buffering by default is turned off. You may use this statement to learn whether or not buffering is ON or OFF on your server:

<% = response.buffer %>

To change buffering setting for an entire website, you can go to the website properties in IIS to change. The following shows how to enable or disable buffering in IIS 5:

Enable or disable buffering by checking Enable buffering
You can turn ON (or OFF) buffering for a particular website by selecting (or deselecting) the "Enable buffering" check box, as shown in this window.

To access this window, access the property panel for the desired site from the IIS. On the Default Web Site Properties window click on the Home Directory tab. Then, click configuration to go to the Application Configuration window. Lastly, click on the Options tab to see a window similar to the one shown above.

You can also enable or disable buffering at the page level context. To turn buffering OFF for a single page, use this statement in the beginning of your ASP code:

<% response.Buffer = false %>

Similarly, you can turn ON buffering by placing this statement in the beginning of the code:

<% response.Buffer = true %>

The following table summaries the two possibilities of buffering:

Buffering State Description
True This state indicates buffering is enabled. Thus the server holds output until all of the scripts on the page have been processed.
False This state indicates buffering is disabled. Thus the server sends the output as your script is processed.

The following examples show buffering in use. In the first example below

<%response.Buffer=true%>
<html>
<body>
<%
for x=0 to 100000
response.write x & " "
next
%>
</body>
</html>

set buffering to true. Because in this code we are using a loop that will execute at least 100,000 times, nothing will be sent to the browser until this loop has finished executing. Once the loop is finished, the HTML output will be sent to the browser. The server in this example will hold the output until the loop has finished executing. If the server is busy with other processes, it may take sometime for the user to receive any output. For the right reasons, you may not want the user to see any output until the entire code has finished executing. But you also do not want the user to wait for any output back from the user. (Realize in the example, we are saying buffer any output until our loop has finished executing. Because the server is prevented from sending any output, how can we send any output to the user?) Well, we can use the flush method whenever we are ready to send any output:

<%response.Buffer=true%>
<html>
<body>
<p>Please wait....while the script is executing</p>
<% response.flush %>
<%
for x=0 to 100000
response.write x & " "
next
%>
</body>
</html>

Output, if any, is sent immediately to the browser when response.flush statement is processed by the server. The statement simply tells the server to empty the output buffer.

If a need arises for you to delete the output buffer, you can use the clear method. With the clear method you can start the output from scratch.

The following provides a brief description for each of the buffering method:

Buffering method Purpose
response.flush This sends to the browser whatever has accumulated in the output buffer.
response.clear This wipes out the output buffer. This allows you to start your page from scratch.