ASP: The Beginning

ASP stands for Active Server Pages and is a Microsoft Technology that allows you to create dynamic web pages, interactive content, and seamless database connections. In this series of tutorials we will learn to use it to create a series of websites from the simple to the complex. There is a lot of ground to cover, so let’s get started.

But before we do, I am going to make a few assumptions:

  • You have at least a basic understanding of HTML.
  • You are aware of the existence of XHTML.
  • You have some knowledge of a scripting language such as JavaScript or VBScript.
  • You have a text editor such as the handy-dandy Notepad File.
  • You have access to a host that supports ASP or
  • You have IIS installed on your computer.

If you said yes to all of these, then carry on my wayward son. If not, lay your weary head to rest and don’t you cry no more. Go learn ’em and come back. What are you waiting for? The whole class is waiting. And don’t forget to share that pizza.

Let’s go ahead and dive right in:

Syntax

ASP files consist of HTML tags, and server scripts, which can contain a variety of of expressions, procedures, operators, and statements. The default language for ASP is VBScript. It does support JavaScript (and Microsoft’s Jscript), as well as languages like PERL and PYTHON, but for any language outside of VBScript and Jscript you will need to install the appropriate script engines for them. For the purposes of this tutorial we will be using VBScript.

Our First Program

Traditionally, the old stand-by “Hello World” program is used to introduce newbie programmers to the world of, well…programming. Similar to how guitarists and bassists are forced to learn Smoke on the Water by Deep Purple. Well if you have read my previous articles you know my complete disdain for this practice. Disdain I say!

<html>

<body>

<%

response.write(“My name is James, conqueror of your Puny World! ALL HAIL HIS MIGHTINESS!”)

%>

</body>

</html>

This will print the text below to your browser:

  My name is James, conqueror of your Puny World! ALL HAIL HIS MIGHTINESS!

You could also accomplish the same with this code:


<html>

<body>

<%=“My name is James, conqueror of your Puny World! ALL HAIL HIS MIGHTINESS!”%>

</body>

</html>

{mospagebreak title=Variables}

The easiest way to think of a variable is with your brain. The second easiest way is to think of them as a box that contains a piece of data. You can take the data out and replace it with another piece of information, or you can leave it empty and store something in it later (or never), or if you are obsessive compulsive you can take things out and put them back in over and over again. Or if you are poor like me you can use the box as a chair. Get a tall enough dog to stand behind you and you’ve got yourself a recliner.

Unlike a box, variables also have a lifetime. I know, it’s sad, but we all have to cross over that dark divide. The lifetime (or scope) of a variable depends upon where you declare it. If you declare the variable outside of a procedure (we will discuss these later on), then the variable lives as long as the file does, and can be called by any script in the ASP file. If you declare the variable inside of a procedure however, it is destroyed every time the procedure is executed, and cannot be accessed by any other script in the file.

If you wish to create variables that are available to every page on your website (and not just the ASP file in which you created them), you can either use a session variable or an application variable.

Session variables store information about a single user. They can be used by any page on your website and typically store data such as the user’s name, browser type, and any preferences they may have.

Application variables store data about every user relevant to the application and are available to every page on your site.

{mospagebreak title=Working with Variables}

So now that all of that is out of the way, let’s get our hands dirty with a little code. In this first example I will beat you until you learn the proper way to declare a variable:


<html>

<body>


<%

Dim teacher

teacher=”Mister Kotter”


response.write(“Welcome back “ & teacher)

response.write(“Welcome back, welcome back, welcome back”)

%>

The above code displays the following text in the users browser:

  Welcome back Mister Kotter

  Welcome back, welcome back, welcome back

The above code creates a variable named, “teacher” and stores the value “Mister Kotter” in it. We then use response.write to write the sentence “Welcome back” to the monitor, and use “&” plus the variable name to append the data within the variable to the sentence.

Arrays

Another way to think of a variable is to think of a file folder that has some information in it. You can take that data out, read it, put it back, or put other data into it; the information in it varies. Now when you store those file folders, you place them inside of a filing cabinet. In ASP (and pretty much every programming language), you store multiple pieces of data in arrays. Here is how you do it:


<html>

<body>

<%

Dim ateam(3),i

ateam(0) = “Hannibal”

ateam(1) = “Murdock”

ateam(2) = “Faceman”

ateam(3) = “B.A. Baracus”

For i = 0 to 3

response.write(ateam(i) & “<br />”)

Next

%>

This program loops through all the values in the array and prints them to the monitor (don’t worry if the code looks a little weird to you; we will cover loops soon). The result is:

  Hannibal

  Murdock

  Faceman

  B.A. Baracus

You will note that when we declared our variable we placed a “3” in the brackets. These brackets determine the amount of elements that are in the array (each piece of data in an array is an element). As you can see, there are actually four pieces of data, which does not correspond with the number of elements we initially declared. This is because ASP (and all languages really) start the first element position at 0, and not at 1.

It is possible to create more dynamic arrays, where the number of elements in the array change throughout the course of the program. To do this, you simply leave off the number of elements in the array, like so:


<html>

<body>

<%

Dim ateam()

ReDim ateam(1)

ateam(0) = “Hannibal”

ateam(1) = “Murdock”

ReDim Preserve ateam(3),i

ateam(2) = “Faceman”

ateam(3) = “B.A. Baracus”

For i = 0 to 3

response.write(ateam(i) & “<br />”)

Next

%>

This code creates an array named ateam. It then uses the ReDim keyword to recreate the array to hold two elements and stores some values in it. Next, we use ReDim on the array again, however this time we don’t want our previous data to disappear, so we use the Preserve keyword, to keep the data in the array. We then assign two more variables and finally loop through the array, printing out its values. The result:

  Hannibal

  Murdock

  Faceman

  B.A. Baracus

{mospagebreak title=Naming Conventions}

When you name a variable, you must follow certain rules, or conventions. Here they are in a nifty little list:

  • The name must start with a character from the alphabet (a through z or A through Z).

  • You cannot use a period in the variable name.

  • No two variables can have the same name within the same scope. Really, it is best to avoid naming two variables the same.

A Brief Example of Sessions in ASP

While we do not have time to cover Sessions in-depth in this article, I would like to go over how one looks briefly (we will discuss Sessions and Cookies in the next tutorial). Here is a simple code to read what time the user visited the website and greet them:


<html>

<body>

<%

Session(“TheTime”) = Time()

response.write(“Greetings nerdlings! Welcome to my humble abode. The time is currently: “ & Session(“TheTime”))

%>

This will display:

  Greetings nerdlings! Welcome to my humble abode. The time is currently:   12:10:19 PM.

Well that is all the time we have for this exciting episode. In the next article we will be discussing cookies (yummy) and sessions, and how to work with them. So be sure to drop on by for a spell. Maybe you can even take a swim in our concrete pond.

Till then…

3 thoughts on “ASP: The Beginning

  1. Thanks for stopping by. In this article we discuss the basics of ASP and how to create and work with variables. If you have any questions or comments, new ideas or ways to do what is discussed in the article, want to shoot the breeze with your favorite writer, or have a topic you would like for me to write about, click that comment button and type in some jibberish. Meanwhile, why not entertain yourself by reading more of my awesome writing by following the links below, and check out Tech News You Can’t Use, written by yours truly, on the landing page of our Developer Shed Network sites:

    http://www.devshed.com/cp/bio/
    http://www.devarticles.com/cp/
    https://www.aspfree.com/cp/bio/

    Thanks.

  2. Believe it or not, its a bonafide ASP without the .Net article. Aimed mostly at the beginner programmer, or the programmer that enjoys learning older languages/versions of languages (they exist). I will be doing a series on ASP.Net pretty soon as well. And maybe some on Fortran, Cobol, and hopefully a tutorial on how to use Water Wheels and Abacus’ as well. So stay tuned!

[gp-comments width="770" linklove="off" ]