To create ASP files, you can use a text editor such as Notepad or a more advanced tool such as Visual InterDev. In any case, your ASP file can consist of any mix of HTML and code. However, some of your pages will probably contain all code, whereas others will have only standard HTML. The code you write in an ASP file is script code, like the VBScript code discussed in Chapter 30, "Using VBScript."
In the following section, you create and experiment with a simple ASP file. Subsequent sections explore more involved issues in creating ASP files.
Creating a Simple ASP File
Using the same code shown earlier in Listing 31.1 and the virtual /test directory you created in the preceding section, you can create a basic ASP file and experiment with using it. Follow these steps:
Enter the following code in Notepad or another text editor:
Name the file THETIME.ASP and save it to the /test directory.
Open the file in your browser using the URL http://myserver/test/thetime.asp,
<HTML> <BODY> The Current date and time is <% Dim sTime sTime = Now Response.Write(sTime) %> </BODY> </HTML>
where myserver is the name of your NT server or PC.
If your code does not run, but is instead displayed onscreen, make sure that the file has an .ASP extension and that the virtual directory has the correct permissions.
Microsoft's Web server software allows you to specify default document names (usually DEFAULT.ASP and DEFAULT.HTM). This means a user does not have to carry the URL out to the file levelfor example, http://myserver/test/ automatically displays the DEFAULT.ASP or DEFAULT.HTM file if they exist in the /test directory.
Using Server-Side Scripting Tags
When processing an ASP file, the Web server executes only the code designated as server-side script. All other parts of an ASP file are sent directly back to the browser. In your ASP files, the server-side code can be marked in a couple of different ways:
Use the HTML <SCRIPT> tag with the RUNAT=SERVER option.
Use <% and %> to mark the beginning and end of the server-side script. This syntax is shorter and is the one I recommend.
However you decide to mark the script, make sure to place it within the script delimiters, as in the following examples:
<SCRIPT LANGUAGE="VBSCRIPT" RUNAT="SERVER"> Dim i For i = 1 to 10 Response.Write ("This is line " & i & "<BR>") Next </SCRIPT>
The same code can also be marked with the shorter tags:
<% Dim i For i = 1 to 10 Response.Write ("This is line " & i & "<BR>") Next %>
Notice that you do not need to specify a scripting language in the second example. This is because the default scripting language for ASP files is VBScript. However, the default scripting language is a configurable option, so you might want to place the following line at the beginning of each of your ASP files:
<%@ LANGUAGE="VBSCRIPT" %>
This line is a directive to the script engine that sets the default language to VBScript for the current ASP page. Although your script tags can be placed anywhere in the ASP file, there are a few restrictions:
Certain directives and options, such as the previous line of code and the Option Explicit statement, must be placed at the very beginning of the file, even before the <HTML> tag.
If your page is used to redirect the user to another side, you cannot send any HTML back to the browser. (You get a demonstration of this in an upcoming section.)
You cannot enter straight HTML inside the script tags, unless you use the Response.Write statement.
Also, keep in mind that the code you are entering is VBScript, not the full-featured Visual Basic language. There are some differences and limitations, as discussed in Chapter 30, "Using VBScript."
See "The VBScript Language," p. 673.
Simple but Dynamic Web Pages
Now that you know the basics, let's write another simple ASP file. At my office, there is a computer named cdtower which, as you might have guessed, is attached to a tower of CD-ROM drives. The purpose of the machine is to allow people on the LAN to connect and install software. However, because people are always swapping and borrowing CD-ROMs, it is hard to know at a given time which CD is in each drive. It would be great if there were a way to automatically keep track of the CDs on a Web page. Fortunately, with Personal Web Server and Active Server Pages, this task is very easy to accomplish.
You use VBScript's FileSystemObject to determine the volume labels of each drive on a computer. By placing this VBScript code in an ASP file where the code executes on the server, you can have the volume labels of the server's CD drives displayed on a remote browser. The code, shown in Listing 31.2, is fairly straightforward.
Listing 31.2 VOLLABEL.ASP
<HTML> <BODY> <H1>Drive List</H1><HR> <% Dim objFileSys Dim objDrives Dim objDrive 'IGNORE DISK NOT READY ERRORS On Error Resume Next 'CREATE REFERENCES TO FILE SYSTEM AND DRIVES COLLECTION Set objFileSys = Server.CreateObject("Scripting.FileSystemObject") Set objDrives = objFileSys.Drives 'DISPLAY VOLUME LABELS For Each objDrive in objDrives if objDrive.DriveLetter > "C" Then Response.Write ("The CD in Drive " & objDrive.DriveLetter) Response.Write (" is " & objDrive.VolumeName & ". <BR> ") End If Next 'CLEAN UP! Set objDrives = Nothing Set objFileSys = Nothing %> </BODY> </HTML>
When a user accesses this page, the section of VBScript code executes on the Web server. The Web page in Listing 31.2 accomplishes its purpose, but leaves a lot to be desired in terms of formatting. It would appear neater if you used some HTML formatting codes, such as the <TABLE> tag. This can be easily accomplished by modifying the Response.Write statements. (As you probably have gathered by now, Response.Write is used in an ASP file to send HTML back to the browser.)
In your example, you display the VolumeID property of the Drive object. It would be nice to also display a more informative description. You can accomplish this by creating a custom function, sDescription, which returns a description for a given volume:
Function sDescription(sVolID) Select Case UCase(sVolID) Case "BACKUP0398" sDescription = "Backup of critical files 3/98" Case "DN_60ENUD2" sDescription = "Developer's Network CD #2" ' ' Add more descriptions here ' Case Else sDescription = "Unknown" End Select End Function
You can create your own subroutines and functions in ASP files, just like Visual Basic. To create the sDescription function, simply enter the function at the top of the section of VBScript code. The scope of the function will be for the current ASP page; you can call it just like a standard VB function:
The final ASP page, with table and description enhancements, is shown in Figure 31.3.
An ASP file displays disk information.
Using Include Files
When you write VBScript code in Active Server Pages, you can create functions and subroutines at the beginning of your page, and call them throughout the Active Server Page. You might, for example, create a subroutine that accepts a number and generates HTML based on the number:
Sub DisplayAmount(nAmount) Dim sHTML sHTML = "<FONT " If nAmount < 2000 Then sHTML = sHTML & "COLOR=RED" If nAmount > 4000 Then sHTML = sHTML & "COLOR=GREEN" sHTML = sHTML & ">" sHTML = sHTML & FormatCurrency(nAmount) sHTML = sHTML & "</FONT>" Response.Write sHTML End Sub
The DisplayAmount subroutine accepts a numeric value and then colors and formats it based on the value itself. Procedures like this one would be very useful in many ASP files, but there is no way to declare this subroutine as public to all ASP files. You could cut and paste it into each file, but maintaining all the copies of it would become a chore. The answer to this problem is to move the function to a separate file and include it where necessary. To include other files in the current ASP file, use the #INCLUDE statement. Two example files are as follows:
<!#INCLUDE VIRTUAL = "/test/formatfuncs.asp"> <! #INCLUDE FILE="C:\Inetpub\wwwroot\test\formatfuncs.asp" >
The first line of code uses the virtual directory to specify an include file. The next statement uses the physical directory. Either way, the end result is that the contents of formatfuncs.asp are inserted into the current ASP file before script execution. Some common uses of the #INCLUDE directive are to add footers at the bottom of each Web page, such as the company name or e-mail address.