Active Server Page For Dummies

By Bill Hatfield

Chapter 1: So What's an Active Server Page?

In This Chapter

  • Beyond static HTML
  • Advantages ofActive Server Pages
  • How ASP works
  • What an ASP page looks like
  • Differences betweenASP pages and other Internet technologies

What’s the big deal about ASP? It doesn’t really let you do anything you couldn’t already do before, right? So why bother taking on yet another new thing. There are way too many new things to figure out already!

To answer these questions, in this chapter I show you what your non-ASP options are for creating interactive Web pages. Then I show you exactly what ASP is all about and how it works.

Boring, Dumb, Static HTML

Think about how a simple Web page works.

  1. You click on your favorite site’s name under the Favorites menu in Internet Explorer @md perhaps it’s called “Lots Of Fun.” (No, this isn’t a real one. I just made it up.) The URL associated with “Lots Of Fun” is
    http://www.lotsoffun.com/default.htm
  2. The browser looks for lotsoffun.com and finds the actual server machine on the Internet.
  3. After it finds that machine, it requests that the machine send over the page named default.htm.
  4. The server finds that page and sends it to the browser.
  5. When the browser gets the page it looks at what’s inside. It reads all the HTML tags and then converts them into a beautiful (or ugly, as the case may be), formatted page.

This is how the World Wide Web was originally conceived. A pretty simple idea. But it provided for a very easy way to access information and gave the page designer quite a bit of flexibility in laying out a page.

Forms and CGI

But the communication for static HTML works one way. There is no way to send information back to a Web server. To fix this problem, forms and CGI were created. Forms are HTML tags that allow Web page creators to include controls like edits, checkboxes, and radio buttons in their Web pages. That way, the user can enter information. It also provides a Submit button that sends the information off to the server.

But now the server has to be smarter, too. It can’t just get requests for pages and send out pages. The server has to know what to do with this form information when it gets it. That’s where CGI comes in.

CGI stands for Common Gateway Interface. CGI makes it possible for the Web server to talk to another application that can handle the form information when it’s sent back. Often these CGI applications are written in a language called Perl. When the CGI application receives the form information, it can save it to a text file, store it in a database, or do whatever else the Web site administrator wants it to do.

This system works great for simple Guest Books and the like. But if you want to make your Web pages really interactive, you may start running into trouble.
The problem with CGI is that if five people are submitting form information at the same time, five different copies of the CGI application have to be running on the server to handle them. If one hundred people are submitting form information at once @md you guessed it! @md one hundred copies of the application run at the same time. This is a great way to make a popular Web server fall to its knees and crawl.

Server APIs

Because of the problems with CGI forms slowing everything down, server APIs were born. API stands for Application Programming Interface. Microsoft’s server API is called ISAPI. It stands for the Internet Server Application Programming Interface (clever, no?).

Like CGI, ISAPI allows the Web server to communicate with other applications running on the server machine. But ISAPI is much more efficient than CGI. It doesn’t launch a separate program each time someone sends back information from a form. And it allows the application developer a lot more flexibility in how the server responds to the browser.

But ISAPI doesn’t solve all the problems. You still have to write separate computer programs that have complex interfaces to your Web server and that work closely with your Web pages. ISAPI isn’t very intuitive and it’s difficult to create and maintain. Because of these problems, few businesses go to the trouble of creating truly engaging, interactive Web sites. Creating great Web sites with ISAPI just takes too much time, and most companies can’t afford to dedicate a group of expensive programmers to the task of making their Web sites really, really cool.

Lofty ASPirations

Active Server Pages (ASP) solves all of the problems associated with CGI and server APIs. In addition to being just as efficient as ISAPI applications, ASP is simple to learn and easy to use.

With CGI or ISAPI, you had to write a computer program in a language like Perl or C that had complex interface code connecting it to the server. Then you had to compile the application and associate it with the appropriate Web pages.

With ASP, you simply write your code in the HTML page itself. The HTML tags and the code are side by side. You write the code in a simple scripting language that is easy to learn and easy to use. Then you save the page to your Web site and it’s ready to go. No compiling and no complex interfacing!
As you can imagine, ASP makes it much quicker and easier to create highly interactive Web sites. It also makes your pages easier to maintain and update in the future.

ASP as Easy as 1, 2, 3

So what happens when an ASP page is requested by the browser? It works like this:

  1. You choose a Favorite or click on a link in your browser to go to an ASP page (you can tell you’re going to an ASP page because ASPs have an .asp extension, instead of an .htm or .html extension).
  2. The Web server locates the page and looks at it.
  3. Since it is an ASP page, the Web server executes the page. In other words, it goes through the page looking for any code you have written and runs that code.
  4. After the code runs, all the ASP code is stripped out of the page. A pure HTML page is all that is left.
  5. The HTML page is sent to the browser.

This arrangement has lots of advantages:

  • It is easy to write and maintain because the code and the page are together.
  • The code is executed on the server so you have a lot of power and flexibility.
  • The code is stripped out before it is sent to the browser, so your proprietary applications can’t be easily stolen.
  • Since only pure HTML is sent back, it works with any browser running on any computer.
Say it again, with redundancy

You may have noticed that I occasionally refer to an “ASP page.” You’re probably saying to yourself, “Now wait a minute. Isn’t that an Active Server Pages page?” My response: “Yup. It shore is.”

You see, when I was on my pilgrimage to the English Gurus I asked them about the redundancy of “ASP page.” This is what they said: Since ASP refers to a technology, the phrase “Active Server Page page” is, in fact, correct, and not redundant at all. The first “Page” is part of the phrase which describes the technology. The second “page” is the actual noun you’re talking about. The Gurus never lie.

What Does an ASP Page Look Like?

If you’re like me, you can only hear someone describe something for so long before you want to see it yourself. The code below shows you what a real, live ASP page looks like.

<html>
<head>
<title>My Home Page</title>
</head>
<body>
<h1>My Home Page</h1><p>
<% If Time >= #12:00:00 AM# And _
Time < #12:00:00 PM# Then %>
<h2>Good Morning! </h2><p>
<% ElseIf Time >= #12:00:00 PM# And _
Time < #6:00:00 PM# Then %>
<h2>Good Afternoon! </h2><p>
<% Else %>
<h2>Good Evening! </h2><p>
<% End If %>
<h2>I’m happy you could stop by…</h2><p>
</body>
</html>

Most of this should look familiar. I’ve identified a title for the page and used a first level head to display My Home Page at the top. But then comes the weird part. What are <% and %>? In ASP these are called delimiters. They set off the code from the rest of the HTML tags. That way you’ll always know which is which. If it is inside the delimiters, you know its code. Otherwise, it’s got to be HTML.

Technical Information: A note for client-side scripters

If you’ve written client-side scripts in VBScript or JavaScript, you’re used to seeing code in a Web page that looks like this:

<HTML>
<SCRIPT LANGUAGE=“VBScript”>
<!@hy@hy
Dim Net, Gross, Tax ‘ Create three variables
Gross = 30000 ‘ Gross income
Tax = 4000 ‘ Taxes owed
Net = Gross - Tax ‘ What’s left...?
@hy@hy>
</SCRIPT>
</HTML>

The SCRIPT tag isn’t generally used when you create server-side scripts. The <% and %> delimiters are much more convenient.

Also note that there is no reason to use the HTML comments (<!@hy@hy and @hy@hy>) around the scripting code. ASP code is processed on the server and is stripped out before it is sent to the browser.

It’s not too hard to guess how this page works, even if you haven’t done much computer programming. This code is written in VBScript and is very English-like. Time is a VBScript function that returns the current time (on the server’s system clock).

So first I check to see if the time is between 12:00 a.m. and 12:00 p.m.. If it is, the HTML after the Then is sent back to the browser:

<h2>Good Morning! </h2><p>

The next line is a continuation of the If..Then statement. It begins ElseIf. In other words, “If that didn’t work, try this”. It checks to see if the time is after 12:00 p.m. and before 6:00 p.m.. If it is, this HTML is sent back to the browser:

<h2>Good Afternoon! </h2><p>

Finally, Else is a catch-all. If none of the other conditions worked, send this HTML back to the browser:

<h2>Good Evening! </h2><p>

The End If let’s you know that the If..Then statement is over. The next line of HTML is displayed, no matter how the If..Then condition worked out:

<h2>I’m happy you could stop by…</h2><p>

Notice that there are five lines of HTML in the page. The first and last are always displayed. But only one of the middle three is displayed, depending on the time. Suppose the time is 7:00 p.m. and you go to this page. Your screen would look like Figure 1-1.

***asp0101.bmp Crop to the IE Window***

Figure 1-1: The page at 7:00 PM.

If you chose View --> Source from the Internet Explorer menus you’d see the HTML below.

<html>
<head>
<title>My Home Page</title>
</head>
<body>
<h1>My Home Page</h1><p>

<h2>Good Evening! </h2><p>

</body>
</html>

As you can see, all the VBScript code has been stripped out. And only the appropriate HTML line in the If..Then statement was sent. From looking at this, you’d have no idea that this was anything more than a simple HTML page that always says Good Evening!.

For more information on If..Then and all the other VBScript commands, see Chapters 3 and 4.

How Is ASP Different from Client-Side Scripting?

You’ve probably seen all the books on the market teaching JavaScript and VBScript. How are these different from ASP?

Before ASP, you could use JavaScript and VBScript in your Web pages to do client-side scripting. Client-side refers to the browser and the machine running the browser, as opposed to server-side where the Web server is running. ASP is a way of doing server-side scripting.

Client-side scripting works a little differently:

  1. You choose a Favorite or click on a link in your browser to go to an HTML page that includes client-side scripting code.
  2. The Web server locates the page and sends it back to the browser.
  3. The browser interprets the HTML tags and, at the same time, executes any client-side scripting code that it comes upon.
  4. Some code isn’t executed immediately @md it waits until the user does something like clicking on a button before it runs.

Client-side scripting and server-side scripting are similar in that they both allow you to write code right alongside your HTML and have that code executed when the page is requested. And often you can do the same thing using either client-side scripts or server-side scripts. There are some important differences, though.

  • Client-side scripts are executed by the browser after the page has been received from the server. Server-side scripts are executed by the Web server before the page is sent.
  • Client-side script code is downloaded as part of the page and can be seen from the browser by simply viewing the source for the page. This means that it is easy to steal others’ scripting code.
  • Client-side script can only run on browsers that support scripting and specifically support the scripting language that you’ve used. For instance, if you used client-side VBScript in your Web page and someone accesses it using Netscape Navigator (which only supports JavaScript), the script will not execute at all. With server-side scripting you don't have to worry about the browser's capabilities because only pure HTML is finally sent to the browser.

The good news is that you don’t have to pick between client-side scripting and server-side scripting. You can use both! Even in the same page. In fact there are some techniques for using them together that are very effective.
For instance, imagine you are selling music CDs from your Web site. The user fills out a form to order threeCDs. He has to give his name, address, phone number, social security number, and so forth. When the user clicks the button to submit the form, you use a client-side script to check to make sure that he’s filled in all the information. You can even check the phone number and social security number to make sure the number of digits is correct. When everything looks good, then you can send it off to the server. After it gets to the server, you can use an ASP page to save the order in your database.

Of course, you could check to make sure everything was filled in and looked good in your ASP page. But that means the user would have to wait for the bad data to be sent to the server, evaluated, and a message returned to the browser. Doing it on the client, before it is sent is much quicker and makes a lot more sense.

How Is ASP Different from ActiveX Controls and Java Applets?

You don’t have to read much about the Internet before you see the words Java and ActiveX kicked around. They are two of the hottest topics in the Web development world. How is ASP different?

The concept of Java applets was created by Sun Microsystems. ActiveX controls were created by Microsoft. Both are small programs that are downloaded as part of a page, just as a graphic would be. But when they are received by a client machine, they are executed in the browser and the result is displayed on the Web page. Simple ActiveX controls or Java applets may provide a control that the user can use to enter information, like a listbox. More complex controls or applets might allow you to explore 3-dimensional VRML worlds, or view an animation, or see live video as it is broadcast from a Web site.

Client-side scripts are often used to coordinate several controls or applets on the page so that they work together. Using these technologies together, you can create something that works like a multimedia application or a game.

Java applets are written in Java and ActiveX controls can be written using Visual Basic, C/C++, Java, or other languages. They allow the site developer to create complex programs that couldn’t be created using scripting alone.

You may wonder, if there are controls and applets that supplement scripts on the client-side, are there also controls or applets on the server-side that supplement ASP? The answer is yes. Server components are the topic of Chapter 9.

The World of Internet Development

The Internet is still a very young platform for application development. The growth of interest in the Internet both for personal and professional use has been staggering. This interest is bound to make the Internet a central focus almost all major new development in the future.

But today we're still in the early stages and the standards and development environments and tools aren't nearly as well-defined as they are for stand-alone application software like Word Processors and Spreadsheets. This is both good news and bad news. The pioneers always have the biggest opportunity to leave their mark on the future. But they also often have to work with very crude tools.

ASP, Java and ActiveX are the forerunners of the tools of the future, and they are the tools that will begin to turn the promise of a truly exciting, interactive Internet into a reality.

Back To Table of Contents