Got a database? ASPs take it to the Web

Why are many webmasters turning to Microsoft Internet Information Server? For dynamic
cross-platform Web page delivery through Windows NT's Active Server Page technology.


Like Common Gateway Interface programs, ASPs use server-side scripts to make pages
interactive. Unlike most CGIs, ASPs embed the scripting code within Hypertext Markup
Language code.


When a user requests a page called info.asp, for example, a script embedded in info.asp
executes before it leaves the server. The user will see only the surrounding HTML code.


The scripts are server-side, so it makes no difference which browser the user has. The
code simply executes and passes standard HTML code to the browser.


Developers who write ASP code within HTML code do their work in one file, making it
easier to plan and visualize the final screen.


ASP script is almost by default Visual Basic Script, although you can write the scripts
in Microsoft JavaScript, Java or even C++.


ASPs work best with Internet Information Server 3.0 or 4.0. They also run under
Microsoft's Personal Web Server and Mac OS' Personal Web Sharing. But remember that a
personal Web server, though fine for developing or testing ASPs, is not robust enough to
serve pages to a large audience.


To learn ASP skills, you must have a strong grasp of HTML. It helps to know a scripting
language, too. Although you can use other languages to program ASPs, I recommend Microsoft
VBScript. Microsoft products, not coincidentally, tend to work best with other Microsoft
products.


If you have a good understanding of JavaScript or another scripting language, VBScript
will not be hard to learn. An excellent VBScript tutorial and reference guide appear at http://www.microsoft.com/vbscript/.


Several Microsoft applications can create ASPs. For example, Access 97 can publish live
databases online quickly and easily. It is short on options for customization, though.


Microsoft FrontPage also can create ASP code. But I have heard that FrontPage sometimes
clobbers the code enough to render it useless. If you use ASPs with FrontPage, take the
precaution of inserting the ASP code as a FrontPage HTML component.


Microsoft's Visual InterDev is the most professional tool for creating Web
applications. Like Visual Basic, InterDev sets up a work space to which a programmer adds
objects and code for Web applications.


InterDev works best under Windows NT 4.0. It generates code that automates the work of
building a cutting edge site. You probably will need to fall back on your trusty text
editor for the final tweaks, however. HomeSite from Allaire Corp. of Cambridge, Mass., is
my editor of choice. It generates no code, but it does recognize ASP code and has buttons
for adding ASP delimiters or server variables.


ASP follows Microsoft's object model, familiar to Visual Basic programmers. Several
built-in objects have methods and properties for adding functions to a page.


The Application object manages states and information for any Web application created
with ASP. Take out the Web element, and a Web application is like any other application.
The most common Web applications so far are online commerce software and chat engines.


The Session object manages information about user Web sessions. The object sends and
receives cookies that track time, preferences and progress through a site. Information
given when a user first enters the site is readily available to any other page on the
site.


The Response object sends information to the client or user. The Server object adds
functions to ASPs. ActiveX components created using Visual Basic or C++ expand the
functions.


Several components are part of the package. The most useful are a browser capabilities
component, a file-access component and a database-access component. Many third-party
components are available, too.


Here's how ASP's ActiveX data object lets you integrate a database with a Web page: A
Structured Query Language command passes from the ASP code through the ActiveX Data Object
to the database. The database retrieves the information requested by the SQL command and
passes it back to the Active Server Page for display in HTML form.


But to start with, the server must know which database it is going to use. This is
where the Open Database Connectivity Data Source Control Panel comes in.


On the control panel, the user gives the database a name, which will appear in the ASP
scripts. Once the server knows about the database, a connection must be established
between the Active Server Page and the database. The code appears at the beginning of an
ASP document before the HTML:


<%
Set DbConn=Server.CreateObject
("ADODB.Connection")
DbConn.Open ("DSN=Contacts;")
%>


The expression Server.CreateObject ("ADODB.Connection") tells the Server
object to make a connection using the ActiveX Data Object component. This results in an
object called DbConn that will be used only for this page. The object opens a connection
with a database referred to above as Contacts--the name given to it in the ODBC Data
Source Control Panel.


Once the connection is established, we can do something with it:


<%
SQL="SELECT * FROM Customers
ORDER BY State"
Set RS=DbConn.execute(SQL)
%>


The SQL statement returns all the data in the Customers table of the Contacts database.
The ORDER BY statement causes the data to be sorted by the State field.


The next line passes the SQL statement to the ActiveX Data Object using the DbConn
object. This retrieves data from the database and creates a record set called RS, a
temporary local copy of the retrieved information.


Now we must output the record set in the body of the HTML document:


<Body>
<%
Do While NOT RS.EOF
%>
<li><b><%=RS ("Name") %></b><br>
<%=RS ("Address") %><br>
<%=RS ("City") %>, <%=RS ("State")
%> &nbsp;<%=RS("Zip") %>
<hr width="180" align="left">
<%
RS.Movenext
loop
%>
</BODY>


The third line is VBScript that begins a while loop. The loop tells the server to do
certain things until it encounters the end of file of the record set. <%= is shorthand
for Response.write, so the sixth line writes out the value of the Name field in the record
set.


Because there are <b> tags around the ASP code, the result of that code will
appear as bold text. The lines that follow do the same for other fields in the record set.
A horizontal rule separates the records.


The RS.Movenext statement tells the code to move to the next record in the record set.
The loop statement starts the whole process over again with the new record.


Once the loop is done and any other ASP code on the page executes, the results go back
to the user who requested the data. If the user looks at the source code of the page, only
HTML appears.


This tutorial only touches on what you can do with Active Server Pages. Two good books
on the subject are Using Active Server Pages from Que Publishing and Professional
Active Server Page Programming
from Wrox Press.


The first book, good for beginners, devotes many chapters to working with and
integrating databases. The second book, written for programmers, covers all the ground.


Many Web sites offer ASP tips, tutorials and articles.


For starters, take a look at the following Web sites: http://www.activeserverpages.com/, http://www.microsoft.com/workshop/server/default.asp,
http://www.serverobjects.comand http://www.activexserver.com.


Jason Hart is a webmaster and analyst at Advanced Management Technology Inc. in
Washington.


inside gcn

  • Phishing

    Phishing is still a big problem, but users can help shrink it

Reader Comments

Please post your comments here. Comments are moderated, so they may not appear immediately after submitting. We will not post comments that we consider abusive or off-topic.

Please type the letters/numbers you see above