Basics of ASP
This introduction
is to explain what ASP is, it's uses and how you as a developer can use
it to spice up your static HTML pages.
First let me tell
you a bit about the kind of person writing this tutorial. I have been
developing HTML web sites for a couple of years and just within the last
year have delved into ASP. I'm not a techie person, so I need explanations
that are in plain english. If that's the kind of person you are, then
you'll fit right in with these tutorials.
ASP is a pseudo-programming
language aimed at HTML development. It allows web pages to do more than
contain just static content. By placing ASP tags in with your HTML tags,
you can have a page that interacts with the user. The page can make decisions
based on logic and user input. If you're familiar with HTML, then you
know that an HTML tag uses <> around it's tags. For example <b>this
text will be bold</b> would make this text will be bold.
ASP is similar in that it uses delimiter tags like HTML. However, the
tags differ slightly. An ASP delimiter tag starts with <% and ends
with %>.
ASP was developed
by Microsoft and it's core language is based off of Microsoft's Visual
Basic. The language ASP uses is VBScript. However, you can change this
if you are familiar with another language that you would like to use instead.
At the top of an ASP page, before the first <html> tag, you would
put <% @Language=VBScript %>. If you wanted to use another language,
you would make the change in this tag. There is a reason for this. ASP
is not like HTML in certain ways. ASP is interpreted at the server instead
of by the browser. This is what makes ASP dynamic. By taking user input,
connecting to a database or whatever, ASP is translated on the server
and outputs pure HTML. Because ASP was designed for Microsoft's NT servers,
it's default language is VBScript. However, it is good coding practice
to always include the specified language of your code at the top of the
page, so the server knows how to interpret it. ASP can also run on a UNIX
server, but the UNIX server must be running ChiliSoft. To develop ASP
on your own computer, you must be running either an NT server product,
or Win95/98 Personal Web Server. You can usually find PWS on your Windows
CD. Just run a find>files or folders on your Windows CD and search
for PWS. After running the installation for PWS, you must open all ASP
pages by typing into the address bar http://localhost/nameofmypage.asp.
You cannot simply open an ASP page like you normally would open an HTML
page.
ASP's primary use
today seems to be geared towards database work. However, there are many
other uses for ASP. Companies use ASP to create virtual storefronts and
shopping carts. You can use ASP to password protect pages on your website
(a tutorial on this is coming:), to create a messageboard system and many
other uses. If you scour the web, you're sure to find tutorials for almost
anything with ASP. By tying your ASP pages into a database, you can have
a very dynamic site based on user interaction with your database files.
This is very fun to do and tutorials on database interaction will be coming
soon!
I'll give you 2 coding
examples for now to show some very basic uses of ASP on your website.
The first example displays the date and time on your page.
<%
@Language=VBScript %>
<%Option Explicit%>
'this tag is used to display any errors in your pages and
' is another good coding practice. It helps to cut down on bugs.
<html>
<head><title>My First ASP Page</title></head>
<%
Dim today, clock, both 'Dim is a keyword
to declare variables like var for javascript
today = Date 'Date is a built in VBScript
function that returns the current Date
clock = Time ' Time is a built in VBScript
function that returns the current Time
both = Now ' Now is a built in VBScript function
that returns both the Date and the Time
%>
Output : Today's date is <%=today%> and the current time is <%=clock%>.<br>
Output : To see both together, we will use the both variable here to display
<%=both%>.
</body></html>
The first thing we
did above was tell the server that this ASP page was written in VBScript.
Secondly, we declared Option Explicit to make sure that if the page contains
bugs, it will show the bugs. Next, we declared our variables using the
Dim keyword followed by our variables. Then we set our variables equal
to the values we wanted them to hold. Finally, we displayed the values
of our variables by including them in our regular HTML but by using the
<% and %> delimiters. ASP has many built in functions like the Date,
Time and Now functions. We will explore those more as our tutorials progress.
Notice the use of the ' before a comment.
This apostrophe tells the server that anything that follows is a comment
and not to try to interpret it as a script.
Our second script
will give you a brief introduction to the request object in ASP. We will
not get into a full tutorial on the request object here, but it's enough
to say that it's a built in object of ASP that allows information to be
passed from one page to the next. We are also using an if then statement
which we will not be covering fully. Just realize that this allows the
page to make a logical decision based on whether certain criteria have
been met.
<%
@Language=VBScript%>
<% Option Explicit %>
<%
Dim name, age
name = trim(request.form("name")) 'get
the value of the name field from the form
age=trim(request.form("age")) 'get
the value of the age field from the form
%>
<html><head><title>My Name Form</title></head>
<form method="post" action="intro.asp">
Type your name here! <input type="text" name="name"><br>
Type your age here! <input type="text" name="age"><br>
<input type="submit" name="submit" value="submit">
<form>
<%
If name <> "" and age <> "" then '
tests to make sure that the values of name and age variables are not empty
strings
%>
Output : My name is <%=name%> and my age is <%=age%>.
<%
else
%>
Please fill out the form above and click submit!
<%
end if
%>
</body></html>
This script is a little
more complicated than the first, but a challenge is always fun, right?
You can do it! Okay, first thing we did was, again, tell the server that
we were using VBScript for our language and then declare Option Explicit
to catch bugs. Then we declared our variables using the Dim keyword. The
next part is the new part. We used the request object to "request"
the contents of the form. See how it is followed by a .form? That tells
the page to get the contents from the form input field called name and
give the value to the variable called name. The age variable does the
same thing, only requests the value from the input field called age. We
use the trim function to "trim" off any extra spaces before
or after the string. Later in the page, we use what's called an if then
statement to make our page do some basic logic. It should be pretty easy
to follow, but in case you're lost, here's the explanation. The first
line says "If name <> "" and age <> ""
then" do this.... This is telling the server that if the variables
name and age both are equal to nothing, then do what follows (which is
to print out a string including the name and age that was submitted on
the form). This keeps the page from displaying an empty value if only
one field was filled in and then submitted (You can always change the
"and" keyword to "or" if you don't mind having only
one value passed). Then the if statement continues by saying "else...do
this". So, if the form was submitted without any fields being filled
out, or if you just came to this page, then it would print a different
message asking you to complete and submit the form above. If you copy
this code, make sure to save the page as intro.asp so the form submits
to itself.
As you can see, ASP
can be a great language to learn to develop web pages with. It enables
you, the HTML developer/designer to create more interactive pages that
respond to user input.
To ensure smooth work of the ASP scripts consider using webhosting specialized to meet all your programming needs.
I hope you've enjoyed the introduction to ASP and
I look forward to bringing you more tutorials to help you learn how to
master this wonderful development language. See ya next time!
~ Geoff
Swartz |