Académique Documents
Professionnel Documents
Culture Documents
transform the plain, static web site into a dazzling, dynamic solution. With Microsoft's
server side scripting language you can gather data from your site's visitors, use sessions,
cookies, application variables, and more. Don't worry if you don't know what all of those
terms mean because you will soon be learning all about them.
Introduction
For any webmaster, once you have created a page with graphics and content, the
next logical step is to make it interactive. You can, of course, go to one of the
remotely hosted scripting sites who will provide you with a simple piece of code to
put on your site, but there is a lot more flexibility if you can create and install your
own scripts which will do exactly what you want.
It's thought by many that this 'server-side scripting' (it is processed by the server and
not the browser, so unlike JavaScript the use of ASP doesn't depend on someone's
browser supporting it) is very difficult to learn, and this has come from the early
languages like Perl, which are difficult to write and even more difficult to debug. Over
the past few years two new languages have emerged, PHP and ASP. These are easy
enough for even the novice webmaser to learn.
What Is ASP?
ASP stands for Active Server Pages. It is basically a server-side scripting language
designed for the Windows Platform, although it is available on Unix/Linux systems
through new systems, although PHP is the more popular choice for this platform.
Active Server Pages is based around VBScript, a variant of Visual Basic, which makes
it very easy to use as the majority of the commands are plain English and simple to
decipher.
What Do I Need?
ASP is a server-side language, so you will need to make sure that your web server
has the correct software for running it. The most common setup for running ASP
scripts is on a Windows-based server running IIS (Internet Information Server). It is
possible to use Linux-based systems, though, but they must have the Chillisoft ASP
package installed. Most web hosts will publish whether they support ASP, but if in
doubt contact your systems administrator. If you need a free web host supporting
ASP, try visiting Free-Webhosting.info.
Once you have the server ready to accept scripts, running one is as easy as simply
uploading and running the file. You don't need to put it in any particular place on the
server or change any settings. Just upload and run.
Test Your Web
After you have installed IIS or PWS follow these steps:
Note: Look for the IIS (or PWS) symbol in your start menu or task bar. The program has functions for
starting and stopping the web server, disable and enable ASP, and much more.
After you have installed IIS, make sure you install all patches for bugs and security problems. (Run
Windows Update).
ASP Code
When writing ASP you don't need to worry about changing all your HTML, you simply
add ASP code into your HTML pages where needed. YOu also don't need any special
software on your computer, a simple text editor will do. To begin an ASP page you
will first need to tell it what language you have written it in. The most common (and
the one used in this tutorial) is VBScript. You should begin your page with:
All this code does is tell the ASP system that you are writing your page in VBScript.
You will notice that the ASP code is enclosed in special tags. All ASP code should be
enclosed in the 'percent sign tags' in the form:
Code can be written over multiple lines, but any code not enclosed in the ASP tags
will simply be treated as HTML. Similarly and HTML inside these tags but not
specifically sent as output by the code will cause an error.
Testing ASP
Before you start writing scripts it is a good idea to test whether ASP will run correctly
on your server. Make a simple page with the following:
<html>
n<head><title>Test Page</title></head>
<body>
This is some HTML. Below this I have ASP<br>
<%@ Language=VBScript %><br>
Nothing should appear above here.
</body>
</html>
and save it as test.asp. Then upload this to your server and access it with your
browser. If it has worked correctly, the page should display and you should only see
the lines:
If the ASP appears in the page or the source of the page, something has gone wrong.
Check the code and also the settings on your server. No ASP should appear as it
should have been processed by the server before it was sent to the browser.
Response.Write()
so to write 'Hello World' to the user's browser the complete code would be:
Again, this code begins by telling the system that you are writing in VBScript. Then
comes the Response.Write command. Basically this is made up of two parts.
'Response' tells the server that you want to send information to the user. There are
other types of command including: Request (which gets information from the user),
Session (for user session details), Server (for controlling the server) and Application
(for commands relating to the application). More about these later.
The second part, 'Write', tells the server that the type of response you would like to
send is to write information to the user's browser. This doesn't just have to be text,
but can include variables, which will be discussed in more depth later in this tutorial.
Variables
Probably the most important feature of a programming language is a variable. A
variable is basically a way of storing text, numbers or other data, so that it can be
referenced later. For example, to change the earlier 'Hello World' script:
<%@ Language=VBScript %>
<%
OutputText = "Hello World"
Response.Write(OutputText)
%>
The output of this code will be exactly the same as the first script, but it is
fundementally different as it uses variables. Basically what this code does follows:
This line sets up a variable called OutputText and stores in it the string of letters
'Hello World'. As this is now stored in a variable, you can now reference this text you
have stored in any part of your script, and you can also manipulate it. The next line:
Response.Write(OutputText)
tells the server that you are sending information to the browser, and that the
information to be sent is the contents of the variable called OutputText. Please note
that the variable name is not enclosed in quotation marks. If you did this the browser
would simply output the title of the variable as text.
There is a second way of outputting the values of variables, other than using
Response.Write. The earlier code could have been written:
Variable Operations
The main benefits to storing information in variables is that you can use the text over
and over again. For example, once storing "Hello World" in the variable OutputText, I
can then use it in various places in my code:
This is my <% =OutputText %> script. The whole reason for it is to output the text <
% =OutputText %> to the browser.
This is my Hello World script. The whole reason for it is to output the text Hello World
to the browser.
You can also do various operations on text stored in variables using len, left and
right.
The len function simply tells you how many characters are in a string, so if you used
the following code:
which would set the value of the variable called StringLength to 11.
You can also use the functions left and right. These will display only part of the
variable. For example:
He
would display:
orld
Basically, these functions take the number of characters specififed from the left or
right of the string, so left("Some Text", 5) takes the first 5 characters of the text.
The Basics Of IF
If statements are used to compare two values and carry out different actions based
on the results of the test. If statements take the form IF, THEN, ELSE. Basically the IF
part checks for a condition. If it is true, the then statement is executed. If not, the
else statement is executed.
IF Strucure
The structure of an IF statement is as follows:
If something=somethingelse Then
Execute some code
Else
Execute other code
End If
Common Comparisions
The ASP IF statement construction is very much like plain text, but here is a quick
example of a common use of ASP. In this example the user has entered a password
which has been stored in the variable EnteredPassword. The idea of this script it to
check whether the user has entered the correct password:
Other IF Options
There are many of different comparisions you can make with ASP, for example you
can comapre two variables:
If EnteredPassword=RealPassword Then
If Age>13 Then
which will check to see if the age entered by the user is greater than 13.
You can also place HTML etc. in IF statements, as the ASP will continue executing a
THEN statement until it reaches an Else or an End If, and will continue to execute Else
statements until it reaches End If, for example:
<%
If EnteredPassword="password1" Then
%>
<font face="Arial" size="3">Congratulations. You may enter.</font>
<%
Else
%>
<font face="Arial" size="5" color="Red">ERROR! You cannot enter.</font>
<%
End If
%>
<%
For index = 1 to 10
Response.Write("Hello")
Next
%>
For index = 1 to 10
Repeat the following code until the variable 'index' is equal to 10, starting at 1 and
going up 1 by 1.
Next
This tells the server to return to the beginning of the loop and increment the variable.
<%
For index = 1 to 10
Response.Write(index)
Next
&>
STEP
Step is an extra part you can add on to the end of the For line of the code to change
the way it counts. In the loop above, the code starts by setting index to 1, then when
Next is reached it adds another 1 (2), the next time it adds another 1 (3) and so on.
Using, STEP you can change this action. For example:
<%
For index = 1 to 10 STEP 2
Response.Write(index)
Next
%>
Would output:
246810
While Loops
Another type of loop which can be used in ASP is the While loop. A While loop is
written as:
<%
Do While thenumber<10
Resonse.Write("Less than 10")
thenumber = thenumber + 1
Loop
%>
Do While thenumber<10
This code first checks if the variable thenumber has a value which is less than 10,
then if it is executes the following code until it reaches:
Loop
This tells the code to return to the Do line. Now, you may have noticed the problem
here. If all the Do line does is check whether thenumber has the value of less than
10, the loop will go on forever. This is why the line:
thenumber = thenumber + 1
has to be included. This increments the value of thenumber, so that it will eventually
be more than 10, and the loop will end. Of course, you aren't just limited to adding
and subtracting as you are with a For loop. You can make any changes to the
variable you like in the code.
Until Loops
A third type of loop is the Until loop. This is almost exactly the same as the While
loop:
<%
Do Until thenumber=10
Response.Write("Less than 10")
thenumber = thenumber + 1
Loop
%>
The difference between this and a While loop is that the code will execute until the
condition in the Do line is met, unlike a While loop where it will only execute while
the condition is met. As with the While loop you must increment the variable yourself.
Form Basics
All HTML forms are created using the form element:
</form>
The method attribute controls how the information that the user enters in the form is sent to the
GET
Sends the form data as part of the URL (e.g. "script.pl? name=Joe&
email=joe@joe.com"). This is the default option. It's useful and efficient for
small amounts of data (e.g. a search engine query) and it's easy for the user
to refresh the results of the form by just pressing the browser's refresh button.
However it cannot be used for large amounts of data (more than a few
hundred bytes).
POST
Sends the form data encoded in the HTTP data stream. This is recommended
for most types of forms (e.g. feedback forms and form mailers). The user will
not see the form data in the URL. Large amounts of data can be sent this way.
Unlike the GET method, the user cannot easily refresh the form results page -
they usually see a dialog asking if they want to resend the form data - but this
The action attribute specifies where the form data submitted by the user will be sent. Usually this is
http://www.yoursite.com/poll.asp.
If you're thinking you recognise this part of the tutorial, that's because it's part of the ELATED HTML
Forms tutorial. If you need any help on creating form fields, you might like to check out that tutorial.
Request.Querystring
We use the Request.Querystring collection to retrieve data posted from forms that use the GET
method. The collection contains an entry for each form field posted to the server. Assume we have an
<option value="Mr">Mr.</option>
<option value="Miss">Miss</option>
<option value="Ms">Ms.</option>
<option value="Mrs">Mrs.</option>
</select><br>
</form>
We could use
Request.Querystring("title")
Request.Querystring("firstname")
Request.Querystring("city")
to retrieve the values entered by the user. There would be a named entry for each named form field, so
the "submit" button would also result in a value being stored in Request.Querystring("submit").
Request.Form
ASP provides the Request.Form collection to retrieve data sent from forms using the POST
method. As with the QueryString collection, the Form collection also contains an entry for each form
field posted to the server. So, taking our example form above and changing the GET method to be a POST,
we could use:
Request.Form("title")
Request.Form("firstname")
Request.Form("city")
Request.Form("submit")
to retrieve the values entered by the user. Sometimes you'll see ASP code where
Request.QueryString("field_name") or Request.Form("field_name") has been
idea to explicitly reference the collection you want to use - it's faster to execute and it avoids ambiguity
Multiple values
Sometimes we might have a form that contains a set of checkboxes. If we make these a group by
giving them the same name, all the checked box values will be sent to the server using the same field name:
</form>
Assume we checked all four boxes, we can access the values like this:
Request.Form("interests")(1)
Request.Form("interests")(2)
Request.Form("interests")(3)
Request.Form("interests")(4)
We can also use the Count property to find out how many values were submitted. (In the above
(If you're not familiar with loops, you might want to read our loops tutorial.) However, usually we'd
Next