Active Server Pages
- Active Server Pages (ASP) is a mix of HTML, scripts and ASP code that
enables you to build dynamic and database-driven web sites.
- Microsoft describes it as "a server - side scripting environment that you
can use to create and run dynamic, interactive, high - performance Web server
applications."
- While standard HTML is only a display language, ASP allows you to tailor
the information displayed on the page based on user interaction.
Uses for ASP
- You can use ASP for:
- Browser detection,
- Server variables such as what IP and referring page a site's user comes
from,
- Reading from and writing to a database or text file,
- Surveys and quizzes,
- Electronic commerce,
- Chat,
- Message boards,
- With add-on server components, you can email web form responses, upload
and download files, etc.
How Does ASP Work?
- Server-side ASP is interpreted by the server and the information is
returned to the browser to display.
- This makes it is cross - browser compatible.
- ASP can be written using Visual Basic, Scripting Edition (VBScript),
JScript or PerlScript. However, VBScript is the standard language used for
most ASP applications.
- Since ASP code is run by the Web server before it ever gets to your Web
browser if you open a ASP page on your local computer using a file URL
(file:///C:\data\myfile.asp) the ASP code will not run. ASP files must
be developed and tested using a Web server and browsed via the Internet.
Which Web Servers Run ASP?
- ASP runs on Microsoft IIS and Personal Web Server (PWS) on Win95,98 and
NT.
- ASP support is also available in O'Reilly's
Website Pro 2.0.
With the addition of commercial software from Chili!ASP or
Instant ASP from
Halcyon Software, ASP can also be run on a number of other servers and
platforms, including linux/unix.
Creating an ASP
- ASP Web pages generally begin and end with standard html. This sets up the
html page that the script will write to.
- These pages include VBScript, which is enclosed by
<% and
%>, to
write "Active" content to the page.
- Below is an example of a simple ASP Web Page that displays a Message and
today's Date.
<head>
<title>ASP Welcome Page</title>
</head>
<body>
<center>
<h2>
<%
Dim Now
' declare Now
Now = Date()
' Now's value = current date
Response.Write "Welcome to ASP in ISM 411<br>"
Response.Write(Now)
' end the response
Response.End
%>
</h2>
</center>
</body>
</html>
- In the above code
Date() returns today's date
(Just like in JavaScript)
- VBScript uses use
Response.Write to write HTML formatted text to the screen
- The final VBScript line ends the Response. If you don't have any more scripting on the
page, you can skip this. The
Response.End statement is often used
in using conditional statements (if then...else) when you want to go no further if
certain conditions are met.
Running Your Script
- To run ASP scripts, first save the script
,using the
.asp extension, to a directory within your wwwroot
of your Personal Web Server or to your directory on ism01.
- If your are running your scripts using PWS at home you should properly configure the directory in which you save your ASP
scripts.
- The directory must allow scripts access, and generally its a good idea
to turn off read access in that directory for security reasons.
- To check this,
fire up the PWS server administration screen and right-click on your scripts
directory.
- To run your script at home type:
http://localhost/testasp.asp,
in your Browser.
ASP and Databases
- One of the primary uses for ASP is to read from and write to databases.
- Active Server Pages communicate with databases via ADO (Active Data
Objects). ADO is an application programming interface (API) and part
of Microsoft's Data Access Components. It provides a number of objects that
are used in the query and manipulation of databases through ASP.
- ADO objects are used by first creating an instance of
(instantiating) the object you want on the server. This is done with simple
ASP code. Then, you simply use the methods, properties and collections of
that object to accomplish what you want.
- The basic component of the ADO is the Connection. Each connection
can then have Commands, Errors, and a RecordSet associated
with it (Ch. 25).
Reading From a Database
- We
will begin with a simple ASP script that reads addresses from a database and
displays them in a table. This example uses MS Access.
<%@ Language=VBScript %>
<% Option Explicit %>
<HTML>
<HEAD>
</HEAD>
<BODY>
|
-
The code begins with VBScript lines specifying that the page uses
VBScript, and that all variables need to be declared before use.
-
It then has 4 standard HTML tags that will be used to start the
page.
|
<% Dim objConnection, _
objRecordset, _
strSQL %>
|
- Create the variables to hold the connection, recordset and
SQL request for the database.
|
Creating the Connection
- To use a database with ASP you need to create an ADO database connection
object and use it to open the database. (ADO is a Microsoft data
access technology).
- To create an ADO database connection you create a server object which
stores the variables and other information needed to process the ASP.
- To do this you create the connection object of type
ADODB.Connection
and assign the connection object to the variable objConnection (or
other variable name)
<%
Set objConnection = _
Server.CreateObject("ADODB.Connection")
%>
- If you know the full pathname to the database you can open the connection
using the full path:
<%
objConnection.Open _
"DRIVER={Microsoft Access Driver (*.mdb)};" _
& "DBQ=C:\InetPub\wwwroot\Food.mdb;"
%>
- If you DON'T know the full pathname to the database, you can have the
server "map the path" using the line below.
- Note you only put one
objConnection.Open line in you
code.
- I used the line below because it still works when I move my
.asp
file from home to ism01.
<%
objConnection.Open _
"DRIVER={Microsoft Access Driver
(*.mdb)};" _
& "DBQ="
& Server.MapPath("Food.mdb") & ";"
%>
Creating the Command (SQL Query)
- Next, create the command string (a SQL query) you are going to send to the database to
get the data you want.
<% strSQL = "SELECT * FROM Suppliers"
%>
Getting the Data (RecordSet)
- Next, we open the recordset (that is get the data), pass it the command
object we created.
<% Set objRecordset = objConnection.Execute(strSQL)
%>
Displaying the Data
- We can read the data from the record set and write it to the HTML
file we are sending the browser.
- We have a recordset and the recordset pointer is positioned at the
first record in the record set.
- If the recordset has more than one record we need to loop through the
records to read them all (using a
Do While).
objRecordset("FieldName") returns the data in the
FieldName
field for the current record (objRecordset is the variable name you gave
your recordset).
objRecordset.MoveNext moves the
recordset to the next record.
<TABLE border=1>
<% Do While not objRecordset.EOF %>
<TR>
<TD><% = objRecordset("Name")
%></TD>
<TD><% = objRecordset("Address")
%></TD>
<TD><% = objRecordset("City")
%></TD>
<TD><% = objRecordset("Region")
%></TD>
<TD><% = objRecordset("Zip")
%></TD>
<TD><% = objRecordset("Country")
%></TD>
<TD><% = objRecordset("Phone")
%></TD>
</TR>
<% objRecordset.MoveNext
Loop %>
</TABLE>
Closing the Connection & Ending the File
- Once you have finished reading and displaying the records you should close
the connection, return resources to the server and send the final HTML tags
to the file
<% objConnection.Close
Set objRecordset = Nothing
Set objCommand = Nothing
Set objConnection = Nothing %>
</BODY>
</HTML>