How to use Windows script components to package ASP scripts

Veralteter Haftungsausschluss für KB-Inhalte

Dieser Artikel wurde für Produkte geschrieben, für die Microsoft keinen Support mehr anbietet. Deshalb wird dieser Artikel im vorliegenden Zustand bereitgestellt und nicht mehr aktualisiert.

We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:

Summary

This article demonstrates how to use Windows script components (WSC) to package scripts for use in Active Server Pages (ASP) applications. This component includes an entry in the registry and a type library. You can call this component just as you call a compiled component. Also, because the component is a text file, you can modify your code repeatedly during development without having to recompile.

Requirements

  • The steps in this article require one of the following configurations:
    • Microsoft Windows 2000 Professional, Windows 2000 Server, or Windows 2000 Advanced Server with Internet Information Server (IIS) 5.0
    • Windows NT 4.0 and IIS 4.0 with Windows Scripting Host 5.5 and Microsoft Internet Explorer 5 or later.
  • A valid, configured connection to Microsoft SQL Server 7.0 or 2000, which uses an OLE DB Provider connection string to the sample Pubs database.

    Note In the "Data Source=" parameter of the first code sample to follow, you must replace "<Your Database>" with the name of your database server.

    If you do not have access to SQL Server, the sample should still work if you substitute a valid connection string to the Microsoft Access Northwind sample database, change the SQL string to connect to the Employees table, and use two different fields (one for employee ID and one for last name). For more information about how to set up a database connection, click the following article number to view the article in the Microsoft Knowledge Base:
    300382 How to create a database connection from an ASP page in IIS

Setup and installation

Create the Web root and application folder

  1. Right-click Start, and then click Explore to open Windows Explorer.
  2. Click to highlight the C drive (or main drive).
  3. From the File menu, point to New, and then click Folder. Name the folder WSC.
  4. Right-click WSC, and then click Properties.
  5. On the Security tab, click Everyone. In the Permissions text box, ensure that the Read and Execute check boxes are selected. Leave any other check boxes as they are. Click OK to close the WSC Properties dialog box.
  6. Click to highlight the root folder of your Web site (which is C:\InetPub\Wwwroot by default).
  7. From the File menu, point to New, and then click Folder. Name the folder WscTest.
  8. From the Windows Start menu, point to Programs, point to Administrative Tools, and then click Internet Services Manager to open the ISM.
  9. Notice that the WscTest folder appears under the Default Web Site. Right-click WscTest, and then click Properties.
  10. Confirm that Anonymous access is enabled as follows:
    1. On the Directory Security tab, under Anonymous Access and Authentication control, click Edit.
    2. Select the Anonymous Access check box, click OK, and then click Apply.
  11. Mark the directory as an Active Server Pages (ASP) application as follows:
    1. On the Directory tab of the WscTest Properties dialog box, click Create under Application Settings.
    2. Click Apply to save your changes, and then click OK to close the WscTest Properties dialog box.
    For more information about how to create subwebs and configure them for ASP applications, click the following article number to view the article in the Microsoft Knowledge Base:
    301392 How to create a virtual folder (subweb) in IIS 4.0 or in IIS 5.0

Download and install the Windows Script Component Wizard

  1. To download the Microsoft Windows Script Component Wizard Setup file (Wz10en.exe), visit the following Microsoft Download Center Web site:
  2. After you have downloaded the file, double-click
    Wz10en.exe to install the wizard.
  3. When you are prompted for an installation location, click OK to install the wizard to the default location.
  4. When you are prompted to create the installation directory, click OK.

Create a Windows script component

  1. From the Windows Start menu, point to Programs, point to Microsoft Windows Script, and then click Windows Script Component Wizard to start the Windows Script Component Wizard.
  2. In Step 1 of the wizard, in the Name text box, type HTMLComponent. Notice that the Filename and Prog ID text boxes are populated automatically. In the Location text box, type the path to the WSC folder that you created earlier (for example, type C:\WSC). Click Next.
  3. In Step 2 of the wizard, click JScript as the language. Select the Do you want special implements support? check box, and then click Support Active Server Pages. Select both the Error checking and Debugging check boxes, and then click Next.
  4. In Step 3 of the wizard, you are prompted to define the properties of your WSC. For this demonstration, skip this step, and click Next.
  5. In Step 4 of the wizard, you are prompted to define the methods of your WSC. In the Name text box, type MakeDropDown. In the corresponding Parameters text box, type strSQL. In the Name column, click below your first entry, and type
    DateString. This method has no parameter. Click Next.
  6. In Step 5 of the wizard, you are prompted to define custom events. For purposes of this sample, skip this step, and click Next.
  7. In Step 6 of the wizard, review the selections that you made, and then click Finish.

Modify the script component

  1. Right-click Start, and then click Explore to open Windows Explorer.
  2. Open the WSC folder that you specified in Step 1 of the wizard.
  3. Right-click HTMLComponent.wsc, and then click Open to open the WSC in Notepad.
  4. Examine the contents of the file and note the following items:
    • The file is a normal Extensible Markup Language (XML) text file.
    • The "registration" element defines the registry details of your component.
    • The "public" element provides public interfaces for the methods that you defined.
    • The code includes your two functions, each of which include nothing more than a "return" line of code.
  5. To dynamically populate a drop-down list box, follow these steps:
    1. Locate the MakeDropDown function.
    2. Delete the following line of code from the MakeDropDown function:
      return "Temporary Value";
    3. Copy the following code, and paste the code between the "{" and "}" brackets in the function.

      Note Remember that you must modify the connection string to work with your database so that the sample code works:
                      //ADO constants
      var adOpenForWardOnly = 0
      var adLockReadOnly = 1

      //Open the recordset.
      var conn = Server.CreateObject("ADODB.Connection");
      conn.Open("Provider=SQLOLEDB.1;User ID=<username>;Password=<strong password>;"
      + "Initial Catalog=pubs;Data Source=<Your_Database>;");
      var rs = Server.CreateObject("ADODB.Recordset");
      rs.Open(strSQL,conn);

      //Create the HTML string for the drop-down list box.
      var strHTML = "<SELECT id='myDropDown' name='myDropDown'>\n";
      while(rs.EOF != true) {
      strHTML += "<OPTION value='" + rs.Fields("au_id").Value + "'>"
      + rs.Fields("au_lname").Value + "</OPTION>\n";
      rs.MoveNext();
      }
      strHTML += "</SELECT>\n";

      //Clean up the objects.
      conn.Close();
      conn = null;
      rs = null;

      //Return the HTML string for the drop-down list box.
      return strHTML;
  6. Modify the DateString function so that it allows for varied date formats and displays them as expected:
    1. Locate the DateString function.
    2. Delete the following line of code from the DateString function:
      return "Temporary Value";
    3. Copy the following code, and paste the code between the "{" and "}" brackets in the function:
      /* 
      This function does not rely on system settings for date
      formats, which assures that it will display as expected
      in the browser.
      */
      var s = "Today's Date is: ";
      var d = new Date();
      s += (d.getMonth() + 1) + "/";
      s += d.getDate() + "/";
      s += d.getFullYear();
      return s;
    4. Save and close the WSC file.

Register the component

  1. Right-click Start, and then click Explore to open Windows Explorer.
  2. Open the WSC folder, right-click HTMLComponent.wsc, and then click Register.
  3. After you register the WSC, right-click HTMLComponent.wsc again, and then click Generate Type Library. This creates the Scriptlet.tlb file in the WSC folder. Scriptlet.tlb enables you to use IntelliSense coding if you are using a development environment such as Microsoft Visual InterDev for your ASP pages.

How to use your component's methods

  1. From the Windows Start menu, click Run, type notepad, and then click OK to open Notepad.
  2. Create a new ASP page that calls HTMLComponent.wsc and uses its MakeDropDown and DateString functions in your page. Copy and paste the following code into the new ASP page:
    <%@ Language=JScript %>

    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    </HEAD>
    <BODY>
    Hello!<BR>
    <%
    var HTMLObj = Server.CreateObject("HTMLComponent.WSC")
    Response.Write(HTMLObj.DateString());
    %>

    <FORM action="" method=POST id=form1 name=form1>
    Select Author:<BR>
    <%
    var SQL = "Select au_id, au_lname FROM Authors";
    Response.Write(HTMLObj.MakeDropDown(SQL));
    %>

    </FORM>

    </BODY>
    </HTML>
    Notice in the preceding code that you use Server.CreateObject and pass it the Prog ID ("HTMLComponent.WSC") of your component to declare an object. Then, all that you have to do is call the two methods, DateString and MakeDropDown, and use Response.Write to write the returned values into the page.
  3. From the File menu, click Save. In the Save In dialog box, browse to the WscTest folder that you created earlier. In the File name text box, type Test.asp, and then click Save.

View your page

After you view the page in the browser, return to the Web server. In Notepad, open and compare the Test.asp and the HTMLComponent.wsc files. Notice how much cleaner the ASP page looks, with only two lines of code to call methods in the HTMLComponent that combined take up over 40 lines. It is much easier to read and maintain your ASP application if you use WSC components to package reusable code.

Troubleshooting

  • Windows script components are not compiled; thus, they will not match the performance of compiled components.
  • Because Windows script components are XML files and real COM components, if you manually modify the component file (except to change the code in your defined methods), you can easily cause your component to fail or generate unexpected errors.
  • WSC components are a newer Microsoft technology. Despite their strengths, they are not widely documented or used yet. Unlike traditional Visual Basic COM components or regular ASP pages, a quantity of books and articles does not exist yet to provide help.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites: For a complete WSC guide, see the "Windows script components" topic under "Tools and scripting | Scripting" in the Platform Software Development Kit (SDK) documentation. To download the Platform SDK, visit the following Microsoft Download Center Web site:
Eigenschaften

Artikelnummer: 299983 – Letzte Überarbeitung: 23.03.2007 – Revision: 1

Feedback