Introduction
Every time you're surfin' the net, you come across many kinds of files. You see HTML files, JPEG files, GIF, JPG, MP3, Zip files and the list is endless. And there are those strange .asp files. What are they? When you fill some details in a form and then click "Submit", and getting to a page telling you the registration is complete, you may have noticed that the extension of the page you reached is ASP. But it looks the same as HTML! So what does that fancy extension mean? Well, ASP is an abbreviation for Active Server Pages. What does it mean? Simple - it means that those pages are running on the server. The idea is that the client computer who visits your site is sending some information to the server via an ASP page, the server manipulates and processes the data, building an appropriate page according to the ASP file and then returning it to the client. Therefore, ASP allows you to interact with the server, and use databases to store information and then display it. I will not explain the use of ASP with databases. It is a whole subject by itself.
Note: This article is written assuming you have the basic knowledge of HTML.
What do I need?
To get started with ASP you will mainly need two things. First, because ASP pages run on the server, you will need a server which supports ASP. You can find some free ones on the end of this guide. You may use Windows 2000 to start a web server on your desktop computer. Secondly, you will need an editor to write your ASP files. You can use any plain text editor such as Notepad, but if you have Visual Studio, I will suggest you to use Visual Interdev, a full featured web building suite, and a very comfy one.
Why do I need ?
I was waiting for this question since the last two paragraphs. You ask - why I need ASP ? For what purposes do I need it? Well, you can use ASP to:
- Build a database and maintain it with ASP code.
- Setup a security mechanism, allowing only registered users to access your webpage.
- Manipulate forms.
- Build a newsletter.
- Build a search engine for your site.
- Brag off at parties.
Let's get it started!
Well, there are two languages you can use in ASP. They are VBScript and JScript. I will teach you VBScript here. If you are a VB programmer, it would be very easy for you to start working with ASP.
Beginning the script
Just like JavaScript, you need to start the ASP script somehow, to tell the server it is a script and not plain text. How do you do it? Simple. You enclose the whole script with <%
and %>
. Inside those two weird things, you write ASP code. I might add that you can also type <% Language=VBScript
, but because VBScript is the default, you can omit it. We'll start with ASP objects.
Objects, please!
Just like JavaScript, ASP provides us with many objects, which have their own methods and properties. I will discuss two main ones.
The Response object
The Response
object does explicitly what it sounds like. It responds to your commands. With the Response
object you can do many things. I will give you the most frequently used methods.
Write
- Just like Document.Write
, this method outputs the text between the parentheses to the document.
Redirect
- Redirects your user to another page.
Cookies
- Allows you to create and update cookies. I will not get into cookies in this article.
Those are the basic components of the Response
object. How do you use it? Simple. You access a property or a method by using a period after the object name. For example:
Response.Write("Hello")
Response.Redirect("home/default.htm")
The Request object
The Request
object allows you to get data from several sources, like forms and cookies. Here are the common collections:
Form
- Allows you to get information from a form. I will explain it in details later.
QueryString
- Gets information from a query string. A query string is in those strange addresses you see sometimes like http://myserver.com/asp/search.asp?p=Blah&s=1
Cookies
- Retrieve information from a cookie.
Example of usage:
Request.Form("Name")
This will get the name
field of the form.
Forms and queries
So, you have a form and you want to pass the data on the form to the ASP script. Well, take for example the following HTML page, containing a form with three textboxes and one submit button.
<html>
<head><title>ASP Example</title></head>
<body>
<form action="example.asp" method=post>
First name <input size=20 name="fname"><br>
Last name <input size=20 name="lname"><br>
Age <input size=3 name="age">
<input type=submit value="Do it">
</form>
Now we have the form. Notice the action
and the method
properties of the <form>
tag - the action
property points to the ASP page. The method we use here is Post
. You could use Get
as well, but usually you will use Post
(if you had used Get
you would have to use Response.QueryString
). We'll now create the ASP page.
<%
FNAME=Request.Form("fname")
LNAME=Request.Form("lname")
AGE=Request.Form("age")
Response.Write("Hiya " & FNAME & ", what's up mate?")
Response.Write("<BR>So I see that your last name is " & LNAME)
Response.Write(" and that you are " & AGE & " years old.")
%>
A little explanation: The user fills the form. He clicks submit. Then, the form sends the data in the textboxes to the ASP script. In the ASP script, we fetch the data by using Request.Form
. Then, we use the response
object to display some lines. Notice that the two files (HTML and ASP) are both on the server. But only the ASP file is running on the server after getting the form info from the HTML file. Now, what's that query thing? Well, there are two ways of passing parameters to ASP pages. The first one is through forms, as you seen in the last example. The other way is through queries, so the parameters are passed like this "http://blah.com/example.asp?p=1&s=2". The question mark(?) tells the ASP that it's gonna get some parameters, and the "and" sign (&) separates the different parameters. Here we pass two parameters - P
with the value 1 and S
with the value 2. So how do we tell ASP to get those parameters? By using Request.QueryString("Blah")
where Blah
is the parameter name. For example:
<a href="example.asp?print=1">Print 1</a>
<a href="example.asp?print=2">Print 2</a>
You could build an ASP page that gets the print
parameter and prints (by Response.Write
) an appropriate welcome message, for example. Note that if you use the Get
method on a form, you should use the QueryString
collection and not the Form
collection of the Request
object.
If's
Like any programming language, VBScript (and therefore ASP too) provides us with control statements. The if
sentence is very important, and it would be impossible to build ASP pages without it. The structure of the if
sentence:
if condition then
...
...
else
...
...
end if
Note that you don't have to include the else
part. Example for If
use:
if age>18 then
Response.Write("Adult")
else
Response.Write("Teenager")
end if
Loops
Sometimes you just have to repeat a block of commands for a couple of times. Sometimes you know the number of times in advance and sometimes not. When you have to repeat it for a constant number of times , you use the for
loop. The structure is:
For variable=starting value to> ending value
...
...
Next variable
For example:
For I=1 to 10
Response.Write("Hello")
Next I
This piece of code will print out "Hello" ten times.
Sometimes you need to run a block of commands until a certain condition is made. In this case you use the while
loop. Here is how its written:
Do While condition
...
...
Loop
For example:
Do While Age<30
Response.Write("Too Immature")
Loop
Summary
I have given you the basics of ASP. There are many more commands and objects you can use, but this should give you a good start. I would appreciate some critics, as it is my first article. In the Appendix you will find some info about setting up or finding a server.
Appendix
To practice and run ASP, you will need a server with support for ASP. You can use Microsoft PWS (Personal Web Server), but I don't recommend it because it does not support all the features of ASP. If you have Windows 2000, you can use IIS (Internet Information Server) on your home computer. Lastly, you can use a web server which will host your site. There are many free web servers out there. I would suggest Domaindlx.com.
Good Luck!