Introduction
Active Server Pages 
Contents paper :
1. Active Server Pages basics
(Response.write, Session, Parameter scooping)
2. Connecting to a database
3. Reading text files
4. The benefits of ASP
This paper is an introduction in Active Server Pages. The basics of
ASP are discussed.
1. Active Server Pages basics
Active Server Pages (ASP) is a relative new technology to build
web sites. In this paper we try to give you a clear view on ASP and what it can do for
you.
ASP is not a programming tool, ASP is a Microsoft
technology that expands the possibilities of the (web)server with a number of objects.
With ASP you can build web pages dynamically. To use ASP the server must be
provided with Microsoft's Internet Information Service .
ASP offers objects for saving variables, getting information from
users and servers and to generate HTML files.
ASP also offers database objects to give the ability of using ODBC databases.This is done
by ActiveX Data Objects. (ADO)
You can use VBScript or JavaScript to work with the objects. VBScript is the default
language (why doesn't this surprise me ?).
ASP code can be put between the HTML code, between the marks '<%' and '%>'.
In this example we see a bit of ASP between the HTML code :
<html>
<head><title>Een stukje ASP</title></head>
<%
response.write("Welkom op deze site het is vandaag :")
response.write Now( )
%>
</html> |
Response.write writes the output to HTML format. The user can not
see the ASP code in the source of the HTML.
The code above is very easy and could also be done without ASP, but when we are using
databases ASP can be very useful in generating HTML from the database.
The HTML is generated interactive from the database on the users request.
Everytime a user visits a site which uses ASP a unique Session can be
created. In the Session object variables can be set which are valid during the
session. Even if the user is switching between different pages.
An example of using a session is when a site is in two or more different languages.
On the default page of the site the user could choose a language which then is used during
the session to get the right information from the database. (read the right language) This
code shows how a session variable is set :
<%
Session("Land") = "English" 'Or for example
Session("Landl") = "E" The E can be used as field indication of
the language in the database
%> |
Parameters can be 'send' from one page to a other :
<%
ArticleId = Request.QueryString("Article")
%>
'The parameter article is sent by the calling page as follows
: ShowArtikel.asp?Article=101011
'The local variabele ArticleId gets the value of Article and can be used
inside the page |
2. Connecting to a database
Next code places the data from a database table into a HTML table :
<%
' Make the ODBC connection and open the database
Set Conn = Server.CreateObject ("ADODB.Connection")
Conn.Open "TestDatabase"
' Create a SQL-string and
execute to the ResultSet RS
SQL = "SELECT * FROM Artikelen WHERE Artikelnr > 1000"
Set RS = Conn.Execute (SQL)
' Create the html table
(just html)
response.write("<table border=1 width=100% bgcolor = white>")
RS.Movefirst
Do While not RS.EOF
response.write("<tr><td width='50%' align = 'left'>")
response.write(RS("Artikelnr"))
response.write("</td>")
response.write("<td width='50%' align = 'left'>")
response.write(RS("Omschrijving"))
response.write("</td>")
RS.Movenext
Loop
response.write("</table>")
response.write("<p>")
response.write("Above you see a table with two collumns Artikelnr and
omschrijving")
' Close the database connection
Conn.Close
Set RS = nothing%> |
3. Reading text files
With ASP you can read 'simple' textfiles and create a HTML page of it.
This example reads the file "Pagina.txt" line by line to HTML :
<%
Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
Set File = FileObject.OpenTextFile(Server.MapPath ("/TekstDir/Tekst") +
"Pagina.txt", 1, False, False) '1
= read only, 2 = write
Do While notFile.AtEndOfStream
Resonse.write(file.ReadLine + "<br>")
Loop
file.Close( )
Set FileObject = nothing
Set File = nothing
%> |
4. The benefits of ASP
The benefits of ASP are :
1. Decreasing the number of HTML files, because most are generated from databases
or textfiles.
2. Sites are easier to maintain
3. Sites can be maintained without explicate HTML knowledge. (Just fill the database
tables or textfiles.)
Top
|