Developing Web Applications

Next Topic

ASP Server-Side Scripting

You can create highly interactive pages that are independent of the type of browser used to access those pages. Unlike client-side script, with ASP you can hide your scripting on the server, enabling you to protect your development ideas and intellectual property.

The ASP scripting environment is language agnostic, meaning that it isnt limited to a particular language. VBScript, Microsoft JScript, or any language for which a thirdparty ActiveX scripting engine is available (such as PerlScript, REXX, or Python) can be used to create scripts in ASP pages.

ASP scripting instructions appear side-by-side with HTML. (In fact, you can create an ASP page simply by changing the file extension of a file in plain HTML to .asp.) To differentiate between HTML and script meant to run at the server, ASP uses special tags, called server-side scripting delimiters , to indicate server-side script: <%** and %>** . Script appearing inside these delimiters will be invoked on the server as the page is processed. A special form of these scripting delimiters, <%= expression %>, ** can be used as a shorthand for returning values from script.

The following line of server-side VBScript code returns the current date:

Today is <%= Date %>.

This instruction generates something like the following line (the exact text depends on the date):

Today is 7/4/99.

*Note * You can also use the Write method of the ASP Response object to display text or the results of an expression on the page.

A slightly more complex example of ASP might use the conditional execution elements of the scripting language, intermixed with HTML, as follows:

<% If Hour(Now) < 12 Then %>
   <FONT COLOR=YELLOW>Good Morning!</FONT>
<% ElseIf Hour(Now) < 18 Then %>
   <FONT COLOR=LIME>Good Afternoon!</FONT>
<% Else %>
   <FONT COLOR=ORANGE>Good Evening!</FONT>
<% End If %>

Although each page has a primary scripting language, you can use more than one scripting language on a single ASP page. You can set the primary scripting language for an application in Internet Services Manager. Use declaratives (also known as @-directives ) to define the primary scripting language for a page. For more information about declaring scripting languages, see the IIS 5.0 online product documentation.

ASP subroutines and functions can be in any script language, although if you define them inline with the rest of the script, youre limited to the primary scripting language. To change the scripting language of the subroutine, you need to use HTML <SCRIPT> tags to define them. You will also need to add the RUNAT=SERVER attribute, to indicate that this script is intended for the server rather than for the client.

The following sample page demonstrates how to combine a variety of scripting languages and subroutine declaration styles into a single ASP file:

<%@ LANGUAGE="VBScript" %>
<HTML>
<HEAD>

<% Sub InlineSub %>
This text won't be displayed until this subroutine is called.<br>
<% End Sub %>

<SCRIPT LANGUAGE="VBScript" RUNAT=SERVER>
   'Immediate script (outside a function).
   Response.Write "This text is displayed last"
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript" RUNAT=SERVER>
function TestJavaScript(str) {
   Response.Write(str);
}
</SCRIPT>

<SCRIPT LANGUAGE="PerlScript" RUNAT=SERVER>
sub TestPerlScript {
   $Response->Write($_[0]);
}
</SCRIPT>
</HEAD>

<BODY BGCOLOR=#FFFFFF>
<%
   Response.Write "This is VBScript<br>"
   TestJavaScript "This is JavaScript<br>"
   TestPerlScript "This is PerlScript<br>"
   InlineSub
%>
</BODY>
</HTML>

*Note * You can use <SCRIPT> tags to enclose immediate server-side script, but dont expect it to be run until the entire page has been processed. Earlier versions of ASP used <SCRIPT> tags before the introduction of the <% %> delimiters, but their use for immediate script is now discouraged. You should reserve server-side <SCRIPT> tags for defining functions and subroutines only.