ASP (ACTIVE SERVER PAGES)
by Fikret Sivrikaya
1. INTRODUCTION
In the early years of the Internet and World Wide Web (WWW), web pages were all static documents aimed at presenting information in some field. As this large network of computers began spreading widely all over the world, the content and information on the web increased in huge amounts. In the age of technology, the need for all kinds of information grew substantially, and the role of Internet on information retrieval and data sharing became more distinct.
The
idea of benefiting from the Internet more than just sharing static information
came into existence, as the Internet provides a powerful means for connecting
companies to their customers worldwide, and other companies they work with.
Therefore new technologies arose enabling programmers to create web pages with
dynamic content that may come from a database on the web server. The use of
dynamic web applications and interactive web pages enabled many business interactions
to be done through the Internet. E-business, e-commerce and CRM (Customer
Relationship Management) applications over the Internet are some outstanding
application areas of these technologies.
The
basic tools that can be used to implement dynamic web programming include ASP
(Active Server Pages), PHP (stands for Personal Home Page), CGI Scripting, Cold
Fusion, Java Servlets and JSP (Java Server Pages). We will present in this
paper, ASP, the Microsoft technology developed for web application programming.
2. ELEMENTARY CONCEPTS OF ASP
2.1.
What is an Active Server Page?
An Active Server Page is a text file that resides on a Web server. When a Web client (browser) calls an Active Server Page, the Web server processes the code in the Active Server Page and returns standard HTML to the browser. The advantage Active Server Pages have over standard HTML pages is that they are dynamic; the HTML sent to the browser is generated "on-the-fly" by the server and is dependent on the previous actions of the browser user.
A browser calls an Active Server Page in the same way that it calls a standard HTML page. The only difference is in the file extension, whereas HTML pages have extension htm or html, Active Server Pages have the extension .asp. An Active Server Page looks something like this:
<HTML>
<BODY>
<% For i =
3 To 7 %>
<FONT
SIZE="<% = i %>">Hello World!<BR>
<% Next
%>
</BODY>
</HTML>
An Active Server Page consists of scripting language statements and standard HTML code. To distinguish between the two we put the scripting code in brackets <% %>. When the URL of the Active Server Page is entered in the browser, the web server processes the Active Server Page code and would, in this example, return the following HTML
<HTML>
<BODY>
<FONT
SIZE="3">Hello World!<BR>
<FONT
SIZE="4">Hello World!<BR>
<FONT
SIZE="5">Hello World!<BR>
<FONT
SIZE="6">Hello World!<BR>
<FONT
SIZE="7">Hello World!<BR>
</BODY>
</HTML>
The processing is invisible to
the browser and the user and all they see is standard HMTL.
The default scripting language
for Active Server Page is VBScript, which is a subset of Visual Basic for
Applications (VBA) which itself is a subset of Visual Basic. However you are
not limited to using VBScript as your scripting language. Other alternatives
include Jscript and Perl.
2.2.
Requirements to run Active Server Pages
Web Server
Active Server Pages runs as a component of Microsoft Internet Information Server web server (Versions 3.0 and above) on Windows NT server. It will also run with Microsoft's Personal Web Server on Windows 95, 98. So Active Server Page technology is tied in with Microsoft web servers. However third parties are developing Active Server Page add-ins for other web servers (see www.chillisoft.com).
ODBC Drivers
If you are
going to use Active Server Pages to interface with databases then you will need
an ODBC driver for your particular database system. The Active Server Page
installation includes drivers for Microsoft Access and Microsoft SQL Server.
Although it
is possible to generate Active Server Pages with development tools such as
FrontPage 98, a simple text editor is fine for writing Active Server Pages,
since they are text files in their presence. Third party text editors such as TextPad
offer more functionality than the standard Windows editors like WordPad.
2.3. Why use Active Server Pages?
There are a number of alternatives for those wishing to create interactive, dynamic web pages. Alternatives include using CGI scripting and more specialist development tools such as Cold Fusion. Here are some reasons for using Active Server Page technology
Active Server Page technology does have some drawbacks. In particular is its reliance on Microsoft platforms. Active Server Page technology does not provide a full program development environment so activities such as debugging a script may not be straightforward. However since most Active Server Pages tend to be fairly short this is rarely a problem.
3. Active Server Page Programming with VBScript
VBScript is
a scripting language rather than a programming language, such as Visual Basic
or C++. Scripting languages tend to be less rigid in their rules than
programming languages. For example, in VBScript (and Perl) you need not
predeclare variables before using them.
Values are assigned to variables in the standard way. For example,
x = 24
You do not need to declare variables before using them (although many would say it is good practice to do so). There is only one data type, called variant. As its name implies it can store a variety of information - numeric, string, Boolean - depending on the situation. This is similar to how Perl handles data types.
How do we return the value of a variable to the user? If we return to our original example
<% for i =
3 to 7 %>
<FONT
SIZE="<% = i %>">Hello World</FONT><BR>
<% next
%>
we can include the
value of a VBScript variable within HTML using this format:
<% =
variable_name %>
Note that the spaces are not significant.
We can add comments to our VBScript code using a single quote. For example
'this is a
comment
Below are the
examples of basic program flow structures:
If X < 0
Then
%>
<B>Negative
Number</B>
<%
ElseIf X >
0 Then
%>
<B>Positive
Number</B>
<%
Else
%>
<B>Value
is 0</B>
<%
End If
%>
The following
code prints the value of X ten times,
X=10
Do While X > 0
%>
<B><%=X%></B><BR>
<%
X = X - 1
For X = 1 To
5
Statements
Next
3.2. Built-in Functions
VBScript contains a number of built-in functions. Functions take an argument and return a value. The argument is supplied in brackets. For example the Len function returns the number of characters in a string
MyWord="Potato"
X =
Len(MyWord)
The value of X would be 6.
3.3. User Created Procedures - Sub and Function
The Sub procedure contains a group of VBScript statements which are
executed when the sub is called. The Sub may take arguments but does not return
a value. A sub is defined as follows:
Sub sub_name
statement group
End Sub
4. HTML FORM HANDLING AND
MAINTANING STATE
An HTML form is populated with controls. These could include text
boxes, radio buttons, and drop-down menus. When the form is filled in, and the Submit
button clicked, the browser parcels up the name of each control and the value
entered in each and sends this information to the Web server. This string of
data is known as the query string. A typical query string may look like
surname=smith&forenames=David+John&gender=1
The web server passes the query string to the Active Server Page specified in the HTML form. The Active Server Page processes the query string and sends back HTML to browser via the Web server.
HTML forms are specified as follows:
<FORM
METHOD="POST | GET" ACTION="path_of_Active_Server_Page">
Form controls
</FORM>
The METHOD parameter, which can be either GET or POST specifies how the form data is sent to the server. There are subtle differences between the GET and POST methods. The ACTION parameter specifies the name of the Active Server Page which is going to process the data.
4.2. How Active Server Pages handle HTML form
data (The Request Object)
As discussed in the previous section the web browser sends the form
data in the form of a query string. The query string is parsed automatically by
ASP and stored in the Request object. The form control values can be
accessed within VBScript using
Request("control_name")
Web pages are stateless, the user does not log on to the server.
A page is requested via the browser, the server delivers the page and the
transaction is complete. However, we may wish to keep values entered in HTML
forms for use by subsequent pages. These values could represent personal
preferences or a username, password combination, say. One way of doing this is
by using hidden fields in a HTML form. Values can be stored in a hidden field
and passed from page to page. This method although effective is somewhat
cumbersome. An alternative is to use the ASP session object. Say for
example a HTML form has fields for student_ID and password. We can extract the
entered values using the request object and store them in the session object.
For example,
Session("student_ID")=Request("student_ID")
Session("password)=Request("password")
The session variables are then available to other Active Server Pages
in the site. For example to reference the student ID value we use
student_ID=Session("Student_ID")
Session variables
are available as long as the session is active. The session will be closed
after 20 minutes of inactivity.
5. DATABASE INTERACTION IN ACTIVE SERVER PAGES
5.1. How Active Server Pages interface with
databases
Active Server Pages interface with databases via ODBC (Object Database
Connectivity). ODBC is a protocol which allows applications to interface with
databases. ODBC runs on a variety of platforms and is used by numerous
applications and database systems and is therefore a widely used standard. ODBC
sits between the application (in our case an Active Server Page) and the
database. The database is registered with ODBC using a Data Source Name (DSN)
which is used by the application to refer to that database. Only ODBC needs to
name the type of database and its location, the application merely needs to
know its DSN.
We can use VBScript to pass commands to the database via ODBC. These commands use the Structured Query Language (SQL) to query the database. SQL is a standard language which can be used for querying most databases. We can use SQL to search, update and delete records from a database. SQL is very powerful and SQL commands can get quite lengthy when used for querying complex relational databases.
5.2. Configuring ODBC
Active Server Pages interact with databases via Object Database
Connectivity (ODBC). As explained earlier, ODBC is simply a cross-platform
method for allowing applications to interface with databases. The application
connects to the database via ODBC and then sends the SQL query to ODBC. To use
ODBC we need to make our database known to ODBC. We do this by registering a
database file in ODBC with a Data Source Name.
5.3.
Creating an ODBC Data Source Name
1. From the Windows Control Panel double-click the ODBC icon.
2. Select the System DSN tab.
3. Click the Add… button
4. You are then prompted for a database driver. If you are using an
Access database then select Microsoft Access Driver (*.mdb). Click Finish.
5. Next to enter a Data Source Name. You can optionally provide
a Description.
6. The next stage is to provide a database. Click the Select… button.
7. Use the file window to select a database name then OK.
8. Once you have provided a DSN and a database name click OK twice.
We can now refer to the database in an Active Server Page simply by using the DSN. We do not need to know the location of the database file or the even the type of database - all that is handled by ODBC.
5.4. Connecting to a database using Active Server
Pages
The following
four steps will allow us to query a database
1. Create instance of database component
Set instance_name =
Server.CreateObject("ADODB.Connection")
We will use instance_name throughout when communicating with the
database.
2. Open a connection to the database
instance_name.Open "data_source_name"
where data_source_name is the DSN as specified in ODBC
3. Create an Instance of the Recordset
Set recordset = Server.CreateObject("ADODB.recordset")
The recordset is where we are going to store the results of the query.
It will contain a number of database records depending on the result of the SQL
query.
4. Execute the SQL statement and store the result
recordset.Open query_name,instance_name,3
where query_name is a string containing the SQL query.
The 3 is a parameter which specifies the cursortype. This specifies how we execute the query, an explanation of the options for this parameter are beyond the scope of this paper.
In the example
below, lines 2 and 3 connect to the database with the DSN “members”. It starts
with creating a connection object, then call its “Open” method in order to
establish a connection with the database. Line 4 initializes the variable query
as the query string that we want to ask for from the database. Line 5 creates
an instance of recordset object to hold the results of our query. Line 6
actually executes the query by calling the “Open” method of the recordset
object.
Lines 13 through 23 iterate over the recordset object and prints out the results on a table on the web page. The first column of the table lists the usernames fetched from the database and the second column lists the corresponding passwords for the usernames.
