共用方式為


Working with Scripting Languages

Programming languages such as Visual Basic, C++, and Java provide low-level access to computer resources and are used to create complex, large-scale programs. Scripting languages, however, are used to create programs of limited capability, called scripts, that execute Web site functions on a Web server or browser. Unlike more complex programming languages, scripting languages are interpreted, instruction statements are sequentially executed by an intermediate program called a command interpreter. While interpretation reduces execution efficiency, scripting languages are easy to learn and provide powerful functionality. Scripts can be embedded in HTML pages to format content or used to implement COM components encapsulating advanced business logic.

Active Server Pages makes it possible for Web developers to write scripts that execute on the server in variety of scripting languages. In fact, several scripting languages can be used within a single .asp file. In addition, because scripts are read and processed on the server-side, the browser that requests the .asp file does not need to support scripting.

You can use any scripting language for which the appropriate scripting engine is installed on your Web server. A scripting engine is a program that processes commands written in a particular language. Active Server Pages comes with two scripting engines: Microsoft Visual Basic Scripting Edition (VBScript) and Microsoft JScript. You can install and use engines for other scripting languages, such as REXX, PERL, and Python.

If you are already a Visual Basic programmer, you can immediately begin using VBScript, which is a subset of Visual Basic. If you are a Java, C, or C++ programmer, you may find that JScript syntax is familiar to you, even though JScript is not directly related to Java or C.

If you are familiar with another scripting language, such as REXX, Perl, or Python you can obtain and install the appropriate scripting engine so that you can use the language you already know. Active Server Pages is a COM scripting host; to use a language you must install a scripting engine that follows the COM Scripting standard and resides as a COM (Component Object Model) object on the Web server.

Setting the Primary Scripting Language

The ASP primary scripting language is the language used to process commands inside the <% and %> delimiters. By default, the primary scripting language is VBScript. You can use any scripting language for which you have a script engine as the primary scripting language. You can set the primary scripting language on a page-by-page basis, or for all pages in an ASP application.

Setting the Language for an Application

To set the primary scripting language for all pages in an application, set the Default ASP Language property on the App Options tab in the Internet Information Services snap-in.

Setting the Language for a Page

To set the primary scripting language for a single page, add the @LANGUAGE directive to the beginning of your .asp file. The syntax for using this directive is:

<%@ LANGUAGE = "ScriptingLanguage" %> 

where ScriptingLanguage is the primary scripting language (such as VBScript, JScript, or PerlScript) that you want to set for that particular page. The setting for a page overrides the global setting for all pages in the application.

Follow the guidelines for using an ASP directive; for more information, see Creating an ASP Page.

Note

To use a language that does not support the Object.Method syntax as the primary scripting language, you must first create the LanguageEngines registry key.

Using VBScript and JScript on theServer

When using VBScript on the server with ASP, two VBScript features are disabled. Because scripts written with Active Server Pages are executed on the server, the VBScript statements that present user-interface elements, InputBox and MsgBox, are not supported. In addition, do not use the VBScript functions CreateObject and GetObject in server-side scripts. Use Server.CreateObject instead so that ASP can track the object instance. Objects created by CreateObject or GetObject cannot access the ASP built-in objects and cannot participate in transactions. The exception to this rule is when you are using the IIS Admin Objects, and when you are using Java monikers. For more information, see Using the IIS ADSI Provider and Creating an Object from a Java Class.

For a list and description of all VBScript and JScript operators, functions, statements, objects, properties, and methods, refer to the VBScript Language Reference and JScript Language Reference. You can find this reference at the Microsoft Script Technologies Web site.

Including Comments

Because the processing of all scripts in ASP is done on the server side, there is no need to include HTML comment tags to hide the scripts from browsers that do not support scripting, as is often done with client-side scripts. All ASP commands are processed before content is sent to the browser. You can use HTML comments to add remarks to an HTML page; comments are returned to the browser and are visible if the user views the source HTML.

VBScript Comments

Apostrophe-style comments are supported in VBScript. Unlike HTML comments, these are removed when the script is processed and are not sent to the browser.

<%  
  'This line and the following two are comments.  
  'The PrintTable function prints all  
  'the elements in an array.  
  PrintTable MyArray()  
%> 

You cannot include a comment in an output expression. For example, the first line that follows will work, but the second line will not, because it begins with <%=.

<% i = i +1 'This statement increments i. (This script will work.) %>  
<%= name 'This statement prints the variable name. (This script will fail.) %> 

JScript Comments

The // comment characters are supported in JScript. These characters should be used on each comment line.

<% 
  var x 
  x = new Date()  
  // This line sends the current date to the browser,  
  // translated to a string. 
  Response.Write(x.toString())  
%> 

Case Sensitivity

VBScript is not case sensitive. For example, you can use either Request or request to refer to the ASP Request object. One consequence of case-insensitivity is that you cannot distinguish variable names by case. For example, you cannot create two separate variables named Color and color.

JScript is case sensitive. When you use JScript keywords in scripts, you must type the keyword exactly as shown in the reference page for that keyword. For example, using date instead of Date will cause an error. The case shown in this help topic for the ASP built-in objects will work in JScript commands.