Article

Handling Submitted Data with ASP

Page: 1 2 3 4 Next

The Response Object

As the object that controls everything to do with the response that is sent back to the browser, Response contains properties and methods that support this purpose. We’ve already seen the most important and most commonly-used method in ASP - the Write method - in action, so let's look at another example of a method supported by Response: the Redirect method.

Response.Redirect "http://www.sitepoint.com/"

The Redirect method sends a special Hypertext Transfer Protocol (HTTP) code (specifically, HTTP response code "302 Object Moved") to the browser to inform it that the page it requested is actually available at a different address and that it should try loading that address instead. The address in question is provided as a parameter to this method. Thus, the line above will cause the browser to load http://www.sitepoint.com instead of the file that it originally requested.

The Redirect method is useful in situations where you want to send the user to another page. For example, when a user logs out of your site by clicking on a link to logout.asp, you might want to send the user back to the login page of your site (e.g. login.asp) after you log him or her out of the system:

<% ' logout.asp -- log out and redirect to the front page  
 
If bLoggedIn Then  
 ' ... Code to log the user out ...  
End If  
 
Response.Redirect "login.asp" ' Send back to login page  
 
%>

Because of the way the Redirect method works (sending an HTTP response code to the browser), it may not be called (invoked) after any text or HTML has been sent as part of the page. If other words, if you want to redirect the user to a different page, you must do it before you display any of this page -- you can't have it both ways.

A bunch of the other methods provided by the Response object are related to controlling how the page content is sent to the browser. But before we look at these, we must first examine a property of the Response object: Buffer.

Response.Buffer = False

As you can see in this example, an object property behaves exactly like a variable, except that you must specify the object to which it belongs followed by a dot (.) when you want to refer to it. In the above line, we’ve assigned the Buffer property of the Response object a value of False.

If the Buffer property is set to True, ASP waits for the ASP script responsible for the page to finish executing before it sends the entire page to the browser in one big chunk. If Buffer is set to false, the contents of the page are sent to the browser line by line as they are produced by the script. In general, it’s quicker to send the page all at once than to send it in bits and pieces; thus, as of ASP 3.0, Buffer is set to True by default. If your script is particularly long, however, or if you want visitors to be able to see the output of your script as it is produced, you can set the Buffer property to False using the line above.

Even if your script is very long, you'll usually want some control over what gets sent back to the browser and when. Two of the additional methods supported by Response give you that control. These methods work only when Response.Buffer is True:

Response.Flush          ' Immediately send everything accumulated  
                       ' in the response buffer so far.  
Response.Clear          ' Throw away everything accumulated in  
                       ' the buffer without sending it.

For example, if a long script was performing a series of complex calculations and you wanted to display the outcome of each computation as it was completed, you could structure your code as follows, using the Flush method:

For calcNo = 1 To 1000  
 
 ...                                 ' Perform calculation...  
 
 Response.Write "The results of calculation number " & calcNo  
 Response.Write " are as follows..."  
 ...                                 ' Print out the results...  
 
 Response.Flush                      ' Send the results now  
 
Next

The Clear method, meanwhile, is useful when something unexpected happens and you want to print out an error message. If you're in the middle of an HTML table and an error occurs in your ASP script, you can't simply print out an error message and end the script there, because the incomplete table may result in a page that is not viewable. Use the Clear method to avoid such problems - it cancels the display of your incomplete page content before printing the error message:

If errorHasOccurred Then  
 Response.Clear                   ' Clear the output buffer  
 Response.Write "An error has occurred!"  
 Response.End                     ' Terminate the page here  
End If

The End method (used above) flushes the buffer (if Buffer is set to True, that is), and then stops executing the script at that point. Another common use of the End method is to abort a long calculation if it is determined that the client (or browser) has already moved on to another page, or has otherwise stopped receiving the page. This condition is detected by checking the IsClientConnected property of the Response object:

' Long calculations...  
 
If Not Response.IsClientConnected Then  
 Response.End  
End If  
 
' More long calculations...

To learn about the other properties and methods supported by the Response object, look them up in the documentation that accompanies Internet Information Server or Personal Web Server.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links