How to use Windows script components to package ASP scripts (299983)
The information in this article applies to:
- Microsoft Active Server Pages
This article was previously published under Q299983 NoticeWe 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 installationCreate the Web root and application folder- Right-click Start, and then click Explore to open Windows Explorer.
- Click to highlight the C drive (or main drive).
- From the File menu, point to New, and then click Folder. Name the folder WSC.
- Right-click WSC, and then click Properties.
- 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.
- Click to highlight the root folder of your Web site (which
is C:\InetPub\Wwwroot by default).
- From the File menu, point to New, and then click Folder. Name the folder WscTest.
- From the Windows Start menu, point to Programs, point to Administrative Tools, and then click Internet Services Manager to open the ISM.
- Notice that the WscTest folder appears under the Default
Web Site. Right-click WscTest, and then click Properties.
- Confirm that Anonymous access is enabled as follows:
- On the Directory Security tab, under Anonymous Access and Authentication control, click Edit.
- Select the Anonymous Access check box, click OK, and then click Apply.
- Mark the directory as an Active Server Pages (ASP)
application as follows:
- On the Directory tab of the WscTest Properties dialog box, click Create under Application Settings.
- 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- To download
the Microsoft Windows Script Component Wizard
Setup file (Wz10en.exe), visit the following Microsoft Download Center Web site:
- After you have downloaded the file, double-click
Wz10en.exe to install the wizard.
- When you are prompted for an installation location, click OK to install the wizard to the default location.
- When you are prompted to create the installation directory,
click OK.
Create a Windows script component- 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.
- 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.
- 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.
- 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.
- 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.
- In Step 5 of the wizard, you are prompted to define custom
events. For purposes of this sample, skip this step, and click Next.
- In Step 6 of the wizard, review the selections that you
made, and then click Finish.
Modify the script component- Right-click Start, and then click Explore to open Windows Explorer.
- Open the WSC folder that you specified in Step 1 of the
wizard.
- Right-click HTMLComponent.wsc, and then click Open to open the WSC in Notepad.
- 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.
- To dynamically populate a drop-down list box, follow these
steps:
- Locate the MakeDropDown function.
- Delete the following line of code from the MakeDropDown function:
return "Temporary Value";
- 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;
- Modify the DateString function so that it allows for varied date formats and displays
them as expected:
- Locate the DateString function.
- Delete the following line of code from the DateString function:
return "Temporary Value";
- 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;
- Save and close the WSC file.
Register the component- Right-click Start, and then click Explore to open Windows Explorer.
- Open the WSC folder, right-click HTMLComponent.wsc, and then click Register.
- 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- From the Windows Start menu, click Run, type notepad, and then click OK to open Notepad.
- 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. - 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:
Modification Type: | Major | Last Reviewed: | 2/8/2006 |
---|
Keywords: | kbConfig kbHOWTOmaster kbSample kbScript kbWebServer KB299983 kbAudDeveloper |
---|
|