Developer

Mastering the basics of Active Server Pages

When it comes to data-driven Web pages, Active Server Pages (ASP) offer the most popular approach. Developer Republic kicks off its new ASP column with a look at ASP fundamentals.


Microsoft’s Active Server Pages (ASP) technology is one of the most popular Internet development platforms for creating dynamic, data-driven Web sites. Let’s run through some ASP fundamentals. Then, we’ll write a simple “hello world” program and pick it apart to see how it works.

A little background
ASP was introduced by Microsoft to meet the needs of Web developers using Internet Information Server. ASP functions as an Internet Server Application Programming Interface (ISAPI) filter. ISAPI was designed as an improvement to Common Gateway Interface (CGI). Whether or not Microsoft succeeded is a debate I will leave to others.

The basic principle of an ISAPI application like ASP is to load code that responds to requests from users into the Web server’s memory space. ASP is an ISAPI filter that responds to all files with an .asp extension. Once a file with an .asp extension is detected, that file is processed by the asp.dll file instead of simply serving the file back to the user like a typical .html or .htm file.

The asp.dll file can be found in the System root\system32\inetsrv directory on Windows 2000 and NT. To install ASP on a Windows 2000 development machine, use the Add/Remove Programs option in the Control Panel and choose Add/Remove Windows Components. Check Internet Information Services and follow the prompts to install the IIS Web server and, of course, the ASP ISAPI filter. I recommend using NT or Windows 2000 since they are designed to be more robust operating systems. Additionally, Windows 2000 offers several improvements to ASP and IIS.

You can also run ASP on Windows 98 by installing Personal Web Server. Windows 98 isn’t as feature-rich as Windows 2000, but if you have a laptop running Windows 98, it’s nice to know you can run IIS with ASP.

One of the handy features of ASP is the ability to choose the scripting language you would like to use for programming your pages. With a standard installation of ASP, you can use VBScript or JScript. With additional third-party add-ons, you can use other languages like Perl. (PerlScript is an optional component when installing ActivePerl from ActiveState and is available as a free download at ActiveState’s Web site. This flexibility is one of the reasons I like ASP for Web page development. For additional flexibility, you can run ASP on Linux, Solaris, and other operating systems by using Chili!Soft, which is a cross-platform implementation of ASP.

Jumping in
Getting started with some basic pages is the best way to learn how to use ASP. While Visual Interdev is the recommended development tool for ASP, you can use any text editor to generate pages.

First, create a blank text document in Notepad or your favorite text editor and save it to the wwwroot folder (usually located in C:\inetpub) as Default.asp. Next, enter the following in that document:

 
<%@ LANGUAGE=”VBScript” %>
<%  Option Explicit %>
<html>
<head>
<title>ASP Test Page</title>
</head>
<body>
<%
   dim gOutputMessage
 
   gOutputMessage = “Hello World”
   Response.Write(gOutputMessage)
%>
</body>
</html>

 

The language setting should be placed at the top of every ASP page. For this sample, we are using VBScript, so this is the first line of the document:
<%@ LANGUAGE=”VBSCRIPT” %>

The <%@ and %> are used as delimiters for preprocessing directives. LANGUAGE, ENABLESESSIONSTATE, CODEPAGE, LCID, and TRANSACTION are the available directives. LANGUAGE and ENABLESESSIONSTATE are the most common directives. In a future article, we’ll take a closer look at maintaining session state.

The <% and %> are used as delimiters in ASP to separate HTML from script. These same delimiters are used by Java Server Pages (JSP). Another way to declare code from raw HTML is to use the script tag, as in this example:

 
<SCRIPT LANGUAGE=”VBScript” RUNAT=”SERVER”>
   dim gOutputMessage
 
   gOutputMessage = “Hello World”
Response.Write(gOutputMessage)
</SCRIPT>

 

It really doesn’t matter if you use the script tag or the <% and %> delimiters. It is simply a matter of preference. I like the delimiters because I’ve developed a habit of using them, but the script tag does add a degree of documentation to the programming code.

If you’re a Visual Basic programmer, you probably recognize the Option Explicit command. This forces the VBScript engine to generate an error if you use a variable without first declaring it. This is another statement that should appear on all of your pages. Omitting this statement can lead to some very hard-to-find errors.

The remaining code for this first sample page is mostly HTML. This mixture of HTML and code can lead to some messy documents (spaghetti code) and is one of the arguments against ASP and JSP environments.

The code sample declares a variable named gOutputMessage and initializes that variable to the string “Hello World.” The last line of code performs a write to the output stream using the Response object. Every HTML page involves a request from a user and a response from the Web server. The ASP environment represents the user request as the Request object and the server response as the Response object. The methods available in the Request and Response object are the meat of ASP programming. The various methods in these two objects will allow you to determine what information the user has submitted and respond to that information.

Final thoughts
You can see from our exercise above that it’s not very difficult to get started putting ASP to work, at least with a simple task. As we continue to explore this technology, you’ll be able to harness the power that ASP makes available to you.

What suggestions or questions do you have?
Do you have any advice for dealing with ASP? What ASP topics would you like to see covered? Send us an e-mail with your suggestions and questions.

 

Editor's Picks

Free Newsletters, In your Inbox