Article

Getting Started with ASP

Page: 1 2 3 4

How It Works

Assuming you've got your ASP test page working, you're probably curious to know how it works. Well, as you no doubt guessed, the first five lines of the file are just plain HTML.

<html>
<head>
<title> My First ASP Page </title>
</head>
<body>

One of the most convenient aspects of ASP (and, indeed, most server-side scripting languages in use today) is that it lets you intersperse your script code with plain HTML. The script code gets processed, while the plain HTML gets sent as-is (with a few notable exceptions that you'll see later on).

The next line marks the beginning of the actual ASP code in this page.

<%

Every block of ASP code must begin with a start marker (<%). This is what lets the ASP scripting engine know that it should start converting ASP code into HTML. This marker doesn't necessarily have to appear on a line by itself; as I'll show in later examples, a block of ASP code, markers and all, can in fact appear right in the middle of a single HTML tag on a single line. In this case, I've placed the marker on a separate line to make it stand out more.

The next line actually doesn't do anything:

' Write out a simple HTML paragraph

This line is what programmers call a comment. The apostrophe at the start of the line marks the rest of the line as something for the ASP scripting engine to ignore. To the script, this line is utterly without function, and if you removed it or changed it in any way (except for the apostrophe) you would not affect how the script worked at all. Nevertheless, it is good programming practice to intersperse little explanatory remarks such as this in your code for the benefit of anyone who needs to read and understand your code later on (including yourself!).

A comment need not appear on a line by itself. As soon as it sees an apostrophe, the ASP engine will ignore the remainder of the line on which it appears, so you can just as well place a comment on the end of a line, following the command to which it pertains.

Speaking of commands, the next line is the first and only ASP command in the script:

Response.Write "<p>This is a test of ASP.</p>"

In simple terms, an ASP command, or statement, is a piece of code that tells the ASP scripting engine to do something. By listing a series of statements one after another in the order you want them to be completed, you can write an ASP script that does any number of things in the specified order. In this case, however, I only needed the script to do one thing: write out the HTML code necessary to produce a paragraph of text that reads, "This is a test of ASP."

The code Response.Write is ASP's way of saying, "write the following into the Web page." The HTML code to be written then follows, enclosed in double quotes to mark the start and end of the string of text to be written (any portion of text in ASP is called a text string).

Having finished telling ASP what to do, I ended the ASP code block with the ending marker, (%>). Like the starting marker (<%) above, the ending marker serves to indicate the boundary between normal HTML code and ASP script commands. When it sees an ending marker, the ASP scripting engine goes back to just sending the HTML code as typed.

%>

To finish off the page, I added the familiar closing tags:

</body>
</html>

And there you have it! With this guided tour of a simple ASP script under your belt, you should at least be able to point to each line in your asptest.asp file and explain what it does (or, in the case of the comment line, what it doesn't!). You may be asking a very sensible question, however: "Isn't that just a long-winded way of creating a plain, old HTML page?"

Well you're partly right. Certainly, every person who viewed asptest.asp would just see that simple one-liner. But the magic is in how that one-liner came about. It is re-created dynamically every time someone requests the page. It's not ASP's fault that you supplied it with code that will produce the exact same output every time it is run. Actually it's my fault. Allow me to remedy that situation by altering the code of your asptest.asp file to read as follows:

<html>
<head>
<title> Time Check </title>
</head>
<body>
<%
' Write out a the current server time
Response.Write "<p>The server time is now: "
Response.Write Time
Response.Write "</p>"
%>
</body>
</html>

Save the changes, and then place the updated file on your Web server. When you view the page in your browser now, you should see something like this:

Modified asptest.asp

Notice that when you refresh your browser, the time is updated to match the time according to the computer on which the Web server is running. Notice also that when you use the "View Source" command in your browser, all you see is plain HTML code. ASP writes the time into the page before sending it to the browser. Net result: a dynamic Web page, albeit a very simple one.

The only new trick in this page is the use of the special word, Time, on line 9. Since this word isn't surrounded by double quotes, ASP knows that it isn't supposed to just print out the word "Time." Instead, ASP knows that the word Time should be replaced with a string of text representing the current time (e.g. "5:32:45 PM"), and so that's what the Response.Write command prints out. You'll learn other special ASP words like Time later on in this series.

Notice also that this new script contains three ASP statements, each of which is a Response.Write command. These are executed in the order they appear, from top to bottom, producing the desired HTML code.

Wrap-Up

With this article, you've taken your first steps into the world of ASP. The next article to follow in this series will cover everything you need to know about the VBScript language, which is the primary language used for writing ASP scripts (you can use JavaScript as well). After that, you'll be ready to tackle the subject of building Web applications that access databases and other server-side resources in ASP, all of which will be covered in upcoming instalments of this series.

If you're in a hurry to move forward, however, the ASP documentation accompanying Microsoft IIS and PWS, in combination with the VBScript documentation at the Microsoft Scripting Technologies Web site are fairly well written and should help you on your way. If the documentation is too cryptic for your tastes, you might find a book like "Beginning ASP 3.0" from WROX Press helpful.

Good luck, and I hope to see you back soon for part two of this series!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: