Previous Section Table of Contents Next Section

The Request Object

If the Response object provides a means of producing output, you might suspect that the Request object allows you to work with user input-and you'd be right. Primarily, the Request object lets you access cookies, and lets you access information entered into HTML forms.

HTML Forms

Whenever you add an HTML form field to a Web page, you give it a name. For example, <INPUT TYPE="TEXT" NAME="ComputerName"> creates a text box named "ComputerName." When that form is submitted back to the Web browser (when the user clicks a Submit button), whatever the user entered is paired with the form field name to make it more readily accessible to you.

ASP exposes form fields through its Forms collection. You can use these just like read-only variables, as shown in this snippet.


The computer name you entered was:

<% Response.Write Request.Forms("ComputerName") %>

Any fields not filled in by the user will be blank, and equal to "". You can use Request.Forms in logical comparisons, to produce additional output (as in the preceding example), and so forth. You'll see plenty of the Request object in the full-length sample later in this chapter.

TIP

As a shortcut, you can omit Forms when reading forms input. For example, instead of typing Request.Forms("ComputerName"), you could simply type Request("ComputerName") and get the same results. I'll use that technique in most of my examples to save space.


Cookies

Just as Response.Cookies allows you to write cookies to client Web browsers, Request.Cookies allows you to read them back out again. For example, if you've saved a crumb named "UserName," Request.Cookies("UserName") will read the crumb. Here's an example.


<HTML><BODY>

You requested account creation for user name 

<% Response.Write Request.Cookies("UserName") %>.<BR>

Request.Cookies can be used anywhere in a script as it doesn't attempt to modify the HTTP headers being sent to the client.

    Previous Section Table of Contents Next Section