Introduction to Active Server Pages

The server-side technology used on a web page can often be determined by the extension:

.asp - Active Server Pages
.aspx - ASP.NET
.cfm - Cold Fusion
,jsp - Java Server Pages
.php - PHP

Active Server Pages (ASP) is a server-side scripting language that runs only on Windows servers. The default scripting language for ASP is VBScript which is similar to Visual Basic. Most ASP texts and documentation are written with VBScript examples. Although VBScript and JavaScript are similar, there are some differences in syntax so those more familiar with JavaScript may prefer to use JavaScript as their scripting language. To do this, the first line of an ASP program, which is termed the processing directive, should be as follows:

<%@ LANGUAGE="JScript" %>

Remember that JScript is the name for the Microsoft-version of JavaScript. Alternatively, one can write

<%@LANGUAGE="JAVASCRIPT" %>

Displaying ASP Pages

As indicated above, ASP requires a Windows server. To test pages ASP locally (on your own computer) requires Personal Web Server (PWS) (Windows 95, 98, and often in ME), Internet Information Services (IIS) (Windows XP Professional), Internet Information Server 3.0 and higher (Windows NT Server 4.0 and Windows 2000), or Peer Web Services (Windows NT Workstation 4.0). There is no way to test ASP pages locally with Windows XP Home, and Vista/Windows 7 Home Basic or Starter Editions, and . Installation instructions for Personal Web Server

Although IIS comes with Windows XP Professional, it is not installed by default. To install it, choose Start>Set Program Access and Defaults or Start>Control Panel>Add or Remove Programs. Either method brings up the Add or Remove Programs window and then click Add/Remove Windows Components. This requires that you be logged on with an account that has Administrator Privileges. A similar procedure is used in Vista and Windows 7.

With PWS or IIS installed, one can test ASP pages in a particular folder (e.g. c:\Inetpub\wwwroot by entering http://compname in the location field of the browser where compname is the name of the computer you are using. To determine the computer name, in Windows Explorer or My Computer, right click on My Computer and click Properties>Computer Name and look for the entry after Full computer name. To test pages in other directories, one can in IIS establish virtual directories which maps http://compname/vdirname to a folder on the user's computer. One cannot use File>Open to test these pages in the browser.

Because of security issues, some computer classrooms and labs do not make it possible to run IIS locally and thus one must test files by transferring them to the Windows server, either by an FTP client program, FTP using a browser, command line FTP, or other file upload capability.

ASP Code

If one is using JavaScript as the scripting language for ASP, one can use any statement in "core" JavaScript (that which can run both in the client (browser) and the server plus some statements that can only run on the server. "Core" JavaScript includes everything in JavaScript that doesn't refer to the window or document object. The purpose of this section is to describe the server-only objects, methods, and properties.

An asp page (with an extension of .asp) will typically consist of ASP commands and HTML code. The ASP commands are processed on the server and the results are sent to the browser along with the HTML file. Code in the asp file that appears between opening <% and closing %> signs is treated as ASP and processed on the server. The ASP statements are not sent to the browser (and thus are not viewable by selecting View>Source in the browser); only the results are sent to the browser.

The Response Object

Response.Write("str") sends the value str to the browser. It is therefore analogous to document.write in client-side JavaScript. Equivalent to Response.Write is the output directive with the syntax <%= expression %>

Response.Redirect(url) loads the url into the browser. It is the server-side equivalent of window.location.href = url in client-side JavaScript.

The Request Object

The request object is short-lived. When a page requests an ASP page, the request object refers to the page doing the requesting and the Request object only exists until the page request is fulfilled, i.e. when the ASP page, after processing on the server, is sent to the client. The important methods of the Request object are

Request.Form("ename")
Request.Querystring("ename")

The former accesses the value submitted from the previous page associated with the form element named "ename" where the form was submitted using METHOD=POST whereas the latter accesses the value submitted from the previous page with a name of ename in the querystring -- the querystring is the portion of the URL beginning with the ? mark. Values can be put into the querystring by submitting a form using method="get" (the default) or by expliciting adding name-value pairs to a link. Both methods make it easy to pass values from one page to another because one does not have to parse the name-value pairs as in client-side JavaScript but instead can use these methods to extract the value associated with a name.

Session Object

The Request Object makes it easy to pass values from one page to a specified second page. However, if the values are then to be transferred to a third page, this requires the values to again be put up in the querystring or posted from a form. Furthermore, this only works if the user follows a well-defined path through the application. To avoid either of these difficulties, the Contents collection of the Session object provides for variables which last until the user closes the browser or some period of time has expired (often 20 minutes).

One might think one would create this session variable as follows: Session.Contents("vname")=Request.Form("vname") where vname is the name of the form element and the name of the session variable. However, this produces an error message since once is trying to assign the Request.Form object to a session variable. To get around this, one must convert the Request.Form object to a string. This can be done by either of the following statements.

Session.Contents("vname") = String(Request.Form("vname")); or
Session.Contents("vname") = Request.Form("vname") + "";

Application Object

Application variables survive until the server is shut down. To avoid two users trying to simultaneous alter application variables, the Application Object provides a Lock() and Unlock() method. Thus

Application.Lock():
statements;
Application.Unlock();

Thus execution of the statements between the Lock() and Unlock() will only occur one user at a time.

Server Object

The Server.CreateObject(), Server.Execute(), and Server.MapPath() methods are useful when working with databases, to be described later.

Coding Styles

You often use HTML tags and text in conjunction with ASP script delimiters. You can also use them in a server-side JavaScript decision making structure, e.g.

<% if (Request.Form("country")=="Spain") %> Buenos Dias
<% else if (Request.Form("country)=="Germany") %> Guten Tag
<%else>I don't speak your language.

or write everything within a single pair of delimiters, i.e.

<% if (Request.Form("country")=="Spain" Response.Write("Buenos Dias");
else if (Request.Form("country")=="Germany" Response.Write("Guten Tag");
else Response.Write("I don't speak your language"); %>

ASP Error Messages

To see useful error messages from ASP pages, in Internet Explorer, select Tools>Internet Options>Advanced and uncheck the box saying "Show friendly HTTP error messages". To understand a specific error message, go to www.microsoft.com and put the error messages in the search term.

Revised: August 10, 2010. Comments to William Pegram, bill@billpegram.com