Creating Your First Page with ASP
JUN 14, 2001 By Christoph Wille. Article is provided courtesy of Sams.

Active Server Pages is a very powerful and yet easy-to-learn server-side scripting environment. Active Server Pages comes with Internet Information Server for Windows NT Server, and with Personal Web Server for Windows NT Workstation and Windows 98. This environment enables you to create a Web site that is dynamic, fast, and interactive without requiring you to worry about the capabilities of your clients' browsers, which you must do if you rely on client-side scripting like client-side JavaScript or client-side Visual Basic Script (VBScript).

In this hour, you will learn the following tasks:

What You Need to Run ASP

ASP is available on multiple platforms. With all Microsoft platforms presented in this hour, I recommend you use at least Microsoft Personal Web Server (PWS) for Windows NT Workstation to develop Active Server Pages applications because of its superior functionality over PWS for Windows 98. However, to use all features--such as the full-text search in the ASP documentation--you should install Microsoft Internet Information Server (IIS) version 4.0 on Windows NT Server for development.

If you have only a small computer available for development, and Windows NT Server is out of reach, you can take your first steps in the world of ASP using the PWS for Windows 98 or Windows NT Workstation.

Personal Web Server (PWS)

Personal Web Server (PWS)is a Web server that was developed for companies or individuals who need a down-scaled IIS for intranet applications or development. Unlike IIS, a PWS can host only one Web site. PWS' advantage is that it can be run on an inexpensive Windows 95, Windows 98, or Windows NT Workstation operating system.

Installing PWS on Windows 98

For certain purposes, you might want to install PWS on Windows 98 if it is the only operating system available to you, or you want to take the first steps in ASP programming. To install PWS on Windows 98, you only need the Windows 98 operating system CD because PWS is contained on this CD.


NOTE: If you want to install PWS on a Windows 95 computer, you must install it from the Windows NT 4.0 Option Pack, which also includes a Windows 95 version of PWS.


If you already have installed Windows 98 on your computer, proceed as follows to install PWS on your Windows 98 operating system.

To Do: Install PWS on Windows 98

1. Put the Windows 98 CD in your CD-ROM drive.

2. Click the Start button and choose Run.

3. In the Run dialog box, type X:\add-ons\pws\setup.exe in the Run input field. Substitute the letter of your CD drive for X, and click OK.

4. The Personal Web Server Setup window appears (see Figure 1.1). Click Next.

FIGURE 1.1 The Personal Web Server Setup window.

5. Click the Custom button in the configuration menu (see Figure 1.2).

FIGURE 1.2 Personal Web Server Setup options.

6. Add the ADO documentation (about database access): Double-click Microsoft Data Access Components, and in the active window on MDAC: ADO, ODBC, and OLE DB. Then check the ADO documentation to be installed (see Figure 1.3).

FIGURE 1.3 Add a checkmark next to ADO Documentation in setup.

7. Click OK to close the window and OK in the second window to close it as well. It is recommended for development to install ADO documentation because you will need the documentation to develop database-driven ASP pages.

8. Then add the Active Server Pages documentation. To do so, double-click Personal Web Server (PWS), and double-click Documentation. Checkmark Active Server Pages in the window that appears (see Figure 1.4).

FIGURE 1.4 Checkmark Active Server Pages Documentation in Setup.

9. Close the two windows by clicking OK.

10. Click Next in the PWS Setup Window.

11. Notice the default Web publishing home directory, X:\Inetpub\wwwroot, where X: is a drive letter. Click Next to accept the default value (when you change it, remember that all procedures in this book refer to the default location).

12. Leave the Microsoft Transaction Server (MTS) install folder to its default value, and click the Next button.

13. The PWS setup is completing the installation. When the installation is finished, you can end setup with a click on the Finish button.

14. Restart the computer now to be able to use PWS.

After restart, you can use the PWS. Start the PWS, where you can control it using Start, Programs, Microsoft Personal Web Server, Personal Web Manager. In the left frame, you see the icons for the five available areas that appear in the right frame (see Figure 1.5).

FIGURE 1.5 The main section of Personal Web Manager.

FIGURE 1.6 The Personal Web Manager Publishing Wizard.

FIGURE 1.7 The Personal Web Manager Home Page Wizard.

FIGURE 1.8 The Personal Web Manager Product Tour.

FIGURE 1.9 The Personal Web Manager advanced options.

A virtual directory is a directory that doesn't have to be located within your home directory; in fact, it could reside on another disk or computer, but it can be accessed by a client browser through the Internet as if it were located within your home directory. For further details, see the section "Creating Virtual Directories" in Hour 2, "Introducing Internet Information Server."

Installing PWS on Windows NT Workstation 4.0

It is necessary to apply Service Pack 3 (or higher) for Windows NT to your workstation and install Microsoft Internet Explorer (IE) 4.01 (or higher) first before you can install PWS.


NOTE: IE 4.01 is the minimum requirement; however, you will learn features in this book that are available only in IE 5.0. Therefore, it is strongly recommended that you install IE 5.0.


PWS is included in the Windows NT 4.0 Option Pack. Therefore, you must install the Option Pack to get PWS on your workstation.


NOTE: You can download Windows NT 4.0 Option Pack from Microsoft's Web site. The Web address for download is http://www.microsoft.com/ntserver/nts/_downloads/recommended/NT4OptPk/default.asp (that's 4O, not four zero in the address).


Be sure to also install the Internet Service Manager (ISM) by performing the following Windows NT 4.0 Option Pack setup steps:

1. Start Windows NT Option Pack setup.

2. Click the Custom button in the configuration menu (refer to Figure 1.2).

3. Add the Internet Service Manager: Double-click the Personal Web Server (PWS) component and checkmark Internet Service Manager (see Figure 1.10).

FIGURE 1.10 Subcomponents of PWS--Internet Service Manager.


TIP: To install the Internet Service Manager (ISM) is some good advice to take to become familiar with this tool. If you decide to work with IIS under Windows NT Server, the ISM is necessary for Web server administration. Also you cannot use Microsoft Script Debugger to find errors in your script if you have not installed ISM.

It is also useful to install the whole Online Documentation subcomponent of PWS and the whole Microsoft Data Access Components 1.5. This will make it easier for you to get answers to questions that are raised during work.


4. All other setup settings might be left to their defaults. Click the Next button.

5. You will see the folders of the Web publishing home directory and the Application Installation Point (see Figure 1.11).

FIGURE 1.11 Web publishing home directory and Application Installation Point _folders default setup.

6. Click Next, and you will see the MTS install folder. Click the Next button.

7. Configure the administrative Account as local and click the Next button.

8. Windows NT 4.0 Option Pack Setup is completing installation.

9. Click the Finish button and restart your computer to use PWS.

10. ISM, the administrative interface of PWS, can be found using Start, Programs, Windows NT 4.0 Option Pack, Microsoft Personal Web Server, Internet Service Manager.


NOTE: It is recommended to install Windows NT Service Pack 4 after installing PWS. You can download Service Pack 4 from Windows NT from the Microsoft Web site at http://www.microsoft.com/support/winnt/default.htm. Keep in mind that you must reapply Service Pack 4 after you install new features from Windows NT operating system or from Windows NT 4.0 Option Pack.


Internet Service Manager (ISM) is the name for the IIS administration tool. ISM on Windows NT is available in the form of an IIS snap-in to the Microsoft Management Console.

The Microsoft Management Console (MMC) is a common management console that provides a consistent framework that can be used for all network administration programs, like the IIS or the MTS. A screenshot of the MMC with the IIS snap-in is provided in Figure 1.12. Because you can manage Internet services like PWS or IIS from the MMC, it is often referred to as Internet Service Manager.

FIGURE 1.12 MMC with the IIS snap-in opened.


NOTE: For the difference between PWS for Windows NT Workstation and PWS for Windows 95/98, refer to Hour 2.


In Windows NT Workstation, the MMC with the IIS snap-in can be accessed through Start, Programs, Windows NT 4.0 Option Pack, Microsoft Personal Web Server, Internet Service Manager if installed. You can configure your Web site more precisely with MMC than it would be possible using Personal Web Manager.

Microsoft Transaction Server, or MTS, is a transaction processing system for building, administering, and deploying robust Internet server applications. A transaction is a set of processes that must be executed either all or none. For example, if you pay with your credit card, there are two processes:

Either both processes or none of these processes must be executed, or there would be a magic money increase or decrease.

Internet Information Server

Microsoft Internet Information Server (IIS) is a Web server that integrates into the Windows NT Server. IIS allows you to publish information on the Web and to run multiple business applications using ASP.

Installing IIS on Windows NT Server 4.0

Make sure to have Service Pack 3 (or higher) and Windows Internet Explorer 4.01 (or higher) installed on your Windows NT Server before you install the Option Pack. It is recommended to install Internet Explorer 5 instead of 4.

Similar to PWS on Windows NT Workstation, IIS 4.0 is contained in the Windows NT 4.0 Option Pack. Therefore, you must install the Option Pack to run IIS 4.0. IIS is administered using the ISM, which is in fact the IIS snap-in running in the MMC framework.

I recommend developing and testing your ASP site using IIS on a Windows NT server. IIS provides the Microsoft Index Server, which is also needed to allow a full text search in the Windows NT Option Pack documentation that includes IIS and ASP documentation. Therefore, you are able to find information on a certain topic much faster without knowing the full structure of the Option Pack documentation.

Microsoft Index Server indexes the contents and properties of documents. In addition to indexing Web pages, it also indexes Microsoft Word or Excel documents. Using the capabilities of Microsoft Index Server, you can provide a full-text search for areas of your Web site running on an IIS. To install IIS, start Windows NT 4.0 Option Pack setup. Make sure you install Script Debugger and all needed online documentation.

I recommend applying Service Pack 4 for Windows NT after installing IIS. You must reapply Service Pack 4 to your computer when you install a new component of the Windows NT operating system or the Windows NT 4.0 Option Pack.

Write a Little ASP Example

You bought this book because you already mastered HTML page development and want to make the pages more dynamic with database binding and more. This is why you are learning Active Server Pages. ASP pages are nothing more than HTML files interspersed with server-side script code. An ASP file has an .asp extension instead of .htm or .html to denote server-side code to the hosting Web server. Because an ASP file is a text file with the extension .asp that contains a combination of text, HTML tags, and ASP script commands, you can use every text editor to create or change an ASP file.

It is time to practice. Open Notepad or another text editor, and type the lines from Listing 1.1. Do not type the line numbers! Type Output:<BR> instead of 3: Output:<BR>. Line numbers are used to reference the lines of the code during explanation of the script.

LISTING 1.1  A SIMPLE ASP FILE THAT COMPUTES THE NUMBER OF SECONDS IN AN HOUR AND RETURNS THE RESULT TO THE CLIENT

 1: <HTML>

 2: <BODY>

 3: Output:<BR>

 4: <%

 5: intS = 60*60

 6: %>

 7: There are

 8: <%

 9: Response.Write intS

10: %>

11: seconds in an hour.

12: </BODY>

13: </HTML>

Save Listing 1.1 as asp24h.asp in the home directory of the default Web site that was installed with PWS or IIS. The path of the home directory is X:\InetPub\wwwroot, where X is the operating systems hard disk in a default installation.

Congratulations! You have written an ASP file yourself and deployed it on the server by saving it in the Web server's home directory.

You can access the ASP page asp24h.asp by opening the following URL in your Web browser:

http://localhost/asp24h.asp

The result of the processed ASP page asp24h.asp appears as follows:

Output:

There are 3600 seconds in an hour.

Take a closer look at the ASP file in Listing 1.1. In lines 1 and 2, there are _common HTML tags. The third line consists of text and an HTML tag.

Output:<BR>

Line 4 includes the characters <%, which indicates the beginning of a server-side script block. The whole script block is

<%

intS = 60*60

%>

The <% and %> characters, called delimiters, enclose server-side script commands of the default scripting language, which is Visual Basic Script (VBScript) unless you changed the settings of IIS.

Line 5 is a statement that assigns the value of the multiplication 60*60 to the variable intS.

intS = 60*60

Line 6 closes the script block.

Another line of text follows in line 7.

There are

The last script block in lines 9-11 write the content of the variable strS to the browser.

<%

Response.Write intS

%>

The final line of text follows in line 11.

seconds in an hour.

As you can see now, an ASP page is an HTML page extended by ASP statements. The ASP statements also control the appearance of text and HTML tags outside the script blocks, which are just performed in the program flow of the server-side script. ASP controls the appearance of the Web page with every call of the ASP file.

You can call the default Web site of your PWS or IIS using an address like http://localhost/ when the browser is running on the same machine as the PWS or IIS. You can also use an address like http://IP-Address, where IP_Address is the Internet Protocol address of the computer that is running PWS or IIS. Note that one or more Internet Protocol (IP) addresses are assigned to each Web site and the IP addresses also must be assigned to the network adapter of the computer hosting the Web sites.

The unique IP (Internet Protocol) address identifies a node (such as a workstation or a server) in a network. It is also used to specify routing information from one network or subnet to another network or subnet. You can also use domain names in the Internet instead of IP addresses when calling a Web page. This is made possible through Domain Name System Server, which provides the correct IP address for a given domain name. In most of the figures in this book, the domain name www.asp24h.com is used instead of an IP address like 127.0.0.1. You cannot use the domain name www.asp24h.com to run the examples in this book unless you have installed and configured WINS (Windows Internet Naming Service) or DNS (domain name service). Use the domain localhost or its IP address 127.0.0.1 instead when you are using a browser that is running on the same computer as PWS or IIS.

Handling an ASP File

You can edit an ASP file with every text editor as long as you save it like a text file with the .asp extension instead of .txt. There are also several software products available that support developing ASP applications. More about ASP development software is presented in Hour 3, "Working with ASP."

The Browser's Result of a Computed ASP File

How is the magic of an ASP file done? You will examine the processing of an ASP file, from its storage on the hard disk, to being handled in IIS, to sending the HTML result to the browser.

The basic information to take away from this section is that processed ASP pages are plain HTML after the Web server has finished processing the ASP page. See Figure 1.13 for a schematic overview of how this processing is done.

FIGURE 1.13 Processing of an Active Server Page request.

The following list walks you, step-by-step, through what happens if the client requests the page xyz.asp:

1. The client requests the page xyz.asp from the server. The Web server checks the file extension to see whether a special program (such as the Active Server Pages engine) must be invoked to process the request. If there's an .asp extension, the Web server determines that it should invoke ASP to process this page.

2. If this page has never been requested before or has been changed since the last request, it must be parsed and the syntax checked and compiled by the Web server. Otherwise, the page might be read from a cache of recently processed pages, which aids in performance. During the parsing process, the HTML and scripting code are separated. IIS determines which scripting engine is responsible for which part of the script and delegates the work of syntax checking and compiling to the proper scripting engine (such as VBScript).

3. Now the code is executed by the scripting engine using resources from IIS, which is hosting the scripting engines. All objects that the language engine cannot handle are requested from IIS, which is also responsible for handling inputs and outputs for external ActiveX objects that are created and used inside the script. If it is not able to supply the object, an error is generated.

4. Script output and static HTML code in the ASP file are merged.

5. The final HTML is sent back to the user in an HTTP response.

An ActiveX object provides--as every component does--reusable code stored in a wrapper, which provides access to the object through properties, methods, and events. Additionally, the code of the ActiveX object is running in the same process space as the application that uses it. ActiveX provides a standard mechanism to extend any programming or scripting language using the same reusable object. ActiveX Data Objects (ADO) are ActiveX objects that provide an interface to OLE DB, so you can access nearly any database through ADO. ADO is the first choice if you want to access a database from an ASP file.

Summary

In this hour's lessons, you got an overview about how to install PWS or IIS. You wrote an ASP file, deployed it on the default Web server, and called it using a Web browser. You also learned a lot of new terms. The following are the most important terms that will come up, over and over again, in this book:

Q&A

Q Where can I get Windows NT 4.0 Option Pack and Service Pack 3 for Windows NT?

A Ask your local software dealer for Windows NT 4.0 Option Pack and Service Pack 3 or download the software from Microsoft. Windows NT Service Pack 3 is available at http://support.microsoft.com/support/downloads/LNP400.asp. Windows NT 4.0 Option Pack can be obtained on http://www.microsoft.com/ntserver/nts/downloads/recommended/NT4OptPk/default.asp.

Q I want to host more than one Web site on my Web server. Can I use PWS?

A You can host only one Web site on PWS. To host multiple Web sites on the same computer, you have to use Windows NT Server and IIS.

Workshop

The quiz questions and exercises are provided for your further understanding. See Appendix A, "Answers," for the answers.

Quiz

1. Is an ASP file processed on the server side (Web server) or on the client side (Web browser)?

2. Is any ASP code of an .asp file transmitted from the server to the browser?

3. Which URL do you call in the browser when you want to access the default Web site?

Exercise

Explore the files in a directory of the default Web site in PWS or ISM. To explore directories in PWS, you must view the Advanced Option section, select a directory, right-click it and select Explore. To explore directories using ISM, double-click the Internet Information snap-in and the icon of your computer. Then you can browse a directory simply by opening it.