February 1999

Using ASP Server-Side Components

One of the most useful features of ASP programming is the ability to perform difficult or cumbersome tasks quickly and easily using server components. Server components allow you to perform tasks by manipulating a server object's methods and properties. One familiar example of a server component is MSWC.BrowserType. Using this component, your ASP pages can determine the browser used by the client, as well as some particular capabilities of that browser.

In addition to the built in components, there are a number of third-party components available. In this article, we'll show you just what components are available for you to use and give you a general overview of what they can do for you. In addition, we'll go through the process of downloading, installing, and implementing a third-party server component in order to build a credit card authorization page.

The available components

When you install the Internet Information Server (IIS) 4.0, a number of standard server components are also deployed. Table A lists 11 server components that are available from Microsoft for IIS 4.0. Not all of these components were shipped with all releases of IIS 4.0, so you may need to download one or two if you find you can't create a particular object in your ASP code. For more details, visit www.microsoft.com/windows/downloads/default.asp and search under Internet Information Server. Complete documentation for each standard component is available in the IIS Online Documentation.

Table A: Microsoft standard server components
Component Description
ActiveX Database Objects Allows you to add, query, and manipulate data in a data store.
Ad Rotator Allows you to display a different graphic on an ASP page each time the page is loaded into a Web browser.
Browser Capabilities Allows you to determine the type of browser used to download the ASP page, as well as determine specific browser capabilities.
Collaboration Data Objects for NT Server Allows you to perform basic collaboration tasks, such as send and receive email.
Content Linking Allows you to establish a pathway through your Web site and then use the component to navigate between pages.
Content Rotator Like the Ad Rotator component, this component allows you to display different content on an ASP page each time the page is loaded in a browser.
Page Counter Allows you to count the number of times an ASP page was hit.
Permissions Checker Allows you to determine if a user has permissions to read a file.
Counters Allows you to create and maintain counters, other than a page count, on an ASP page.
MyInfo Allows you to store, retrieve, and update personal information.
Tools Provides tools to check for existence of a file, process an HTML form, and generate a random number.

In addition to the components that ship with IIS 4.0, there are a number of free or shareware components available on the Internet. Table B lists eight handy third-party components and the URLs where you can find and download the software. While not all of the components listed are free, they're all available for at least a trial period. There are many more ASP components available, so it's a good idea to search before trying to develop your own customized server component.

Table B: Third-party server components
Component Description URL
AspExec 2.0 Allows you to execute DOS and Windows applications, and use their return values in your ASP pages. Developed by Steven Genusa. www.serverobjects.com/
comp/Aspexec.exe
AspInet Allows you to get and put files using FTP from your ASP pages. Developed by Steven Genusa. www.serverobjects.com/
comp/AspInet.exe
AspProc 1.0 Allows you to retrieve an array of process ID's and names, and terminate a process by it's ID. Developed by Steven Genusa. www.serverobjects.com/
comp/aspproc.exe
AspPing Allows you to execute the ping command within your ASP pages. Developed by Steven Genusa. www.serverobjects.com/
comp/aspping.exe
File I/O Allows you to perform directory scans, read and write INI files, delete and rename files, and other file manipulation functions. Developed by Tarsus. www.tarsus.com/
asp/io
LastMod Allows you to determine the last modified date/time from within your ASP page. Developed by Steven Genusa. www.serverobjects.com/
comp/lastmod.exe
Strings Allows you to create string collections and perform string manipulation functions, including sort and reverse collection, formatting, credit card validation, and numbers to words. Developed by Tarsus. www.tarsus.com/
asp/ts
WaitFor 1.0 Allows you to pause your ASP script until a specified period of time has elapsed, a file exists, or the component can get exclusive read/write access to a file. Developed by Server Objects Inc. www.serverobjects.com/
comp/waitfor.exe
Putting the components to work

Now that you know what's out there, how do you get these components to work in your ASP pages? It really is quite simple. First, if you're planning to use a third-party component, you need to download, install, and register the component. Then, you simple use the Server.CreateObject() method to create an object from the new component. What you can do with that component will depend on the capabilities of the component, which will be fully documented. We'll go through this process to create a Web page that validates credit card numbers using the Strings component from Tarsus. You'll see that using a pre-built server component can save many lines of code.

Installing the component

The first thing you need to do is download and install the Tarsus Strings component. The component you'll need to install is located at

www.tarsus.com/asp/ts

You'll be asked to fill out a short registration form in order to download the software for a 30-day trial. If you want to use the component after the trial period, you'll have to pay $29, which isn't bad for all the time and coding it could save you.

Once you've retrieved the Zip file, you'll need to extract the two DLL files and place them in the appropriate directories. The DELPHIMM.DLL should be placed in the winnt/system32 directory, while the TARSUSTS.DLL file should be placed in your IIS component directory. If you don't have a component directory already, you can create one in winnt/system32/inetserv/asp/cmpnts.

After you've extracted both files, you'll need to register the component by launching a command window, changing to the directory where you placed the TARSUSTS.DLL file, and typing the following line at the command prompt:


REGSERV32.EXE TARSUSTS.DLL
When the DLL has successfully been registered, you'll see the dialog box shown in Figure A. Once it's been installed, you're ready to use the component in your ASP code.

Figure A: Registering a component places an entry in the system's Registry identifying the component with a unique ID.
[ Figure A ] The credit card authorization page

Figure B shows the first screen of the credit card authorization ASP page.

Figure B: Using INPUT Type=Password hides our credit card number from prying eyes.
[ Figure B ]

By indicating the correct card type, typing in a valid credit card number, and clicking Submit, you'll reload the page and a message will be displayed informing you if indeed the number you entered was valid. Figure C shows the page after a valid credit card was entered.

Figure C: The short message above the form lets you know that the credit card number was indeed valid.
[ Figure C ]

The code for this ASP page can be found in Listing C. Next, we'll take a closer look at the logic and exactly how we created and used the Tarsus Strings component.

Note: The credit card validation performed on the number entered only checks for correct length, starting sequence, and checksum. This validation doesn't provide authorizations or check credit card balances, or even if an account exits. It only determines if the card is in the correct format.

Listing A: The credit card authorization page


<HTML>
<HEAD>
<TITLE>Credit Card Validation</TITLE>
</HEAD>
<BODY>
<b>Validate your credit card using an ASP server component.</b>

<%
IF NOT IsEmpty(Request.Form("CardNumber")) 
THEN
	SET tsString = Server.CreateObject(
	"TarsusTS.StringUtils" )
Result = tsString.CheckCard(
Request.Form("CardNumber"),
CInt(Request.Form("CardType")) )
	SET tsString = NOTHING
IF Result > "" THEN
Response.Write("The card number you 
entered is not valid<P>")
	ELSE
Response.Write("The card number you =>entered is valid<P>")
	END IF

END IF
%>	

<FORM action=default.asp method=post>
<P>
Card Type: 
<SELECT name=CardType>
	<OPTION selected value=1>American Express
	<OPTION value=2>Visa
	<OPTION value=3>MasterCard
	<OPTION value=4>Discover
</SELECT>	
<BR>
Card Number: <INPUT Type=Password Name=CardNumber Size=16> 
<P>
<INPUT type=submit value="Submit"> 
</FORM>
</BODY>
</HTML>

The Web page is broken into three main parts. The first part, and the simplest, is the head of the page and we don't really need to discuss that any further. The third part is the HTML form, and there's nothing special involved with it. There are two variables, CardNumber and CardType, that the user sets and then submits for processing. The middle part, where all of the ASP code lives, is the interesting part. Let's take a closer look at that now.

ASP code

The first line of ASP code tests to see if the form has been submitted, or if the user is visiting the page for the first time. If there's some value in the CardNumber variable, the page will then create the TarsusTS.StringUtils object. Next, the CheckCard method is invoked. This method takes two parameters. The first is the card number, passed directly from the form. The second is an integer from 1 to 4. The integer 1 represents American Express, 2 represents Visa, 3 represents MasterCard, and 4 is for Discover. We use the Cint() function to convert the CardType variable to an integer. We place the value returned by this function into the variable Result.

Next, we test Result to see if any information has been returned. If something is there, the card isn't valid. If it's empty, the card is in the correct format. Our simple authorization page does nothing more than print out a sentence declaring if the card is good or bad, but a real world application could take this one step further and actually process orders based on this information, knowing, at least, that it had a correctly formatted credit card.

Conclusion

Much of the promise of the Active Server platform lies in the ease in which you can re-use software. One example of this is the server component technology. Using server components can save you development time and energy. Even if you have to develop your own, you'll be able to use them easily and quickly in many Web applications.

In coming issues, we'll take a closer look at the other standard server components and we'll even show you how to create your own server components.

Copyright © 1999, ZD Inc. All rights reserved. ZD Journals and the ZD Journals logo are trademarks of ZD Inc. Reproduction in whole or in part in any form or medium without express written permission of ZD Inc. is prohibited. All other product names and logos are trademarks or registered trademarks of their respective owners.