Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ASP Programming for the Absolute Beginner

.pdf
Скачиваний:
68
Добавлен:
17.08.2013
Размер:
7.66 Mб
Скачать

46

ASP Programming for the Absolute Beginner

Welcome <%=Request.Form("Name")%> to the world of ASP!

</body>

</html>

As you can see in Figures 3.1 and 3.2, the value you enter in the box on the RequestForm.asp page is displayed within the RequestForm_Results.asp page after

FIGURE 3.1

A typical HTML form you’ve probably seen (and used) many times on your journeys around the Web.

 

 

Y

 

L

 

F

 

M

 

A

 

E

 

T

 

 

FIGURE 3.2

You can easily

process and display

form results in ASP,

using the

Request object.

Team-Fly®

you click the Submit button. This is done through the functionality and power of the Request object, as shown in the following line from Listing 3.3:

Welcome <%=Request.Form("Name")%> to the world of ASP!

What’s happening in this line of code? I’m guessing that by looking at Listings 3.2 and 3.3, you can figure this out. To reiterate, though, here is a breakdown of the preceding line of code from both an ASP and a basic HTML perspective:

1.Notice in Listing 3.2 how you designate the page to point to when processing the form results. Take note of the following line:

<form method="POST" action="RequestForm_Results.asp">

Notice the section action="RequestForm_Results.asp". By using the action attribute of a form (which is straight HTML—nothing to do with ASP), you tell the form where to go after the user clicks the Submit button and puts the form into action. You are asking this form to point to the RequestForm_Results.asp page. On this page, the information entered into the form (the name entered in the text box shown in Figure 3.1) is processed and ultimately displayed on screen.

2.When the information gets to the RequestForm_Results.asp page, it can be processed. Remember the analogy of the telephone call? That is exactly what’s happening here. The Web browser in which the user enters his or her name on the form makes a request that an action be taken by the Web server. In Listing 3.3, the following line accomplishes this:

Welcome <%=Request.Form("Name")%> to the world of ASP!

By using the form collection of the Request object (more about collections in the following section), you garner a response from the Web server—in this case, asking it to pass the information entered into the form field (which I named Name) to the RequestForm_Results.asp page.

3.Finally, after all the requesting and responding is completed, the Web browser displays the results, shown in Figure 3.2.

As you might guess, there is much more to both these objects, and you will see them in action throughout this book. In the next section, you will begin your exploration of Object collections, which will make the Request and Response objects more clear. You’ll also see how they work in conjunction with other objects to really give ASP all of its power.

Understanding Object Collections

Think of collections as special subsets of objects that expose unique information about an object for extended manipulation. In a large company, you can have a manufacturing component, a sales component, a human resources component, and so on. In making an analogy to ASP object collections, think of these company components as exposing specific attributes of the company.

So it is with the ASP object collections. Depending on the object, the number of collections varies, and each collection makes available specific information about the object.

For the Request object, the following collections are available for your manipulation:

ClientCertificate. On some pages, a user certificate is required for access to the page. This certificate contains a variety of information, from encrypted data to

47

C h a p te r

3

W o r k in g

w i t A h S P

O b je c t s

48

ASP Programming for the Absolute Beginner

basic user profile information. The ClientCertificate collection makes available this information, as requested of the Web server.

Cookies. If the Web page requesting information of the server contains cookies, those cookies (and the data they contain) are sent to the server for examination.

Form. By far the most common collection of the Request object, the form collection takes all the values, as passed by an HTML form section, that are submitted as part of the request. In Listing 3.2, the text box form element (named Name) would be included in the form collection of the Request object. As shown in Listing 3.3, the line Request.Form("Name") illustrates the form collection in action.

QueryString. Similar to the form collection, the QueryString collection makes available to the Web server all values contained in the URL of the user’s request. I discuss the QueryString in more detail later in this chapter.

ServerVariables. This collection makes available all the HTTP header values, as sent by the user with his or her request.

TRICK

Each object also has a set of unique properties that allow for even greater control of

 

 

the data being submitted by the user. You will learn about specific object properties

 

in the various code listings throughout this book.

The Request Object Form Collection

You already have an example of the form collection of the Response object in Listings 3.2 and 3.3. As you saw, it is very easy—via the form collection—to access and manipulate information submitted on a typical HTML form.

Even though that example is very simple, it shows you everything you need to know about reading form information from a Web page. However, here is a slightly more complex example, where you can see the true power of the form collection. Listing 3.4 is a regular HTML form, albeit with several form elements.

Listing 3.4 FormExample.asp

<html>

<head>

<title>A complete Request object form collection example</title> </head>

<body>

<p><b>A complete Request object form collection example</b> <hr>

<form method="POST" action="FormExample_Process.asp">

<p>Enter your name here: <input type="text" name="Name" size="20"></p>

<p>What is your gender: <input type="radio" value="M" name="Gender">M   

<input type="radio" name="Gender" value="F">F</p>

<p>In what age group do you belong: <select size="1" name="Age"> <option value="1">Younger than 18</option>

<option value="2">18-26</option> <option value="3">27-45</option> <option value="4">46-65</option>

<option value="5">Older than 65</option> </select></p>

<p>Enter some general comments about what you think about ASP:</p> <p><textarea rows="5" name="Comments" cols="50"></textarea></p>

<p align="center"><input type="submit" value="Submit" name="B1"></p> </form>

</body>

</html>

Figure 3.3 shows how this form appears.

Now see how this code is processed via the form collection of the Request object. Listing 3.5 illustrates a page that processes the information entered into the form of the page shown in Figure 3.3 by using the Request object’s form collection.

FIGURE 3.3

A typical HTML form, with several types of form elements (a one-line text box, a scrolling text box, radio buttons, and

the like).

49

C h a p te r

3

W o r k in g

w i t A h S P

O b je c t s

50

ASP Programming for the Absolute Beginner

Listing 3.5 FormExample_Process.asp

<html>

<head>

<title>A complete Request object form collection example</title> </head>

<body>

<p><b>A complete Request object form collection example - -</b> </p>

<p><i>Utilizing the Request object form collection to read the data</i> <hr>

<p align="left">Name of visitor to the Web page: <%=Request.Form("Name")%> </p> <p align="left">Gender of the visitor: <%=Request.Form("Gender")%></p>

<p align="left">Age classification of the visitor: <%=Request.Form("Age")%></p> <p align="left">Visitor comments:<p>

<i><%=Request.Form("Comments")%></i> <p align="left"> </p>

</body>

</html>

Figure 3.4 shows this page as it processes the information entered in the form illustrated in Figure 3.3.

FIGURE 3.4

The form is

processed using the

Request object’s

form collection.

As you can see in Listing 3.5, the form collection can be used to read information from any form element (text box, radio button, and so on). Because forms and form processing are described in far greater detail in Chapter 6, for now you can familiarize yourself with how form data is being passed and then manipulated via the form collection of the Request object.

The Request Object QueryString Collection

The QueryString collection works nearly the same as the form collection, except that you append values to the URL of the user’s request rather than use a form to capture and pass data.

To demonstrate this, let me refer you to Listing 3.5 in the preceding section. Say that after you pass the form information to the FormExample_Process.asp page, you want to pass that information to another page, this time using the QueryString collection, shown in Listing 3.6.

Listing 3.6 FormExample_Process2.asp

<html>

<head>

<title>A complete Request object querystring collection example</title> </head>

<body>

<p><b>A complete Request object querystring collection example - -</b> </p>

<p><i>Utilizing the Request object querystring collection to read the data</i>

<hr>

<p align="left">Name of visitor to the Web page: <%=Request.Querystring("Name")%> </p>

<p align="left">Gender of the visitor: <%=Request.Querystring("Gender")%></p>

<p align="left">Age classification of the visitor: <%=Request.Querystring("Age")%></p>

<p align="left">Visitor comments:<p> <i><%=Request.Querystring("Comments")%></i><hr>

</body>

</html>

51

C h a p te r

3

W o r k in g

w i t A h S P

O b je c t s

Figure 3.5 shows how this code appears when executed.

52

ASP Programming for the Absolute Beginner

FIGURE 3.5

This page looks exactly the same as that shown in Figure 3.4, which uses the form collection of the Request object.

As you see, you get the same results using either the form or QueryString collection of the Request object. However, you should be aware of several issues when deciding which of these collections to use:

Compare the URL in Figure 3.4 to the one in Figure 3.5. In Figure 3.4, there is no sign that any data has been passed. All the information entered by the user on the form is processed via the Request object form collection. Using the form collection is a more secure method of passing information; nothing is visible as it is passed.

Depending on the type of Web browser being used (and, in some cases, the limitations of the Web server), there can be a limit on the size of the URL. For example, if you use the QueryString collection to pass an excessively large amount of information, not all of it may be transferred via the appended URL.

The appended URL cannot contain any spaces. Spaces can result in errors when processing. Although Internet Explorer (usually) automatically converts a space to a + sign, other browsers might not be able to handle this type of advanced URL processing.

TRICK

For all the reasons listed here, you should always try to use the form collections and

 

 

the POST method to pass information. You will be learning much more about process-

 

ing forms (including the POST method) in ASP in Chapter 6, “Using Forms.”

Introducing the File Object

The capability of your Web pages to write to a text file is a powerful attribute of ASP. Say that you have a Web-based survey (using a typical Web form) that asks visitors to analyze your Web site’s design and functionality. After the form is completed and the

user clicks the typical Submit button, you want the user’s information to be saved to a text file so that you can review it later. Say that you own a small company that specializes in computer training. On your Web site, you have an online registration form that folks interested in taking your classes can complete, and it sends their registration information to them in a text file (so that they can print it out and bring it with them to class). The capability to write to a text file would be perfect for this example.

The possible uses of this capability go on and on. To understand the functionality of the File object (or the ASP object you use to access all this file manipulation capability), you will program the ASP MadLibs game in this chapter. Remember MadLibs? This is the word game that asks you to think of several verbs, nouns, and adjectives. Then, the seemingly innocent list of words you generate is inserted into a prefabricated story, often with very funny results. For example, say that you are asked for two nouns, two verbs, and two adjectives. Your list of words is then inserted into the following story:

One day, (noun) was walking down the street. Suddenly, he (verb) the

(noun) he was carrying and (verb) into the nearest store. All this

activity gave him a/an (adjective) appearance, so he (adverb) walked to the

nearest phone and called his mother!

As you can see, with the list of words you provide, this story can take on humorous connotations. You will learn how to program a Web-based version of this game, using the File object to create your final story. As a mechanism for requesting (and collecting) the various words, this is a preview of using forms in ASP.

Although the File object has several properties and characteristics, special focus in this chapter is on using the File object to

Read from a text file.

Write to a text file.

The File Object Properties

As you did with the Request and Response objects, you will begin by examining the specific properties of the File object:

Attributes. Returns the various attributes of a file. Such attributes include whether the file is read-only and hidden, its name, and its location (in which folder it resides).

DateCreated. Tells the date and time the file was created.

DateLastAccessed. Tells when the file was last accessed.

DateLastModified. Tells when the file was last modified.

TRICK

Remember that DateLastAccessed and DateLastModified are quite different. When a

 

 

file was last accessed means when it was last opened or examined. DateLastModified

 

refers to the most recent date the file was altered, not simply opened or examined.

Drive. Tells you on which drive the file resides.

Name. Returns the name of the file.

53

C h a p te r

3

W o r k in g

w i t A h S P

O b je c t s

54

ASP Programming for the Absolute Beginner

ParentFolder. Tells you the folder in which the file resides.

Path. Returns the absolute path of the file’s location.

ShortName. Tells the DOS-style name of the file.

ShortPath. Shows the DOS-style path of the file.

Size. Returns the size of the file in bytes.

Type. Tells the file type (for example, .txt for a text file).

As you progress through the following sections (especially as you code the ASP MadLibs game example), you will see most of these properties in action.

The File Object Methods

The File object also has its own set of methods:

Copy

Delete

Move

CreateTextFile

OpenAsTextStream

Of these methods, you are going to be looking specifically at the CreateTextFile and OpenAsTextStream methods because within your ASP code, they allow you to create a new file and then read from, write to, or otherwise append the file.

A File Object Example

Listing 3.7 gives a quick example of how to work with the File object, its properties, and its methods.

Listing 3.7 FirstFile.asp

<html>

<title>Working with the File Object</title> <body>

<b>This is a first example of working with the File object!</b> <hr>

<%

set TestFile=Server.CreateObject("Scripting.FileSystemObject")

set TFileStream=TestFile.CreateTextFile("c:\inetpub\wwwroot\ABG_ASP\ TestFile.txt")

TFileStream.WriteLine "Welcome to the File Object in ASP!"

TFileStream.Close

%>

</body>

</html>

To ensure that you understand everything in this code, go ahead and work with it directly now:

1.From the CD-ROM, open the file FirstFile.asp, and save it in your ABG_ASP folder within the wwwroot directory.

2.Open your Web browser of choice, and navigate to this file. When the page loads within your Web browser, it looks like Figure 3.6.

3.After the page loads, the text file you created via the VBScript code (I’ll explain this in a moment) is created in your ABG_ASP folder. Navigate to that folder now, and find a text file named TestFile.txt. Open this file within Notepad (or your favorite text editor). It looks like Figure 3.7.

How does the code in Listing 3.7 work? Very simply, as you will see here:

1. An instance of the File object is created. This is done via the line

set TestFile=Server.CreateObject("Scripting.FileSystemObject")

Note that the TestFile name given to this particular instance is just a random name. You can call it anything you want, but it’s a good idea to keep it as short as possible for easier manipulation within the rest of your code.

2.The file is created as you specify the exact path where you want the file created. This is done in the line

TFileStream=TestFile.CreateTextFile("c:\inetpub\wwwroot\ABG_ASP\TestFile.txt")

In this example, you are creating the file (TestFile.txt) directly within your ABG_ASP development folder, but note that you can create the file anywhere you

FIGURE 3.6

As with most ASP functionality, most of the exciting stuff—in this case, the VBScript code that manipulates the

File object— occurs behind the scenes, with only simple HTML being output to the Web browser.

55

C h a p te r

3

W o r k in g

w i t A h S P

O b je c t s