Free Guides
Language Tutorials

ASP ( Active Server Pages )
Index

What Is ASP?
"Active Server Pages" abbreviated
as ASP, is a server-side scripting environment developed by
Microsoft. Using ASP, we can create and run dynamic and interactive
web server applications. With ASP, we can combine HTML pages, script
commands, and COM components to create interactive Web pages or
powerful Web-based applications, which are easy to develop and
modify.
The default scripting language used for writing ASP is
VBScript although we can use JavaScript. All ASP pages will be
executed at the server and the page will have the extension ".asp".
An ASP page cannot run by simply opening it in a web
browser. The page must be requested through a web server that
supports ASP, this is why ASP stands for Active Server Pages, no
server, no active pages.
When a browser requests an ASP page, the web server
interprets any ASP contained within the web page before sending the
HTML produced to the browser. In this way, the entire ASP is run on
the web server and no ASP will ever be passed back to the web
browser.
Installing ASP
Just as we have seen before,
for running ASP we need a web server, which need not be an external.
We can install web server in our PC. Microsoft provides web servers
such as Personal Web Server (PWS) which can be run on any
Windows95/98, NT or 2000 workstation and Internet Information Server
(IIS) for Windows NT or Windows 2000 based web servers.
Installing Personal Web Server (PWS)
Personal Web Server ships with Windows 98 CD, you can
find it in add-ons folder present in the Windows 98 CD. If you are
using Windows 95, you have to download "Windows NT 4.0 Option Pack"
from Microsoft.
- Insert your Windows 98 CD into your CD-ROM Drive.
- Explore your CD-ROM Drive.
- Open “add-ons” folder, there you will find “pws” folder, run “setup.exe” from this folder.
- Follow the instructions on the screen to install “Personal Web Server”
After installation of PWS, you
will find a PWS icon on your desktop and most probably a small icon
of PWS in the right corner of the taskbar. Open any one of them. If
the PWS is running then you will see an option "Stop PWS" or else
you will see "Start PWS".
To check if PWS is running, open your Internet
Explorer. In the URL box enter http://127.0.0.1 or http://localhost
and press Enter. If everything is working fine, you will see a
default page generated by PWS. If you can see any page then PWS is
running correctly or else you have to restart your computer and
start PWS again and continue the same process. Hopefully everything
will be alright now.
Installing IIS on Windows 2000:
Microsoft Internet Information Server (IIS) is a web
server that integrates into the Windows NT Server so no need to
download from any where. If you haven't install IIS during
installation of Windows 2000, then follow the below steps to install
IIS.
- From Start Button, go to Settings and Control Panel.
- In the Control Panel, select Add/Remove Programs, in that Select Add/Remove Windows Components.
- In the wizard, check Internet Information Services, click Ok. After this, an Inetpub folder will be created where you can find a folder named wwwroot.
Now your installation process is
over. If you want to check whether IIS is running or not, Open
Internet Explorer and type http://localhost in the address bar. If
everything is working fine, it will display the default page
generated by IIS. If not,
- Go to Control panel. Open Administrative Tools and double click "Internet Information Server".
- Select "Default Web Site" and Click on the Start button present in the toolbar.
Basic file structure of ASP
After installing IIS the web
server let us start our first ASP page. Here we assume that ASP
engine is working perfectly and server root is set to proper
directory were we will keep our fist asp page. Here if the directory
path is E:\myfiles\ and our first ASP file name is test.asp then the
path becomes E:\myfiles\test.asp. To open this file in web browser
mode we have to open this as
http://localhost/test.asp
or
http://127.0.0.1/test.asp
Note that in IIS we have set the default directory to E:\myfiles\
Now with this settings let us go for our fist file test.asp
We will start with our ASP code beginning mark <% and end the code
with %> . This way we will tell our ASP engine to execute the ASP
part of the code within the <% and %> symbol and send the other
codes as it is to the browser. So we will start with telling the
server that the code we are going to use is VB Script. ASP is not a
script or language , it is a technology server uses. So we have to
tell the server which language we are using. We can also use
JavaScript as server side script. But mostly VBScript is used for
server side scripting requirements and we will also use this. Here
is the way we will tell to use VBScript language for processing.
<%@ Language=VBScript %>
In the next line we will tell that
all the variables used here are to be declared before using. This
has some advantage as we move to words developing complex
applications using ASP. This is a good practice to follow. So here
is our next line
<% Option Explicit %>
After this we will keep our html meta
tags and then we will go to the body section of the ASP page. We
will try to print the message “Hello World” to the screen. Here we
will store this message string in a variable and let us name the
variable as msg. Please note that we have told ASP engine that all
the variables we use are to be declared before, this we have told in
the second line of our code by saying <% Option Explicit %> . So we
will declare the variable by using the command Dim. Here is the
example
Dim msg
msg=”Hello World”
Now let us print the message or
the variable to the screen by using Response.Write object.
Response.Write msg
That’s our entire first ASP example. Here is the complete
code.
<%@ Language=VBScript %>
<% Option Explicit %>
<html>
<head>
<title>(Type a title for your page here)</title>
</head>
<body >
<%
Dim msg
msg="Hello World"
Response.Write msg
%>
</body>
</html>
Syntax for declaring variables in ASP
ASP ( VB Script ) provides also
easy use of variables but some of the basic syntax is to be
followed. One of the biggest advantage of ASP variables is it is not
case sensitive. If I declare a variable as my_var then I can still
access the same variable by calling it as My_var.
We also have to follow some syntax for declaring variables.
All variables declared must start with a letter. Number or any other
key are not accepted as variable. So variable name like 1my_var
is not accepted but my1_var is accepted.
Length of the variable name should
not exceed 255 characters.
Space period and dashes are not allowed within the variable name.
Reserve words are not allowed. Example we can't declare response as
a variable
The best way is to be use descriptive variable names for easy
understand of the purpose and identification. Birth date variable
should be declared as variable like birth_date
We will use more ASP code and see the use of variables in other
tutorials.
Getting the variable type and checking in ASP
In ASP we can check what type of (
data ) variable it is before using. We can use the function
VarType to display a value for associated with each type of
variable. This value is a number so linking this number to type of
data is to be done. There is another function TypeName which
displays the Data type directly without any associated value.
We can check for a specific type by using the function isArray,
isDate etc functions. These are useful while developing code
for further processing while working with variables.
Let us start with some syntax of TypeName function.
v1="Welcome" 'assigning value
Response.Write "Value of v1=" & v1
Response.Write "<br> v1 is a <i>" & TypeName(v1) & "</i> variable "
In the above code we have declared v1
as a variable and assigned a string data to it. So now the variable
v1 became string variable. So we will get an output like this by
using the above code.
v1 is a String variable
We can change the value of v1 to different
values and see the type of variable TypeName function
returns.
We can create a double variable and check with isArray
function to get true or false. Here is the code for this.
v1="Welcome" 'assigning value
Response.Write "Value of v1=" & v1
Response.Write "<br>is v1 an array? <i>" & isArray(v1) & "</i> "
The output of the above code is
shown here.
is v1 an array? False
Advantage of using ASP and where to use
ASP is a server side script and it
can perform many backend processing jobs for a web site, same way it
can't do many things particularly the client end operations as it
has no control over the client end events or user controls. So there
are many advantages and disadvantages of using ASP to manage web
pages. We may not like to say disadvantages of ASP and it is better
to say the limitation of any server side scripting language, as
these limitations are faced by other scripting languages like PHP,
JSP, Perl etc. We will try to understand where ASP can be used and
where it should not be used.
Using server side scripting
language like ASP we can manage the content of any page and such
dynamic code ( or content ) for the web browsers can be generated
based on various conditions we set in our ASP program. To a visitor
the back end script or processing is of no use. They are more
interested in design, content, speed etc. So there is no point in
asking ASP engine to work when there is simple html content to be
displayed at browser end. This will also improve the loading speed
of the page. The user browser does not understand scripts we run at
server. Brower understands JavaScript (if enabled), html, CSS etc.
So what ever output we want to be send to the browser has to be
written using these languages. So the task we want to be performed
at client end can't be written at server end using ASP.
ASP engine finishes its job of processing the code and then send the
codes to users browser. From this point on words till again the page
request comes back to server, there is no control of ASP on the
page. So we should not expect ASP to perform some tasks which are
likely to happen at the client browser end. This will be clear when
we discuss some of the task and where ( client or server side ) the
task is to be completed and which script will take care of it. There
are some tasks we can do at both the ends and the situation on which
they are to be used we will try to discuss in next page.
Where to use ASP and where not to use ASP
There are many jobs to be
performed and we will try to understand what type of job we should
do at server end and what we will delegate to client end JavaScript
or HTML. Let us start with some tasks we want our web page to do.
Here are some of the common jobs we will discuss to get an idea on
what to use for different tasks a web page perform.
Displaying Alert message in a small window
Small windows are client side property. So we need Client Side
JavaScript to display the alert message. But the content of the
alert message we can manage by using ASP. We can conditionally (
display or not ) show the alert box and manage the content ( text
part ) based on ASP code we write. So let us conclude that showing
an alert message is client side task and we have to use JavaScript
for this.
Authenticating user
before entering to members only area
This task we can't handle at client end. Once the user enters userid
and password, we will verify that using a membership table and then
we can say the user is allowed or not. This job is to be handled by
ASP script running at server end. Now displaying the welcome message
or asking the member to try again is decided by ASP script and the
display is shown at browser by using html tags.
Validating data entered by user in a text box ( or form )
This we have seen at many web sites. If we are suppose to enter
price in figure and if we enter any character in the text box then
we get an error message. Here validation of the data entered by the
user is done and accordingly error message is displayed. This task
can be done at server side and client side also. Depending on the
requirements we can develop the code. There is a risk if we do it at
client end only. If script execution is off at client end then the
validation code written using client side JavaScript is not going to
check and display any error message. This will allow un-validated
data to enter into system. However we can check the browser setting
of the client computer and display a message or redirect to a page
asking them to enable the script execution first before using the
form. Depending on server side validation is full proof but for
bigger forms large amount of data has to return again from server if
data validation fails. This is going to take more time particularly
for the visitors with slow connection. So
validation is used based on requirements of the page.
We have discussed three common applications here and we will able to
understand more on this as we develop scripts for different
applications.
Different requirement for redirecting users
We redirect the user to different
pages or different site based on the condition or based on user
selection. We may need different pages to be executed under
different conditions. Some time visitor goes to different pages by
clicking different links available on the page. But here we will
learn how to redirect the user based on our program or our
requirement. There can be many reasons to go for a redirection. Here
are some of the common reasons for going for a redirection.
Server side form validation
We submit a form with data and we can design a server side form
validation to check the user entered data. If the data entered by
the user is not in required format or not as per standards then we
may ask the visitor to feed the data again. So here we will send
back the visitor to the same form page again showing the error
message and asking the visitor to try again.
Sending users to different sections based on the permission level
In a portal we may have different areas marked for different types
for users and we want the users to go directly to assigned area
after they submit their identity ( may be by login ) . Here without
any trigger from user end system will redirect the user to a page
for there use.
End of a process.
Some time after submitting a data or after a forum posting you want
the visitor to be redirected to display a Thank You message. We can
have a common thank you page or an error page where we can redirect
the browser.
We will discuss header redirection here. Read more on the same topic
in PHP section header redirection.
Header Redirection precaution
One main care we have to take care before giving any header
redirection is no data to be posted to the user browser before the
header redirection command is applied. We can do our script
processing and before sending any output to the browser we will
redirect the user. Sometime we may not have any output intentionally
posted before the redirection but under various condition some time
error message the script will return, resulting in error message
saying header already send.
Here is the common error message given by ASP engine if header is
already sent and script example of header redirection using Response
object.
Redirecting user by using response object in ASP
We can do header redirection in
ASP by using Response object. Read the basics of header redirection
requirements in different condition. Response object takes the url
of the site to which the browser is to be redirected. It can be any
full URL like http:www. sitename.com or it can be a relative url if
within the site. Here is the basic syntax of the Response.Redirect
Response.Redirect "http://www.itsyllabus.com/"
We can't apply redirect
command if we have already posted any data to the browser. In such a
case ASP engine will return error like this .
Response object error 'ASP
0156 : 80004005'
Header Error
redirect.asp, line 15
The HTTP headers are already written to the client browser. Any HTTP
header modifications must be made before writing page content.
So we have to take care that
our redirection command should work before any data is posted to
browser.
Printing double quotes in ASP
In ASP VBScript we can't print a
double quote inside at output statement. We have to take special
care to display a double quote inside or outside a string.
(In PHP we can easily escape the double quote by using escape
character ( like \" ) ).
Here are some examples to display double quotes in ASP
We can use chr(34) to print a double quote to the screen
Response.Write chr(34)
( Try different values of chr
to display different characters to the screen )
We can use " to print double quotes to the screen
Response.Write """
Some time within a string we
will have double quotes to print. Here to print one double quote we
will escape it by one more double quote so two double quotes are to
be used. Read this line.
Response.Write "Raju told ""ASP is
a better scripting language"""
The above line will print
Mark the use of two double
quotes within the string to display single double quotes.
Some time we have to display some variable or strings with double
quotes within a table cell. Here is a code to display session id
inside a table cell.
Response.Write "<tr><td>session(""userid"")</td><td>"
The output of the above is
session("userid")