Home

ASP ADVANCED

This chapter details how to work with a single Active Server Page.  The first section provides an overview of the Request and Response objects.  In the second section, you learn how to buffer the output of your Active Server Pages.  The third section describes some methods for working with long-running scripts and very large HTML pages.  Finally, in the fourth section, you learn how to work with HTTP headers and server variables.

The Request and Response Objects

This section introduces the Request and Response objects.  These two objects are the ones that you'll use most often in your Active Server Pages scripts.  To understand how to use these objects, however, you first need some background on the HTTP protocol, which is provided in the following section.

The HTTP Protocol

How does the World Wide Web really work?  When you type the address of a Web page in your Web browser, if all goes smoothly, the Web page appears.  For example, if you type the Internet address of HotWired into your Web browser, the home page of the HotWired Web site appears in your browser window.  What goes on in the background to make this happen?

When you use a browser to retrieve an HTML page from a Web site, you're using the Hypertext Transfer Protocol(HT7P).  The HTTP protocol specifies how messages can be transported over the Internet.  In particular, the protocol specifies the ways in which a browser and a Web server can interact.

When you retrieve a page from a Web site, your browser opens a connection to a Web server at the Web site and issues a request.  The Web server receives the request and issues a response.  For this reason, the HTTP protocol is called a request and response protocol.

All communication between a browser and a Web server takes place in discrete request and response pairs.  The browser must always initiate the communication, by issuing a request.  The Web server's role is completely passive; it must be nudged into action by the request.

A browser request has a certain structure.  A request message contains a request line, header fields, and possibly a message body.  The most common type of request is a simple request for a Web page, as in the following example:

GET /hello.htm HTTP/1.1

Host: www.aspsite.com

This request message is a request for the Web page hello.htm at the Web site www.aspsite.com.  The first line is the request line.  The request line specifies the method of the request, the resource being requested, and the version of the HTTP protocol being used.

In this example, the method of the request is the GET method.  The GET method retrieves a particular resource.  In this case, the GET method is being used to retrieve the Web page hello.htm.

Other types of request methods include POST, HEAD, OPTIONS, DELETE, TRACE, and PUT.  Only GET and POST are commonly used.  The POST method is used to submit the contents of an HTML form.

The second line in this example is a header.  The Host header specifies the Internet address of the Web site where the hello.htm file is located.  In this case, the host is www.aspsite. com.

Typically, a request will include many headers.  Headers provide additional information about the content of a message or about the originator of the request.  Some of these headers are standard; others are browser-specific.  The later section "Working with Headers" covers these specific topics.

A request may also contain a message body.  For example, if the request uses the POST method rather than the GET method, the message body may contain the contents of an HTML form.  When you click the submit button on an HTML form, and the form uses the ACTION='POST' attribute, any data you entered into the form is posted to the server.  The form contents are sent within the message body of the request, using the POST method.

When a Web server receives a request, it returns a response.  A response also has a certain structure.  Every response begins with a status line, contains a number of headers, and optionally may contain a message body.

You're probably already familiar with the status line.  If you have ever requested a Web page and mistyped the address, you've seen an example of a status line (see Figure below).  A status line indicates the protocol being used, a status code, and a text message (the reason phrase).  For example, if a Web server has problems with a request, it returns an error and a description of the error in the status line.  If a server can successfully respond to a request for a Web page, it returns a status line that contains 200 OK.

An example of a status line

An example of a status line

Response headers contain information about the content of the response or information about the server providing the response.  Some of these headers are standard; others depend on the Web server.  For more about headers, see the later section "Working with Headers."

Finally, the message body of a response typically contains the contents of a Web page.  For example, if the request was for the Web page hello.htm, the message body of the response would contain hello.htm.  However, a message body can contain other types of content as well (text documents, Microsoft Word documents, and so on).

The Request and Response Objects

Active Server Pages includes two built-in objects that correspond to the request message and response message of the Hypertext Transfer Protocol.  The Active Server Pages Request object corresponds to an HTTP request.  The Active Server Pages Response object corresponds to an HTTP response.

Like most Active Server Pages objects, the Request and Response objects have collections, properties, and methods.  By using the collections, properties, and methods of the Request object, you can retrieve information on all aspects of a browser request to your Web server.  By using the collections, properties, and methods of the Response object, you can control almost all aspects of the response of your Web server.

For example, the Request object has a collection that contains all the HTTP headers in a request.  The Response object includes a number of methods for modifying response headers.  Again chapters provide more details about how to use these two objects. Just Click on Next button.