Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / ASP
Article

ASP at a glance

Rate me:
Please Sign up or sign in to vote.
4.73/5 (11 votes)
30 Mar 20027 min read 99.6K   42   4
ASP for beginners, written by a beginner.

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:

VBScript
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
<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.

VBScript
<%
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:

HTML
<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:

VBScript
if condition then
...
...
else
...
...
end if

Note that you don't have to include the else part. Example for If use:

VBScript
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:

VBScript
For variable=starting value to> ending value
...
...
Next variable

For example:

VBScript
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:

VBScript
Do While condition
...
...
Loop

For example:

VBScript
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!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Israel Israel
Born in the darkside of the center of Israel (Petah Tiqva city), raised by my two parents, one of them is a computer man.
Started with the basic language, quickly moving on to Pascal than to borland C for dos. Playing for fun with VB and hopefully Visual C++ time will come..

Comments and Discussions

 
GeneralPersonal Web Server Pin
Chris Maunder1-Apr-02 18:26
cofounderChris Maunder1-Apr-02 18:26 
GeneralRe: Personal Web Server Pin
Dan Pomerchik1-Apr-02 23:36
Dan Pomerchik1-Apr-02 23:36 
GeneralRe: Personal Web Server Pin
Chris Maunder1-Apr-02 23:58
cofounderChris Maunder1-Apr-02 23:58 
GeneralRe: Personal Web Server Pin
Dan Pomerchik2-Apr-02 0:48
Dan Pomerchik2-Apr-02 0:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.