One of ASP’s greatest features is its tight integration with COM. As we examined in Chapter 7, Active Server Pages can instantiate complex COM objects written in high-level languages like Visual C++, Visual Basic, or Java. Microsoft provides several such components for use in Active Server Pages, such as the ActiveX Data Objects (ADO), the Ad Rotator, and the Content Linker.
As the popularity of Active Server Pages has grown, many developers have started their own companies that focus solely on developing COM components for use in Active Server Pages. These components can be used to greatly enhance the functionality provided by your web site. In this chapter, we’ll look at several of these components, focusing on what they do, when they should be used, and how to use them.
ASP was designed to intrinsically support only a small core of functionality, with the premise that if a developer needed further functionality, he could create his own COM component. Some people find this to be a shortcoming of ASP. For example, to open, read, and write to files with ASP, you must use the FileSystemObject; other server-side web scripting languages, like Perl, have file-handling capabilities already built in. While needing to instantiate a component to simply read a text file may seem like a superfluous performance hit, by omitting file-handling capabilities, ASP is more streamlined.
Personally, I find ASP’s implementation to be ideal. ASP is very streamlined, consisting of only a few needed built-in objects. All other functionality should be (and can be) imported from a COM component. Tools to develop COM components are widely accessible (Visual Basic, for instance, is the most popular programming language in the world, with an installed base in the millions), and both ASP and COM have spawned a lively third-party market that focuses on add-on tools for Active Server Pages. The components presented in this chapter help to greatly extend the capabilities of ASP pages.
There may be times when you’d like an ASP page to be able to execute an application on the web server. For example, you may wish to provide the webmaster with the ability to execute certain maintenance programs residing on the web server through a web page. In “Executing Applications on a Server Through an ASP Web Page” (available at http://www.4guysfromrolla.com/webtech/072199-2.shtml ), author Neema Moraveji discusses how he needed to be able to remotely start a setup program on the web server from a web page. In “Creating a Component Using Visual C++ to Manipulate Virtual Directories” (available at http://www.15seconds.com/issue/990107.htm), author Shai Vaingast needed to be able to execute a command-line program through an ASP page.
Rarely will you want to let your users execute an application on the web server. Imagine the performance ramifications of hundreds of users running an application simultaneously on your web server!
In both of these articles, the authors turned to the ASPExec
component, which allows developers to execute DOS and Windows
applications on the web server through an ASP page. ASPExec is a free
component available from Stephen Genusa’s company,
ServerObjects (download it now from http://www.serverobjects.com/products.htm#free).
Once you download the component, you must use
regsvr32
to install it before you can use it in
your ASP pages. To do so, copy ASPExec.DLL
to
the Windows system directory (WindowsSystem
or
Winntsystem32
). Next, run
regsvr32
to register the DLL as follows:
regsvr32 aspexec.dll
Instructions on installing the ASPExec component are included with the download at http://www.serverobjects.com/products.htm#free.
The ASPExec object contains four properties and three methods. The ASPExec properties can be seen in Table 8.1 and the methods in Table 8.2.
Table 8-1. ASPExec Contains Four Properties
Property |
Description |
---|---|
Application |
Specifies the path (optional) and the executable’s filename. |
Parameters |
Specifies the command-line parameters. The property’s value is a single string containing all command-line parameters separated by spaces. |
TimeOut |
Specifies the amount of time to wait in milliseconds if either the ExecuteDOSApp or ExecuteWinAppAndWait method is used. Its default value is 30 milliseconds. |
ShowWindow |
Specifies whether or not a window is displayed on the web server for the executing application (only applicable when either the ExecuteWinAppAndWait or ExecuteWinApp method is used.) |
Table 8-2. ASPExec Contains Three Methods
Method |
Description |
---|---|
ExecuteDOSApp |
Executes a DOS application specified by the Application property, returning the output of the application. |
ExecuteWinAppAndWait |
Executes the Windows application specified by the Application property and waits for the timeout specified by the TimeOut property. |
ExecuteWinApp |
Executes the Windows application specified by the Application property. |
ASPExec can execute both command-line programs (using ExecuteDOSApp) and Windows programs (using ExecuteWinApp or ExecuteWinAppAndWait). To execute a command-line program using ASPExec, you must first create an instance of the ASPExec component. To do so, use the following code:
DimobjASPExecInstance
SetobjASPExecInstance
= Server.CreateObject("ASPExec.Execute
")
Next, set the Application and Parameters properties to indicate the program you’d like to execute and the command-line arguments you wish to pass to the application. Once these two properties have been set, simply call the ExecuteDOSApp method to execute the DOS application.
One such DOS application you can execute through an ASP page is
ping
.
ping
sends an echo request to a server. When the
server receives this request, it returns a confirmation. Oftentimes
ping
is used to ensure that a web site is up and
functional. ping
has many command-line argument
options, but the only required one is the IP, hostname, or domain
name of the server to contact.
Try it out! Drop to the command
prompt (go to Start → Run and enter command
if you are using Windows 95/98, cmd
if you are
using Windows NT or Windows 2000) and type in ping
www.4GuysFromRolla.com.
This will send an echo
request to the web server 4GuysFromRolla.com
runs on. For a full list of ping
’s command-line arguments, enter ping /?
at
the command prompt.
Example 8.1 contains an ASP page that will execute
ping
through an ASP page. The page first creates
a form in which the developer can enter the name of the computer he
wishes to ping. When the developer enters a domain name (or IP or
hostname) to ping, the form reloads the page and the ASPExec
component is instantiated. Its Application property is set to
ping
and its Parameters property is set to the
hostname entered by the user in the form. The code in Example 8.1, which should be saved as
/AspPing.asp
, is nearly identical to one of the
example ASP pages that is included with the ASPExec download.
Example 8-1. You Can Execute ping from an ASP Page Using ASPExec
<% @LANGUAGE="VBSCRIPT" %> <% Option Explicit %> <html> <body> <H3>ASPExec Ping Test</H3> <% if Request.QueryString("host") = "" then %> <!-- The user has no entered a hostname to search yet. Present them with a form to select a hostname. --> <form action="AspPing.asp" method=get> Enter Host to Ping: <input type=text size=45 name=host value="localhost"><P> <input type="Submit"> </form> <% else Dim Executor, strResult 'Create an instance of the ASPExec object Set Executor = Server.CreateObject("ASPExec.Execute") 'Indicate that we want to run the ping program with the hostname as the 'only parameter Executor.Application = "ping" Executor.Parameters = Request.QueryString("host") 'Execute ping, storing the results in strResult strResult = Executor.ExecuteDosApp 'Output the contents of strResult Response.Write "<pre>" & strResult & "</pre>" end if %> </body> </html>
Figure 8.1 shows /AspPing.asp
when first visited. Note that a form is displayed where the user can
enter the hostname of the computer he or she wishes to ping.
Once the user enters a hostname (or an IP or a domain name) and
submits the form, /AspPing.asp
will be called
again. This time, however, an instance of the ASPExec component will
be created and ping
will be executed with the
hostname entered by the user as the only command-line argument. The
output is returned by the ExecuteDOSApp method and stored in
strResult
. This output is then displayed.
Figure 8.2 shows the output of
/AspPing.asp
after the user has selected a
hostname to ping.
The
ASPExec download includes several
examples of using ASPExec. One example uses ASPExec to output the
contents of a DOS command (such as dir C:
). This
idea can easily be extended into a remote DOS window on a web page,
which would serve as a great administrative tool for remote
webmasters. (Of course, such an application is also a scary security
risk. With the proper procedures, however, you could guarantee that
only a select set of users have access to such a powerful
administration tool.)