Special Edition Using
Microsoft Visual InterDev

Chapter 29
Server-Side Scripting

In this chapter, you will learn:

Server-Side scripting is the most exciting product that Microsoft has produced in quite a while. They call it Active Server Pages, or ASP for short. Use your Visual Basic knowledge to create incredibly robust Web sites, complete with database connections, in a day or two!

Microsoft uses the methods described in this chapter for their own sites, including MSN. In fact, Microsoft claims that they redesigned msn.com in just five days with ASP, compared to months for the original custom C code used to launch the service.

Active Server Pages Step By Step

Code confidentiality is a big issue on the Internet. In a traditional Windows programming environment your code is protected because it's compiled. Not so with client-side scripting. The client browser displays every bit of your code in plain text! Not a good idea if part of the script contains a password, or if it represents months of hard work. How do you create these scripts?

If you know Visual Basic syntax, it's dead-easy to create Active Server pages. In fact, there are just three steps!

Step One: Install the Infrastructure

This chapter assumes that you installed Internet Information Server 3.0 (IIS) and the Active Server Pages (ASP) software. Both these software packages are free, and can be downloaded from http://www.microsoft.com/iis.

Don't have Windows NT? Microsoft offers an alternative with PWS, or Personal WebServer, software. PWS is designed for Windows 95, and it's an alternative for developing ASP sites. But be wary of deploying a site on the PWS platform, because Windows 95 is not truly designed for Web site duty.

The examples in this chapter assume that you have a directory named scripts with a subdirectory named chap29. ASP scripts must be located in a directory that has execute privileges set via the IIS Manager. In general, it's a bad idea to modify security permissions at the NT File System level. You must set permissions via IIS, regardless of any changes in other security settings.

Set your system up to run the scripts in this chapter as follows.

  1. Start Microsoft Internet Service Manager by clicking on the Start button in the taskbar, then click on Programs, Microsoft Internet Server (Common), Internet Server Manager. Right-click on the computer name in the line that says WWW. The screen appears as in Figure 29.1.

  2. Choose Service Properties to display a tabbed dialog box. Once the dialog box appears, choose the Directories tab. The dialog will appear as in Figure 29.2.

  3. Finally, double-click on the line that shows /scripts as an alias (IIS created this directory when it set your software up). The screen illustrated in Figure 29.3 will appear. Check to make certain the execute box is checked, then note the physical path to the scripts directory. The read box should not be checked, to prevent someone from downloading your scripts.

  4. Finally, create a subdirectory in the physical scripts directory named chap29. Copy the examples for this chapter from the CD you received with this book into that subdirectory.

Step Two: Write HTML Code

That's correct--just write plain old HTML code, and save it with a file extension of .asp. When a client requests pages ending in .asp, the Active Server DLL automatically parses the page, looking for special server-side statements.

Your basic code is standard HTML, such as the following example. Since there's nothing special about the code in Listing 29.1, the server will pass the HTML without any action.

Listing 29.1LST29_1.ASP--A Simple Page

<HTML>
<BODY>
<B>Welcome to Tropical Beaches, Inc</B><BR>
We specialize in remote tropical beach vacations<P>

</BODY>
</HTML>

Step Three: Add Server-Side Statements And Objects

We'll examine Objects in a moment. Server Side Scripts are extremely easy to add. Simply use the format <SCRIPT LANGUAGE="VBSCRIPT" RUNAT=SERVER>. In fact the only difference between server and client script tags is the RUNAT keyword!

Microsoft makes your life even easier with the <% and %> tags, which are shorthand for the longer script tag. So the following two examples are exactly the same:

<SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>
Response.Write "The Time is " & Date & "<BR>"
</SCRIPT>

<%
Response.Write "The Time is " & Date & "<BR>"
%>

In fact, an even cleaner way to write this statement is shown below. In this example, we've added an equal sign to indicate that the term inside the brackets is either a variable or function.

The Time is <% =Date %> <BR>

Display Local Time In Client Page

This script is an excellent example of a script that might run either at the client, or at the server. There's a distinct advantage if it runs on the client: time will be displayed in local time. Server-generated time gives the result based on the time zone that the Web site is in, which might confuse your visitor!

<SCRIPT LANGUAGE="VBSCRIPT">
Dim stOutput
stOutput = "Your browser last refreshed this page on " _
& Date & " at " & Time & "<P>
Document.Write stOutput
</SCRIPT>

Server-Side Clock

We can't always use client-side scripting.

If you operate a business site that the public visits, there is no way to predict what browser will be in use by any given user. Server-side scripting is one way to avoid most of the "reach" issues, where you're trying to serve as many client browsers as possible.

Reaching Script-Illiterate Browsers

To display the date your page was last reloaded by the client browser, the same code can be run at the server using Microsoft's ASP. There are three changes from the previous example:

1. We added RUNAT=SERVER in the opening SCRIPT tag.

2. The output string says what time zone (Seattle) the time is displayed in.

3. Response.write is used in place of document.write. That's because we're writing to the HTML code that the server is producing, not the document that the client is reading.

<SCRIPT LANGUAGE="VBSCRIPT" RUNAT=SERVER>
Dim stOutput
stOutput = "Your browser last refreshed this page on " _
& Date & " at " & Time & " Seattle time.<P>
Response.Write stOutput
</SCRIPT>

Server-Side Scripting Protects Your Code

The best way to protect code is never give it away! Use server-side scripting to handle all your "if" statements, call password-protected databases, and perform other confidential tasks. Then pass just the results back to the client.

Combine Server And Client Scripting For Power

Microsoft's Active Server Page software provides a tool to make on-the-fly decisions about how to serve up pages to the client browser. One of the most powerful features of ASP is the Browser Capabilities component. Your code can choose between client and server-side scripting, based on information the browser capabilities component provides.

How The Browser Capabilities Component Determines Capabilities

The Browser Capabilities component uses the User Agent HTTP header (always sent by the browser when it connects to the server) to determine what capabilites the browser has.

The header is nothing more than a small ASCII string that contains the browser name and version. Microsoft provides a file named browscap.ini, typically located in c:\WinNt\System32\inetsrv\Asp\Cmpnts, that expands this information into a rich set of information. These capabilities are shown in Table 29.1, and are also in Microsoft's online documentation that ships with ASP.
Table 29.1Browser Capabilities Component Properties
Property Name
Browser Type bc.Browser
Version bc.Version
Major Version bc.Majorver
Minor Version bc.Minorver
Frames Capable? bc.Frames
Tables Capable? bc.Tables
Cookies Capable? bc.Cookies
Able To Play Background Sounds? bc.BackgroundSounds
VB Script Capable? bc.VBScript
JavaScript Capable? bc.JavaScript

Use Browser Capabilities Information In Your Code

The example below pulls server and client scripting together, and makes decisions between the two based on the browser capabilities properties. This code assumes that you want to display date and time on the client page using VBScript if possible, but via server-side scripting as a fallback.


NOTE: A note about code style: Each line that you create with response.write needs a CR/LF after it for clean display if a client views the source code. CR/LF is never displayed by the browser as part of HTML output, and only affects source code readability on the other end of a session. We've created a variable named crLf, then set it to those values.


Listing 29.2LST29_2.ASP--Using The Browser Capabilities Component

<HTML>
<BODY>
<%
dim crLf
crLf = chr(13) & chr(10)
Set bc = Server.CreateObject("MSWC.BrowserType")
If bc.VbScript = True then
Response.Write "<SCRIPT LANGUAGE=" & chr(34) & "VBScript" & chr(34) & ">" & chr(13) & chr(10)
Response.Write chr(9) & "dim stOutput" & chr(13) & chr(10)
Response.Write chr(9) & "stOutput = " & chr(34) _
& "<FONT SIZE=2 FACE=ARIAL>Your browser last reöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþare!
Response.Write "<TR><TD><TR><TD><P><FONT SIZE=2 FACE=ARIAL>Your browser last refreshed this page on " _
& date & " at " & time & " Pacific (Seattle) Time <P><BR><P></TD></TR>" & chr(13) & chr(10)
End if
%>
</BODY>
</HTML>

Use Session Objects To Store Information

Session objects address one of the stickiest parts of Web site development: How do you track session "state" in a stateless environment? Web browsers are similar to IBM 3270 terminals in the sense that you "press transmit" to query a site, which then returns information and disconnects. There's no persistent connection between you and the server.

Cookies are used to store a session "serial number" on the client, which is then passed to the server each time the browser connects. By looking up the serial number in an internal table, the server identifies which user just connected, and thus "maintains state".


TIP: Don't count on the session object if you plan to accommodate older "brain-dead" browsers, or users who turned cookies off!


The session object is valid until it expires, or until the user abandons the session.

Session Object Methods and Properties

The session object is simple to use, because there is only one method and two properties, as shown in Table 29.2:
Table 29.2Session Methods and Properties
Method/Property Description
Abandon (Method) Destroys Session object and releases its resources
SessionId (Property) Returns user's SessionId
Timeout (Property) Timeout period, in minutes, for session. Default is 20 minutes. Must be set in global.asa.öþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþtored as Session objects. So the following code will fail:

<%
Set Session("Answers") = Response
%>

  • Sessions are collections. If you use an array, modify the array items outside the array and return the entire array as a single item to the collection:

<%
Dim arBeachArray()
arBeachArray() = Session("BeachList")
arBeachArray(0) = "My newest favorite"
Session("BeachList") = arBeachArray
%>

CookBook Example: Session Object

The following code samples set up a Web site for a new travel agency named "Playas del Sur", which sells vacation packages to remote tropical beaches. "Remote" implies solitude, so to control beach population (remote tropical beaches are very popular), the visitor must order within one minute or the server will cancel their session. Visitors are asked for their name and a favorite country.

This example uses one minute as a timeout setting. Microsoft's documentation implies that you can put Session.Timeout = 1 in your code, but experimentation proved that approach unreliable. The safest approach is using global.asa to make this a site-wide setting. Put global.asa in your root directory (where the default page for your site resides), then modify Session_OnStart to read as shown below. Restart IIS to make the change take effect.

<SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>
SUB Session_OnStart
Session.Timeout = 1
END SUB
</SCRIPT>

If you copied your scripts to /scripts/chap29, the following three scripts are available to try. Select a beach from the list on the first page. Click submit and navigate to the second page. Finally, click on the link to navigate to the third page. Both the beach and a session ID are displayed.

Try this scenario one more time. This time, wait several minutes between pages two and three. Is the result different?

Remove the Session.Timeout setting and restart WWW services after your experiment! One minute timeouts will create hours of debug effort after you forget that it's wired down to such a small value!

Listing 29.3Timeout1.ASP--Page 1 of 3 Session Timeout

<HTML>
<BODY>
<%
Session.Timeout = 1 ' Experimentation proved this unreliable
%> ' See chapter text

<P><CENTER><B>Welcome To Playas Del Sur</B></CENTER></P>

<P>Please select your favorite beach</P>
<FORM ACTION="/Scripts/Chap29/Timeout2.asp">
<SELECT NAME="FavoriteBeach">
<option selected>Playa Manuel Antonio</option>
<option>Cahuita</option>
<option>Playa del Cocos</option>
<option>Playa Hermosa</option>
<option>Playa Savegre</option>
</SELECT>

<INPUT TYPE="SUBMIT" NAME="Submit">

</FORM>
</BODY>
</HTML>

Listing 29.4Timeout2.ASP--Page 2 of 3--Session Timeout

<HTML>
<BODY>
<% Session("FavoriteBeach") = Request("FavoriteBeach") %>

<P>Your Favorite Beach is <% =Request("FavoriteBeach") %> </P>

<A HREF="/scripts/Chap29/timeout3.asp">Click Here To Order A Vacation Package</A>

</BODY>
</HTML>

Listing 29.5Timeout3.ASP--Page 3 of 3--Session Timeout

<HTML>
<BODY>
<%
If <% =Session("FavoriteBeach") %> > "" Then
Response.Write "Your order for a vacation at " _
& Session("FavoriteBeach") & "is confirmed<P>"
Else
Response.Write "Sorry, too slow! Contents of variable " _
& Session("FavoriteBeach") & " are empty<P>"
End If
%>

Your SessionId is= <% =Session.SessionId %><BR>
Session.Timeout = <% =Session.Timeout %> minutes.<BR>
</BODY>
</HTML>

Other Server Objects

Microsoft provides a variety of other objects that make scripting easy. One of them is even named Server Object, just to confuse you. You just learned about the Session object; this section covers the Application, Request, Response, and Server objects. Table 29.3 summarizes these objects.

Table 29.3Server-Side Objects
Object Purpose
Application Object Share Information Among Users
Request Object Retrieves values passed by the client
Response Object Sends output to client
Server Object Provides access to server utilities
Session Object Stores information for a specific user session (covered above)

Application Object

The Application Object shares information between all users of a given application. Microsoft defines Application as all the .asp files in a given virtual directory and its subdirectories. This is a simple object, with only two properties, and two methods, shown in Table 29.4. There are no pre-defined properties, although you can create your own. In fact, these user-defined properties are quite useful.

Two events exist--Application_OnStart and Application_OnEnd that are used in global.asa. See Microsoft's online documentation for additional information about them.

Table 29.4Application Object Shares Information Among Users
Method Description
Lock Gives client exclusive access to object
Unlock Reverses lock state


How is the Application object relevant to your Web site? Think of it as a "global variable" that can be used across pages. Playas del Sur uses it to store a daily message, changed by the Webmaster each morning. (Important note: the Playas del Sur Webmaster believes in field research, so the message occasionally gets updated every other day.)

<%
Application("BestBeach") = "Playa Mañuel Antonio"
%>

So promptly at noon (almost) every day, the Webmaster fires up his admin Web tool and runs this script:

Listing 29.6SetApp.ASP--Setting Application Values

<HTML>
<BODY>
<P>Previous Application.BestBeach was <% =Application("BestBeach") %></P>
<P>
Enter Today's Best Beach<BR>

<FORM ACTION="/Scripts/chap29/SetAppAction.asp">
<INPUT NAME="BestBeach" VALUE="" SIZE=25 MAXLENGTH=25><BR>
<INPUT TYPE=SUBMIT VALUE="Set Value" NAME="SetApp" >
</FORM>
</BODY>
</HTML>

Which calls the script SetAppAction.asp, shown below

Listing 29.7SetAppAction.ASP--Setting Application Values

<HTML>
<BODY>
<P>Previous Application.BestBeach value was <% =Application("BestBeach") %></P>
<% Application("BestBeach") = request("BestBeach") %>
<P>New Application.BestBeach value is <% =request("BestBeach") %></P>
</BODY>
</HTML>

Request Object

This is probably the most important object that you'll use on a daily basis. The Response object retrieves values passed by the client form as the client navigates from page to page. It's a robust component that deserves a serious look.

The Request object is different than the other objects we've examined, in that it contains collections (listed in Table 29.5), which in turn have properties. There are not any events associated with it.

The URL Examined You often see parameters passed to Web sites via the URL, in the form http://www.playadelsol.com/scripts/Action.asp?MyBeach=playa+hermosa&country=costa+rica. Take a few minutes to examine the URL closely.

Http://www.playadelsol.com/scripts/Action.asp: represents the URL of the Web site, including the directory and ASP script being called.

? is a delimiter. It marks the end of the url and the beginning of the Query String.

MyBeach= is the first variable name. These names are essentially free form. That is, there was no declaration process required. Usually, but not always, you'll know in advance what variable names are being passed.

Costa+Rica represents the value of the variable MyBeach. All spaces in the value were automatically replaced with plus signs. Query strings are not allowed to contain spaces, because a space is used as the delimiter that ends the query string. You're responsible for dealing with the plus signs at the server.

& is another delimiter. It marks the end of the first variable/value combination, and the beginning of the next.
Table 29.5Request Object
Collection Description
Client Certificate Certification fields. See online docs for additional information
Cookies Value of cookies collection
Form Collection of form elements passed by previous page
QueryString Variables in HTTP query string
ServerVariables Environmental variables. See online docs for additional information


Each of the collections shown in the table contains variables. That's what a collection is: a collection of variables! Variables can be accessed either of the following ways:

Directly as Request(VariableName): Request("BeachName")

As Request.Collection(variableName): Request.QueryString("BeachName")

Variables, in turn, can be an array of elements. These elements are a ones-based collection, and can be accessed via the Count property. For example:

<%
Response.Write "The second beach on our list is " & Request.QueryString("BeachName")(2) & "<P>"
%>

Cookies Cookies comprise the glue that holds ASP together. They're used, among other things, to maintain session state. (See the Session object, discussed earlier in this chapter.) You can build your site and never even think about cookies because all the other objects handle them transparently.

There are two types of cookies: "ordinary cookies", which contain a string of characters, and "cookie dictionaries", that contain an indexed list of cookies. The dictionary version is indicated by the .HasKeys property. This is the only property associated with cookies.

Form Collection
The Forms collection is a set of variables and their values passed by a previous page that used the <FORM ACTION=POST> syntax. It is identical to the QueryString collection in most regards.

There are two Web pages shown below. For this example, we've set up two form elements that have exactly the same name: FavoriteBeach. The second page uses subscripts to reference these collection elements.

Listing 29.8Form.ASP--Form Objects Listing 1

<HTML>
<BODY BGCOLOR="White">
<P><CENTER><B>Welcome To Playas Del Sur</B></CENTER></P>

<P>Please provide the following information, so that we can
best server your needs:</P>

<FORM METHOD=POST ACTION="/scripts/Chap29/FormAction.asp">
<TABLE WIDTH=400 COLWIDTH=200,*>
<TR><TD>First Name: </TD>
<TD><INPUT TYPE="TEXT" NAME="FirstName" VALUE="" SIZE=14 MAXLENGTH=14></TD></TR>
<TR><TD>Last Name: </TD>
<TD><INPUT TYPE="TEXT" NAME="LastName" VALUE="" SIZE=14 MAXLENGTH=14></TD></TR>
<TR><TD>Favorite Beach: </TD>
<TD><SELECT NAME="FavoriteBeach">
<option selected>Playa Mañuel Antonio</option>
<option>Cahuita</option>
<option>Playa del Cocos</option>
<option>Playa Hermosa</option>
<option>Playa Savegre</option>
</SELECT>
</TD></TR>
<TR><TD>Second Choice: </TD>
<TD><SELECT NAME="FavoriteBeach">
<option selected>Playa Mañuel Antonio</option>
<option>Cahuita</option>
<option>Playa del Cocos</option>
<option>Playa Hermosa</option>
<option>Playa Savegre</option>
</SELECT>
</TD></TR>
<TR><TD></TD><TD><INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Continue"></TD></TR>
</TABLE>
</FORM>
</BODY>
</HTML>

Listing 29.9FormAction.ASP--Form Objects Listing 2

<HTML>
<BODY BGCOLOR="White">
<P><CENTER><B>Playas Del Sur</B></CENTER></P>
<%
Dim crLf
crLf = chr(13) & chr(10) ' Make source code more readable on client
If Len(Request.Form("FirstName")) < 1 Then
Response.Write "Please return to the previous screen and tell us who you are!<BR>" & crLf
Else
' Note the direct usage of Response variable name
Response.Write "Thanks " & Request("FirstName") & "<BR>" & crLf
Response.Write "We see that your favorite beach is " & Request.Form("FavoriteBeach")(1) _
& "and that your second choice is " & Request.Form("FavoriteBeach")(2)
End If
%>
</BODY>
</HTML>

Response Object

The Response object sends output to the client browser. You've seen it used as Response.Write in a number of examples in this chapter. This is a feature-rich object, as indicated in Table 29.6 below. We will examine several of the most popular properties.

Table 29.6Response Object
Item Description
Cookies (Collection) A collection of Cookie Values
Buffer (Property) Decides whether page is sent as it's composed, or if all scripts run before it's sent.
Expires (property) How long before page is marked invalid in server cache
ExpiresAbsolute (property) Specific Date/Time that the page expires
Status (property) Contains text that writes to client status bar
AddHeader (Event) Sets HTML header name
AppendToLog (Event) Logs page hit
BinaryWrite (Event) Writes raw data
Clear (Event) Erases output
End (Event) Ends processing of ASP file
Flush (Event) Transmits page immediately
Redirect (Event) Tells browser to connect to another page.
Write (Event) Writes variable to HTTP as a string

Expires Property: A Convenient Trick

So you have one or two pages at your site that you never want to cache? This is just the property for you. Setting its value to zero will force the page to reload every time - a useful technique if data changes constantly, on a page such as a database query that displays Web site activity.

This property must be added to the script before the first HTML statement, or an error will result. The example below illustrates this.

<% Response.Expires = 0 %> ` Forces page to reload from server each time it is requested
<HTML>
<BODY>

Redirect Event Pushes Pages

Here's a convenient trick! Assume that a user entered a password to access your top-secret site. Use this to only let in the Chosen Few. (Never mind that they just submitted their password across the Internet in clear text.)

Response.Redirect must be used ahead of HTML code, so an .asp page with this method is one of the few cases where absolutely no HTML code is used.

` This page contains NO HTML code!
<%
If request("Password") = "MySecretSitePassword" then
Response.Redirect "http://www.theVault.com"
else
Response.Redirect "http://www.theSidewalk.com"
End If
%>

Response.Write

This is the meat and potatoes method that you've seen in many examples in this chapter. It's very simple: write whatever you want to the browser, up to 1022 characters long. Not much to add that you haven't used in the examples presented.

<% Response.Write "Today is " & date &"." %>

Your Own Server Side Object!

Not every situation plugs into the default ASP framework, despite Microsoft's efforts. If you absolutely must do something unique, we'll help you build a custom object here. Don't confuse these objects with Active-X controls, which are designed to run on client browsers. These objects are "back end engines" designed for custom processing of client requests.

The example is not entirely realistic, in that you'd probably accomplish the same task with a database in real life. Since it does illustrate methodology, we'll use it. We're going to create a "beach" object that has properties you can retrieve.

You must have one of the following installed on your computer (in addition to ASP software):

1. Visual Basic 4.0 Professional Level or higher

2. Visual Basic 5.0

Building The Object

We'll use VB4 in this tutorial. Start Visual Basic with a new, empty, project. Set focus to the Project window by clicking on View, Project in the menu bar.

This is not a program with a user interface, so remove Form1 by clicking on File, Remove in the menu bar. The project window is now totally empty.

Save the project. Click on File, Save Project. Type in (or browse) the full path name to your ASP components directory, and add Beach.vpb as project name. The ASP components directory is generally a tree under \WinNt\System32, in the format c:\winnt\system32\inetsrv\asp\cmpnts. Click Save, and all future components will default to this directory.

Name the project by clicking on Tools, Options, and then selecting the Project tab. Change the project name from Project1 to PDS (for Playas Del Sol). Click OK to save the change.

Next create a Beach class that we can call. Using the menubar, click on Insert, Class Module. A new code window will appear that says "Class1".

Click in the Class1 Window, and we'll add our first function, sandcolor, which will return beach sand color when called. (Sand color is important! Black sand beaches in the tropics are rather warm at high noon.) Type the following function into the window:

Pubic Function SandColor(stBeachName as String) as String
Select Case stBeachName
Case "Cahuita"
SandColor = "Black"
Case "Playa Manuel Antonio"
SandColor = "Light White"
Case Else
SandColor = "White"
End Select
End Function

Set the class properties by pressing F4 when the Class1 code window has focus. A properties sheet will appear, as shown in Figure 29.4. Set properties as follows:

Instancing = 2 (Createable MultiUse)
Name = Beach
Public = True

Add a Sub Main to the project. In the menubar, click on Insert, Module. A new window named "Module1" will appear. Create an empty subroutine named Sub Main, as follows:

Sub Main()
End Sub

Save the new components by clicking on File, Save Project in the menubar, and saving all requested components.

Build the project as an OLE DLL. Click on File, Make OLE DLL File. Next, click on the Options button to display yet another dialog box. Check Auto Increment by clicking on it, then click OK to return to the File Save dialog box. Accept the default name of Beach.DLL and click OK.

We're finished with Visual Basic, so you can exit the program.

Register the New Object

There's one last step before we can use this component: it must be entered into the registry. Open a DOS box on the server by clicking on the start button, then on Programs and selecting command prompt. Type regsvr32, followed by the full path to beach.dll to register the object:

regsvr32 c:\winnt\system32\inetsrv\asp\cmpnts\beach.dll

Windows NT will tell you whether or not your attempt at registration was successful. Correct any typing errors and try again, if your registration attempt failed the first time.

Adding Custom Components To Your Scripts

The object you created works just like any other ASP object - at least to the extent that you added functionality. All we need to do is create a local copy of the object, then call it like any other component.

Two more sample scripts are shown below. The first is an input form, and the second a script to act on input provided.

Listing 29.10MyObject1.ASP--My Object Listing 1

<HTML>
<HEAD>
<TITLE>Form.asp</TITLE>
</HEAD>
<BODY BGCOLOR="White">
<P><CENTER><B>Welcome To Playas Del Sur</B></CENTER></P>

<P>Please provide the following information, so that we can
best server your needs:</P>

<FORM METHOD=POST ACTION="/scripts/Chap29/MyObject2.asp">
<TABLE WIDTH=400 COLWIDTH=200,*>
<TR><TD>First Name: </TD>
<TD><INPUT TYPE="TEXT" NAME="FirstName" VALUE="" SIZE=14 MAXLENGTH=14></TD></TR>
<TR><TD>Last Name: </TD>
<TD><INPUT TYPE="TEXT" NAME="LastName" VALUE="" SIZE=14 MAXLENGTH=14></TD></TR>
<TR><TD>Favorite Beach: </TD>
<TD><SELECT NAME="FavoriteBeach">
<option selected>Playa Manuel Antonio</option>
<option>Cahuita</option>
<option>Plaöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþya Savegre</option>
</SELECT>
</TD></TR>
<TR><TD></TD><TD><INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Continue"></TD></TR>
</TABLE>
</FORM>
</BODY>
</HTML>

Listing 29.10MyObject1.ASP--My Object Listing 1

<HTML>
<HEAD>
<TITLE>MyObject2</TITLE>
</HEAD>

<BODY BGCOLOR="White">
<P><CENTER><B>Playas Del Sur</B></CENTER></P>
<%
Dim stBeachColor1
Dim stBeachColor2
Dim crLf
crLf = chr(13) & chr(10) ' Make source code more readable on client

Set beach = Server.CreateObject("PDS.Beach")

If Len(Request.Form("FirstName")) < 1 Then
Response.Write "Please return to the previous screen and tell us who you are!<BR>" & crLf
Else
' Note the direct usage of Response variable name
Response.Write "Thanks " & Request("FirstName") & "<BR>" & crLf

stBeachColor1 = Beach.SandColor( Request.Form("FavoriteBeach")(1))
stBeachColor2 = Beach.SandColor( Request.Form("FavoriteBeach")(2))
Response.Write "We see that your favorite beach is " & Request.Form("FavoriteBeach")(1) _
& ", which has a " & stBeachColor1 & " sand beach, " _
& "and that your second choice is " & Request.Form("FavoriteBeach")(2) _
& ", which has a " & stBeachColor2 & " sand beach."
End If

%>
</BODY>
</HTML>

From Here…

Microsoft created an incredibly robust platform to host Web sites when Active Server Pages were added to Windows/NT 4.0. Like almost every other Internet platform, changes and enhancements are made almost weekly. There are several mailing lists and news groups that will provide more information on these topics:

  • DENALI@LISTSERV.MSN.COM is a very popular ASP list. Send mail to listserv@listserv.msn.com with the words SUBCRIBE DENALI in the message body (leave the subject blank).
  • There's an ASP Developers' newsöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþread" chapter for any serious commercial site.

  • Chapter 19, "Internet Information Server", will show you how to set up IIS for use with Active Server Pages.

  • Chapter 20, "The ActiveX Server Framework", will provide a higher-level overview of other components that integrate tightly with ASP.