Sei sulla pagina 1di 14

Active Server Pages (ASP) is a server side scripting language that lets a webmaster

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.

As mentioned earlier, ASP is a server-side scripting language. Basically what this


means is that if an ASP page is requested, the web server will process it and run all
the ASP code, before sending the output to the browser. This has two major
advantages over client-side (processed by the browser) scripts like JavaScript. The
first is that there are no compatibility problems. It doesn't matter if the user is using
the latest browser or the oldest, they will see the same output. The second is that
your code is hidden. Because code is executed on the server, users only ever see the
output, so it is safe to put passwords etc. in your ASP code.

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:

1. Look for a new folder called Inetpub on your hard drive


2. Open the Inetpub folder, and find a folder named wwwroot
3. Create a new folder, like "MyWeb", under wwwroot
4. Write some ASP code and save the file as "test1.asp" in the new folder
5. Make sure your Web server is running (see below)
6. Open your browser and type "http://localhost/MyWeb/test1.asp", to view your first web page

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.

How to Install IIS on Windows XP and Windows 2000


Follow these steps to install IIS on Windows XP and Windows 2000:

1. On the Start menu, click Settings and select Control Panel


2. Double-click Add or Remove Programs
3. Click Add/Remove Windows Components
4. Click Internet Information Services (IIS)
5. Click Details
6. Select the check box for World Wide Web Service, and click OK
7. In Windows Component selection, click Next to install IIS

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:

<%@ Language=VBScript %>

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:

<% ASP Code Here %>

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:

This is some HTML. Below this I have ASP

Nothing should appear above here.

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.

Sending Output To The Browser


It's always been a tradition of programming tutorials to begin by writing the simple
'Hello World' program, so this one won't make an exception! Sending output is done
using the ASP command:

Response.Write()

so to write 'Hello World' to the user's browser the complete code would be:

<%@ Language=VBScript %>


<%
Response.Write("Hello World")
%>

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:

OutputText = "Hello World"

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:

<%@ Language=VBScript %>


<%
OutputText = "Hello World"
=OutputText
%>

In this example, the = sign is used instead of ResponseWrite.

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:

<%@ Language=VBScript %>


<%
OutputText = "Hello World"
%>

This is my <% =OutputText %> script. The whole reason for it is to output the text <
% =OutputText %> to the browser.

which would display in 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:

<% =len(OutputText) %>

The server would return to the browser the


length of the text stored in OutputText, in this case "Hello World", so the browser
would display the number 11 on the screen. You could also assign this value to a
variable using:

<% StringLength = len(OutputText) %>

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:

<% =left(OutputText, 2) %>

which would display:

He

and the code:

<% =right(OutputText, 4) %>

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:

<%@ Language=VBScript %>


<%
If EnteredPassword="password1" Then
Response.Write("Well done. You got the password right.")
Else
Response.Write("Sorry. That was the wrong password.")
End If
%>

If the user enters the correct password (password1) the text:

Well done. You got password right.

but if you get it incorrect you will be shown the text:

Sorry. That was the wrong 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

or different types of comparison:

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 and NEXT Loops


FOR/NEXT loops are used when you want to execute a piece of code a set number of
times. If, for example, you want to output the world 'Hello' 10 times, you could either
code it manually or you could use:

<%
For index = 1 to 10
Response.Write("Hello")
Next
%>

Basically, this code says:

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.

Using The Variable


A loop isn't much use if it just does the same thing over and over again. It really
offers no benefits over a simple piece of code. The real power appears when you use
the counter variable in your code. If, for example, I wanted to output the numbers 1
to 10 I could use:

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

It is counting up in 2s. You can also count down:

For index 10 to 1 STEP -1


which will count down from 10 to 1.

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

To explain this code:

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 method="xxxx" action="xxxx">

(form fields in here)

</form>

The method attribute controls how the information that the user enters in the form is sent to the

server. The two options are:

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

is often a good thing!

The action attribute specifies where the form data submitted by the user will be sent. Usually this is

the URL of a script on the server - for example, http://www.yoursite.com/feedback.asp or

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

HTML form as follows:

<form method="get" action="querystring.asp">

Title: <select name="title">

<option value="Mr">Mr.</option>

<option value="Miss">Miss</option>

<option value="Ms">Ms.</option>

<option value="Mrs">Mrs.</option>
</select><br>

First name: <input type="text" name="firstname"><br>

City: <input type="text" name="city"><br>

<input type="submit" name="submit" value="Send">

</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

written as Request("field_name"). This is a valid short-hand notation, however it is usually a good

idea to explicitly reference the collection you want to use - it's faster to execute and it avoids ambiguity

where an item in a different collection might have the same name.

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 method="post" action="checkbox.asp">

Please check the boxes to indicate your interests:<br>

<input type="checkbox" name="interests" value="film"> Film<br>

<input type="checkbox" name="interests" value="music"> Music<br>


<input type="checkbox" name="interests" value="theatre"> Theatre<br>

<input type="checkbox" name="interests" value="sports"> Sports<br>

<input type="submit" name="submit" value="Send">

</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

example, Request.Form("interests").Count equals 4.) This allows us to loop through the

values using a For ... Next loop:

For counter = 1 To Request.Form("interests").Count

Response.Write "You selected " & Request.Form("interests")(counter) & "<br>"


Next

(If you're not familiar with loops, you might want to read our loops tutorial.) However, usually we'd

want to use code like this to retrieve the data:

For Each item In Request.Form("interests")

Response.Write "You selected " & item & "<br>"

Next

Potrebbero piacerti anche