APPENDIX A
Converting CGI/WinCGI Applications
into ASP Applications

Complete coverage of how to convert a CGI application (standard or WinCGI)
would require an entire book to itself. However, this appendix should provide a
starting point for your conversion.
The CGI Application
In this example, I will convert a simple CGI application to an ASP. I have written
this application in two forms: one version in Perl and one in Visual Basic. Each
version provides exactly the same functionality. It retrieves the user’s name and
programming language preference from a posted HTML form, then saves this
information into a Microsoft Access database using ActiveX Data Objects.
Figure A-1 shows the CGI application in a browser window.
Figure A-1: The HTML interface for our CGI application
366 Appendix A – Converting CGI/WinCGI Applications into ASP Applications
The CGI Application
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
The HTML code for the form in Figure A-1 is straightforward and is shown in
Example A-1.
I will use the same form with three separate values for the <FORM> tag’s ACTION
attribute, as shown in Table A-1.
Example A-1: HTML Source for the Sample CGI Application
<HTML>
<HEAD>
<TITLE>Sample Form</TITLE>
</HEAD>
<BODY bgcolor = #cccccc>
<form action="XXXXXXXX SEE BELOW XXXXXXXXXX" method="POST">
<center>
<h2>Welcome to the Programming Language Survey.</h2>
<h3>Please enter your name and your programming language preference
below.</h3>
<TABLE WIDTH = 40%>
<TR VALIGN = TOP>
<TD WIDTH = 40%>
<font face="Arial" size="+2">Name:</font>
</td>
<TD WIDTH = 60%>
<input type="Text" name="UsrName" size="20"
maxlength="80"><BR><BR>
</TD>
</tr>
<TR VALIGN = TOP>
<TD WIDTH = 40%>
<font face="Arial" size="+2">Language:</font>
</td>
<TD WIDTH = 60%>
<select name="ProgLang">
<option value="Perl">Perl
<option value="Python">Python
<option value="Visual Basic">Visual Basic
</select>
</TD>
</tr>
</TABLE>
<BR><BR>
<input type="Submit" name="Submit" value="Submit Form"
align="MIDDLE">
</form>
</center>
</BODY>
</HTML>
The Perl CGI Script 367
CGI/WinCGI
to
ASP
The Perl CGI Script
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
The Microsoft Access database consists of one table, LangPrefStorage, with two
text fields, Name and LangPref. The database’s data source name (DSN) in this
example is LangPref. All three server solutions will perform the same steps to
add the submitted information to the database:
1. Instantiate an ADO connection to the database.
2. Construct a SQL INSERT statement based on the name and language preference
submitted by the user (retrieved from the server).
3. Execute the SQL statement.
4. Return a “Thank You” message in an HTML page back to the user that will
allow him to return to the form.
The Perl CGI Script
Our first CGI script, which is shown in Example A-2, is written in Perl. I used
ActivePerl (from www.ActiveState.com) on a Windows NT Workstation 4.0
machine. The Perl 5 CGI script is very straightforward. Read the comments (those
lines starting with a “#” character) to understand the code line by line.
Table A-1: Values for the ACTION Attribute
Server
Method ACTION Parameter Value Description
CGI/Perl /cgi-shl/LangForm/Post_CGI.cgi CGI script written in Perl 5
CGI/VB /cgi-win/VB_CGI_32.exe Visual Basic executable written
using CGI32.BAS (from
O’Reilly’s CGI framework for
Visual Basic programmers)
ASP /LangPref/SavePref.asp Active Server Pages version
Example A-2: The Perl Version of the CGI Script
# Use the CGI and OLE perl modules.
use CGI qw(:standard);
use OLE;
# Instantiate an ADO Connection object and open the database.
$conn = CreateObject OLE "ADODB.Connection" || die "CreateObject:
$!”;
$conn->Open('LangPref');
# Retrieve the Name and Language Preference of the user.
$Name = param("UsrName");
$LangPref = param("ProgLang");
# Construct the SQL INSERT statement.
$sql = "INSERT INTO LangPrefStorage (Name, LangPref) VALUES (\
‘$Name\’, \'$LangPref\’)”;
# Execute the SQL INSERT statement and close the ADO connection.
$conn->Execute($sql);
368 Appendix A – Converting CGI/WinCGI Applications into ASP Applications
The Visual Basic CGI Application
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Once the user has entered her name and programming language preference and
clicked on the Submit button, this script will save the information to the database
and send the response shown in Figure A-2 back to the user.
The Perl code is straightforward. First the script imports code from the OLE and
CGI Perl modules. The Active Data Object (ADO) connection object is created and
initialized. Next, the user’s input is retrieved from the submitted HTTP request.
The information is inserted into the database. Finally, the response is written back
to the user.
The Visual Basic CGI Application
I wrote the second CGI application using Microsoft Visual Basic 6.0. I used
O’Reilly’s Windows CGI framework for Visual Basic Programmers (which comes
with O’Reilly’s WebSite Pro 2.0), which is defined by the CGI32.BAS code module.
This code module does much of the CGI work for you by retrieving all of the CGI
environment variables (among other things) from the temporary file created on the
server when the user submits the HTML form. For more information on the
$conn->Close();
# Print out the "Thank You" message in an HTML
# form to the user.
print header,start_html("Language Preference Storage"),h1("Thank
you, $Name.”);
print p("Click <A HREF = '/~wsdocs/langform/formsample.htm'>here</
a> to reset the form.<BR>”);
print end_html;
Figure A-2: The CGI reply
Example A-2: The Perl Version of the CGI Script (continued)
The Visual Basic CGI Application 369
CGI/WinCGI
to
ASP
The Visual Basic CGI Application
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
WinCGI specification or on O’Reilly’s CGI framework for Visual Basic programmers,
see the “Creating Dynamic Content” section of the documentation for
WebSite Pro 2.0. For more information on CGI variables (and their mapping to
ASP variables), see the second half of this appendix.
Example A-3 shows the Visual Basic code for our CGI application.
Example A-3: The Visual Basic Version of the CGI Script
' +-------------------------------------+
' | Force variable declarations. |
' +-------------------------------------+
Option Explicit
Sub Inter_Main()
' +-------------------------------------+
' | If a user of the web server machine |
' | inadvertently attempts to run |
' | this program as a standalone |
' | application, let them know it is a |
' | CGI app. |
' +-------------------------------------+
MsgBox "This is a Windows CGI program."
End Sub
Sub CGI_Main()
' +-------------------------------------+
' | Local variable declarations. |
' +-------------------------------------+
Dim strUserName As String
Dim strPrefLang As String
Dim adoCon As Object
Dim strSQL As String
' +-------------------------------------+
' | Create the ADO Connection object. |
' +-------------------------------------+
Set adoCon = CreateObject("ADODB.Connection")
adoCon.Open "LangPref"
' +-------------------------------------+
' | Retrieve the name and preference |
' | field values from the posted form. |
' +-------------------------------------+
strUserName = GetSmallField("UsrName")
strPrefLang = GetSmallField("ProgLang")
' +-------------------------------------+
' | Create the INSERT statement. |
' +-------------------------------------+
strSQL = "INSERT INTO LangPrefStorage (Name, "
strSQL = strSQL & "LangPref) VALUES ('"
strSQL = strSQL & strUserName & "', '"
370 Appendix A – Converting CGI/WinCGI Applications into ASP Applications
The Active Server Pages
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Even if you are not familiar with Visual Basic, this code is very simple. The
Option Explicit statement simply forces the developer to declare variables. The
Inter_Main subroutine is called any time a user mistakenly attempts to execute this
application in a standalone context (i.e., not as a CGI application). The next code
block retrieves the user’s submitted information from the temporary file created by
the web server (the real work is constructed in the GetSmallField function in the
CGI32.BAS module). Next, the information is stored into the database (for more on
the ActiveX Data Objects code, see Chapter 11, ActiveX Data Objects 1.5 ). Finally,
the CGI32.BAS subroutines for sending HTML back to the client are called to
return a response to the user.
The Active Server Pages
The Active Server Pages equivalent to the earlier CGI applications, which is shown
in Example A-4, is perhaps the simplest of the three applications. First I’ll show
you the code, then I’ll discuss it a bit.
strSQL = strSQL & strPrefLang & "')"
' +-------------------------------------+
' | Execute the SQL statement and close |
' | the ADO connection. |
' +-------------------------------------+
adoCon.Execute strSQL
adoCon.Close
' +-------------------------------------+
' | Send the HTTP request header and |
' | HTML page back to the client. |
' +-------------------------------------+
Send ("Content-type: text/html")
Send ("")
Send ("<HTML><HEAD><TITLE>")
Send ("Language Preference Storage</TITLE>")
Send ("</HEAD><BODY>")
Send ("<H1>Thank you, " & strUserName & ".</H1>")
Send ("Click <A HREF = _
'/~wsdocs/langform/formsample.htm'>here</a> to reset the form.<BR>
“)
Send ("</BODY></HTML>")
End Sub
Example A-4: The ASP Equivalent of the CGI Application
<HTML>
<HEAD>
<TITLE>Language Preference Storage</TITLE>
</HEAD>
<BODY>
<%
Example A-3: The Visual Basic Version of the CGI Script (continued)
The Active Server Pages 371
CGI/WinCGI
to
ASP
The Active Server Pages
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Example A-4 is written using VBScript only because all the code samples in this
book are written in VBScript (and because it is relatively easy to read). However,
as always with ASP, you can use any scripting language you like.
The ASP equivalent to our CGI application is very similar to the Visual Basic CGI
application, with the only significant difference coming in how we retrieve the
information from the HTML form. Instead of retrieving the information from a
temporary file created by the server (by calling the GetSmallField function from
' +-------------------------------------+
' | Local variable declarations. |
' +-------------------------------------+
Dim strUserName
Dim strPrefLang
Dim adoCon
Dim strSQL
' +-------------------------------------+
' | Create the ADO Connection object. |
' +-------------------------------------+
Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "LangPref"
' +-------------------------------------+
' | Retrieve the name and preference |
' | field values from the posted form. |
' +-------------------------------------+
strUserName = Request.Form("UsrName")
strPrefLang = Request.Form("ProgLang")
' +-------------------------------------+
' | Create the INSERT statement. |
' +-------------------------------------+
strSQL = "INSERT INTO LangPrefStorage (Name, "
strSQL = strSQL & "LangPref) VALUES ('"
strSQL = strSQL & strUserName & "', '"
strSQL = strSQL & strPrefLang & "')"
' +-------------------------------------+
' | Execute the SQL statement and close |
' | the ADO connection. |
' +-------------------------------------+
adoCon.Execute strSQL
adoCon.Close
%>
<H1>Thank you, <%=strUserName%>.</h1>
Click
<A HREF = '/~wsdocs/langform/formsample.htm'>
here</a> to reset the form.<BR>
</BODY>
</HTML>
Example A-4: The ASP Equivalent of the CGI Application (continued)
372 Appendix A – Converting CGI/WinCGI Applications into ASP Applications
Converting Environment Variables
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
the CGI32.BAS module), as I did in the VB application, I was able to retrieve the
information from the ASP Request object’s Form collection. The only other real
difference from a code perspective is that the final response display is written as
straight HTML in the ASP, whereas we were forced to rely on some functions in
the VB application.
Behind the scenes, there are some fundamental differences in how ASP retrieves
information. For more on this, see Chapter 1, Active Server Pages: An Introduction,
and Chapter 2, Active Server Pages: Server-Side Scripting.
Converting Environment Variables
CGI applications often make use of information residing in environment variables.
These variables contain information about the web server itself or about the HTTP
request sent by the client browser. In CGI written in Perl, these variables’ values
are retrieved from the %ENV associative array. In WinCGI written using O’Reilly’s
CGI framework for Visual Basic programmers, these values are retrieved from the
contents of global variables made available by the CGI.BAS or CGI32.BAS code
modules.
Active Server Pages applications also make use of these variables. In ASP, this
information is retrieved from the Request object’s ServerVariables collection.
Table A-2 will help you convert your CGI environment variables to ASP, while
Table A-3 will aid in converting WinCGI to ASP. Note that the general syntax
required to retrieve the ASP variable is:
varname = Request.ServerVariables("ASP_Variable")
Note also that there are other environment-type variables available to ASP applications
that are not available to CGI or WinCGI.
Table A-2: Converting CGI Environment Variables to ASP Variables
CGI Environment
Variable ASP Variable Description
AUTH_TYPE AUTH_TYPE Authentication method used
to validate user.
CONTENT_LENGTH CONTENT_LENGTH Length of the query data (in
bytes or number of characters)
passed through standard
input to the CGI
application.
CONTENT_TYPE CONTENT_TYPE The media type of the query
data (for example “text/
html”) sent to the CGI application.
DOCUMENT_ROOT APPL_PHYSICAL_PATH Directory from which web
pages are served. This directory
is the root parent for
your web site.
GATEWAY_INTERFACE GATEWAY_INTERFACE The version of CGI running
on your web server.
Converting Environment Variables 373
CGI/WinCGI
to
ASP
Converting Environment Variables
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
HTTP_ACCEPT HTTP_ACCEPT List of media types the
user’s browser can accept.
HTTP_COOKIE HTTP_COOKIE List of cookies on the client
machine defined for the
particular URL.
HTTP_FROM HTTP_FROM Email address of user
sending HTTP request
(rarely supported).
HTTP_REFERER HTTP_REFERER URL of document from
which user accesses CGI
application.
HTTP_USER_AGENT HTTP_USER-AGENT Browser used by user.
Note: You must use a
hyphen instead of an underscore
for this one. See
Chapter 6 and the discussion
of the ServerVariables
collection of the Request
object for more details.
PATH_INFO PATH_INFO Any extra path information
sent with the CGI request.
PATH_TRANSLATED PATH_TRANSLATED Physical path represented
by PATH_INFO variable.
QUERY_STRING QUERY_STRINGa Query passed to the CGI
application. This consists of
all character data following
the “?” at the end of the
URL.
REMOTE_ADDR REMOTE_ADDR Remote IP address of the
sender of the HTTP request.
This could be the address of
the user or a proxy server.
REMOTE_HOST REMOTE_HOST Remote hostname from
which the CGI request is
being sent.
REMOTE_IDENT NA Username of user making
the request.
REMOTE_USER LOGON_USER Authenticated name of user
sending the request to CGI
(if one exists).
REQUEST_METHOD REQUEST_METHOD Method used by user’s
browser in sending CGI
request (for example GET,
POST, etc.).
SCRIPT_NAME SCRIPT_NAME Virtual path of currently
executing CGI script.
Table A-2: Converting CGI Environment Variables to ASP Variables (continued)
CGI Environment
Variable ASP Variable Description
374 Appendix A – Converting CGI/WinCGI Applications into ASP Applications
Converting Environment Variables
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
SERVER_NAME SERVER_NAME Server’s hostname or IP
address.
SERVER_PORT SERVER_PORT Number of the port on the
host on which the server is
running.
SERVER_PROTOCOL SERVER_PROTOCOL Name/revision of the information
protocol by which
the CGI request was sent.
SERVER_SOFTWARE SERVER_SOFTWARE Name/version information
for the web server software.
a A better way to manipulate the information in the QueryString HTTP request information is
to use the Request object’s QueryString collection. See Chapter 6, Request Object, for more details.
Table A-3: Converting WinCGI Environment Variables to ASP Variables
WinCGI Environment
Variable ASP Variable Description
CGI_AcceptTypes HTTP_ACCEPT List of media types the
user’s browser can accept.
CGI_AuthPass NA Password of authenticated
user, if supported on the
web server.
CGI_AuthRealm NA Realm or domain of
authorized user, if
supported.
CGI_AuthType AUTH_TYPE Authentication method
used to validate user.
CGI_AuthUser LOGON_USER Authenticated name of
user sending request to
CGI (if one exists).
CGI_ContentFile NA Full pathname of the file
created by the web server
that contains any attached
data (i.e., any POSTed
information).
CGI_ContentLength CONTENT_LENGTH Total length in bytes or
number of characters of
the user’s CGI request.
CGI_ContentType CONTENT_TYPE MIME type of the request
data POSTed.
CGI_DebugMode NA CGI tracing flag from the
web server.
CGI_ExecutablePath SCRIPT_NAME Path of CGI application
being executed.
Table A-2: Converting CGI Environment Variables to ASP Variables (continued)
CGI Environment
Variable ASP Variable Description
Converting Environment Variables 375
CGI/WinCGI
to
ASP
Converting Environment Variables
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
CGI_ExtraHeaders NAa Any extra HTTP headers
sent by the browser.
CGI_FormTuples NAb Name=Value pairs sent in
the form data of the CGI
request, if any exist.
CGI_From HTTP_FROM Email address of user
sending HTTP request
(rarely supported).
CGI_GMTOffset NA Number of seconds +/-
from GMT.
CGI_HugeTuples NAb Large Name=Value pairs
in the form data sent with
the CGI request.
CGI_LogicalPath SCRIPT_NAME Logical path or extra path
information for the CGI
application being
executed.
CGI_NumAcceptTypes NAc Number of accepted
media types of the user’s
browser.
CGI_NumExtraHeaders NAc Number of extra HTTP
headers sent by the
browser.
CGI_NumFormTuples NAc Number of Name=Value
pairs submitted through a
form with the CGI request
sent by the user.
CGI_NumHugeTuples NAc Number of large
Name=Value pairs in the
form data sent with the
CGI request.
CGI_OutputFile NA Full pathname of the file
in which the web server
expects to find the results
of the CGI application’s
execution.
CGI_PhysicalPath NAd Physical path represented
by the logical path.
CGI_QueryString QUERY_STRING1 Query passed to the CGI
application. This consists
of all character data
following the “?” at the
end of the URL.
CGI_Referer HTTP_REFERER URL of document from
which user accesses CGI
application.
Table A-3: Converting WinCGI Environment Variables to ASP Variables
WinCGI Environment
Variable ASP Variable Description
376 Appendix A – Converting CGI/WinCGI Applications into ASP Applications
Converting Environment Variables
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
CGI_RemoteAddr REMOTE_ADDR Remote IP address of the
sender of the HTTP
request. This could be the
address of the user or a
proxy server.
CGI_RemoteHost REMOTE_HOST Remote hostname from
which the CGI request is
being sent.
CGI_RequestMethod REQUEST_METHOD Method used by user’s
browser in sending CGI
request (for example GET,
POST, etc).
CGI_RequestProtocol SERVER_PROTOCOL Name and version of the
request protocol used in
the query to the CGI
application.
CGI_ServerAdmin NAe Email address of the web
server admin, if available.
CGI_ServerName SERVER_NAME Server’s hostname or IP
address.
CGI_ServerPort SERVER_PORT Number of the port on
the host on which the
server is running.
CGI_ServerSoftware SERVER_SOFTWARE Name and version of the
web server software.
CGI_Version GATEWAY_INTERFACE Version of the CGI
running on the web
server.
a You can retrieve all HTTP headers sent by the user with ALL_HTTP.
b You can retrieve this information from the Form collection of the Request object. See
Chapter 6 for more details.
c You can retrieve this information programmatically.
d You can derive this using the Server object’s MapPath method in conjunction with the Request.
ServerVariables (“SCRIPT_NAME”) variable. See Chapter 8, Server Object, for more information
on the MapPath method.
e You could use the MyInfo component to define a similar property. See Chapter 19, MyInfo
Component, for more details.
Table A-3: Converting WinCGI Environment Variables to ASP Variables
WinCGI Environment
Variable ASP Variable Description
377
Multiplatform ASP
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
ariables using the current
' values of the corresponding counters in the Counters
' object (instantiated elsewhere).
intDoom = gobjOptionCounter.Get("FavGameCounter_Doom")
intQuake = gobjOptionCounter.Get("FavGameCounter_Quake")
intQuake2 = gobjOptionCounter.Get("FavGameCounter_Quake2")
' Display the current vote tallies for favorite game.
%>
Here are the current vote counts for favorite game:<BR>
<TABLE WIDTH = 50%>
<TR>
<TD WIDTH = 50%>
Doom
<TD>
<TD WIDTH = 50%>
<%= intDoom %>
<TD>
</TR>
<TR>
Increment 313
Counters
Increment
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
<TD WIDTH = 50%>
Quake
<TD>
<TD WIDTH = 50%>
<%= intQuake %>
<TD>
</TR>
<TR>
<TD WIDTH = 50%>
Quake 2
<TD>
<TD WIDTH = 50%>
<%= intQuake2 %>
<TD>
</TR>
</TABLE>
</BODY></HTML>
Notes
The value of a counter is limited to the range of an integer. Note that the number
of counters held in the Counters component has little effect on the memory it
holds on the web server, since the values of its counters are written to the hard
drive.
Increment
objCounter.Increment(strCounterName)
Increments a counter in the Counters component. If you attempt to increment a
counter that does not yet exist, the counter is created and its value is set to 1. The
new value of the counter is returned.
Parameters
strCounterName
A string that represents the name of the counter variable you wish to manipulate.
This name can contain any Unicode character.
Example
The following example assumes a Counters object (gobjOptionCounter) has
been instantiated elsewhere (see “Instantiating the Counters Component” earlier in
this chapter) and demonstrates the use of the Increment method.
<HTML>
<HEAD>
<TITLE>Favorite Games</TITLE>
<BODY>
<%
' The following line of code increments the
' FavGameCounter_Doom counter in the gobjOptionCounter
' object and returns the new value of the counter.
314 Chapter 17 – Counters Component
Remove
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
' Note that if FavGameCounter_Doom does not yet exist
' in the gobjOptionCounter object, the returned value
' is 1.
%>
You are user number
<%= gobjOptionCounter.Increment("FavGameCounter_Doom") %>
to vote for Doom as your favorite game.<BR>
%>
...[additional HTML and code]
Remove
objCounter.Remove(strCounterName)
Removes a counter from the Counters component and deletes its entry from the
Counters.TXT file. This method has no return value.
Parameters
strCounterName
A string that represents the name of the counter variable you wish to remove.
This name can contain any Unicode character.
Example
The following example demonstrates the use of the Remove method of the
Counters object. This example assumes a Counters object (gobjOptionCounter)
has been instantiated elsewhere.
<%
' The following code removes the FavGameCounter_Wolf3D
' counter from the gobjOptionCounter object.
gobjOptionCounter.Remove("FavGameCounter_Wolf3D")
%>
Notes
See the explanation of the Get method earlier in this chapter.
Set
objCounter.Set(strCounterName, intCounterValue)
The Set method allows you to create a counter in the Counters component and
add its entry to the Counters.TXT file. The new counter’s value is returned.
Parameters
strCounterName
A string that represents the name of the counter variable you wish to manipulate.
This name can contain any Unicode character.
Set 315
Counters
Set
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
intCounterValue
An integer that represents the new value of the counter variable you wish to
set.
Example
The following example demonstrates the use of the Set method. This example
assumes a Counters object (gobjOptionCounter) has been instantiated
elsewhere.
<%
' The following code sets the value of the
' FavGameCounter_Unreal counter to an arbitrary number
' (high) to inflate its perceived popularity. If it does
' not already exist, it is created and initialized to the
' value 987.
gobjoptionCounter.Set("FavGameCounter_Unreal", 987)
%>
316
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
ension local variables.
\par Dim objNextLink
\par Dim strNextDesc
\par ' Create an instance of the NextLink object.
\par Set objNextLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve a description text for the next item in the
\par ' Content Linking list file.
\par strNextDesc = _
\par objNextLink.GetNextDescription("/MyContentLinkList.txt")
\par ' Display the next description to the client.
\par %>
\par <%= strNextDesc%>
\par <%
\par ' Free the memory consumed by the NextLink object.
\par Set objNextLink = Nothing
\par %>
\par ...[additional HTML and code]
\par Notes
\par If the current document is not listed in the Content Linking list file, \par the description
\par text for the last item in the list file is returned by default. If the \par current item is the
\par last item in the list, calling GetNextDescription returns an empty string \par (“”).
\par In addition to the previous example, see the full example at the end of \par this
\par chapter.
\par GetNextURL 293
\par Content Linking
\par GetNextURL
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par GetNextURL
\par objNextLink.GetNextURL(strContentLinkList)
\par Returns a string containing the URL entry of the next document listed \par in the
\par Content Linking list.
\par Parameters
\par strContentLinkList
\par A string value representing the virtual or relative pathname and filename \par of
\par your Content Linking file. You cannot use physical paths or absolute URLs
\par (those beginning with HTTP://, //, or \\\\) for this parameter.
\par Example
\par <%
\par ' Dimension local variables.
\par Dim objNextLink
\par Dim strNextDesc
\par Dim strNextURL
\par ' Create an instance of the NextLink object.
\par Set objNextLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve a description text for the next item in the
\par ' Content Linking list file.
\par strNextDesc = _
\par objNextLink.GetNextDescription("/MyContentLinkList.txt")
\par ' Retrieve a URL for the next item in the Content Linking
\par ' list file.
\par strNextURL = _
\par objNextLink.GetNextURL("/MyContentLinkList.txt")
\par ' Use strNextURL to create a link to the item whose
\par ' description you retrieved using GetNextDescription.
\par %>
\par <A HREF = "<%= strNextURL %>"><%= strNextDesc%></A>
\par <%
\par ' Free the memory consumed by the NextLink object.
\par Set objNextLink = Nothing
\par %>
\par ...[additional HTML and code]
\par Notes
\par If the current document is not listed in the Content Linking list file, \par the URL text
\par for the last item in the list file is returned by default.
\par Using GetNextURL with a Content Linking file, you do not have to change \par the
\par code within your HTML to update a “NEXT PAGE” hyperlink, for \par example. You
\par 294 Chapter 15 – Content Linking Component
\par GetNthDescription
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par have only to change the Content Linking list, and the component will automatically
\par update this link for you.
\par In addition to the previous example, see the full example at the end of \par this
\par chapter.
\par GetNthDescription
\par objNextLink.GetNthDescription(strContentLinkList, intItemIndex)
\par Returns a string containing the description for the item in the Nth position \par (on the
\par Nth line) in the Content Linking list.
\par Parameters
\par strContentLinkList
\par A string value representing the virtual or relative pathname and filename \par of
\par your Content Linking file. You cannot use physical paths or absolute URLs
\par (those beginning with HTTP://, //, or \\\\) for this parameter.
\par intItemIndex
\par An integer indicating the index of the item whose description you wish \par to
\par retrieve from the Content Linking list.
\par Example
\par <%
\par ' Dimension local variables.
\par Dim objNextLink
\par Dim intTotalCount
\par Dim intCounter
\par ' Instantiate a NextLink object for this script.
\par Set objNextLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve a total count of items in the Content Linking file.
\par intTotalCount = _
\par objNextLink.GetListCount("/MyContentList.TXT")
\par ' Iterate through all the items in the Content Linking
\par ' list and generate an entry in an ordered list.
\par %>
\par <OL>
\par <UL>
\par <%
\par For intCounter = 1 to intTotalCount
\par %>
\par <LI>
\par <!-- Create a hyperlink to the URL for the current item -->
\par <!-- in the list. -->
\par <a href "<%=
\par objNextLink.GetNthURL("/MyContentList.TXT", _
\par intCounter)%>">
\par <!-- Retrieve the text description for that URL. -->
\par GetNthURL 295
\par Content Linking
\par GetNthURL
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par objNextLink.GetNthDescription("/MyContentList.TXT", _
\par intCounter)
\par </LI>
\par <%
\par Next
\par %>
\par </UL>
\par </OL>
\par <%
\par ' Release the memory held for the NextLink object.
\par Set objNextLink = Nothing
\par %>
\par Notes
\par If there is not an item in the position sent in the intItemIndex parameter, \par an
\par error results. To prevent this, you can compare the value to be supplied \par as the
\par intItemIndex argument with the value returned by a call to the GetListCount
\par method.
\par In addition to the previous example, see the full example at the end of \par this
\par chapter.
\par GetNthURL
\par objNextLink.GetNthURL(strContentLinkList, intItemIndex)
\par Returns a string containing the URL for the item in the Nth position (on \par the Nth
\par line) in the Content Linking list.
\par Parameters
\par strContentLinkList
\par A string value representing the virtual or relative pathname and filename \par of
\par your Content Linking file. You cannot use physical paths or absolute URLs
\par (those beginning with HTTP://, //, or \\\\) for this parameter.
\par intItemIndex
\par The index of the item in the Content Linking list whose URL you wish to
\par retrieve. This is an integer parameter.
\par Example
\par <%
\par ' Dimension local variables.
\par Dim objNextLink
\par Dim intTotalCount
\par Dim intCounter
\par ' Instantiate a NextLink object for this script.
\par Set objNextLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve a total count of items in the Content
\par ' Linking file.
\par intTotalCount = _
\par 296 Chapter 15 – Content Linking Component
\par GetPreviousDescription
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par objNextLink.GetListCount("/MyContentList.TXT")
\par ' Iterate through all the items in the Content Linking
\par ' list and generate an entry in an ordered list.
\par %>
\par <OL>
\par <UL>
\par <%
\par For intCounter = 1 to intTotalCount
\par %>
\par <LI>
\par <!-- Create a hyperlink to the URL for the current -->
\par <!-- item in the list. -->
\par <a href "<%=
\par objNextLink.GetNthURL("/MyContentList.TXT", _
\par intCounter)%>">
\par <!-- Retrieve the text description for that URL. -->
\par objNextLink.GetNthDescription("/MyContentList.TXT", _
\par intCounter)
\par </LI>
\par <%
\par Next
\par %>
\par </UL>
\par </OL>
\par <%
\par ' Release the memory held for the NextLink object.
\par Set objNextLink = Nothing
\par %>
\par Notes
\par If there is not an item in the position indicated by the intItemIndex \par parameter,
\par an error results. To prevent this, you can compare the value to be supplied \par as the
\par intItemIndex argument with the value returned by a call to the GetListCount
\par method.
\par In addition to the previous example, see the full example at the end of \par this
\par chapter.
\par GetPreviousDescription
\par objNextLink.GetPreviousDescription(strContentLinkList )
\par Returns an ASCII string containing the description of the previous document \par listed
\par in the Content Linking list.
\par GetPreviousURL 297
\par Content Linking
\par GetPreviousURL
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par Parameters
\par strContentLinkList
\par A string value representing the virtual or relative pathname and filename \par of
\par your Content Linking file. You cannot use physical paths or absolute URLs
\par (those beginning with HTTP://, //, or \\\\) for this parameter.
\par Example
\par <HTML>
\par <HEAD>
\par <TITLE>Document List</TITLE>
\par <BODY>
\par <%
\par ' Dimension local variables.
\par Dim objNextLink
\par Dim strPrevDesc
\par ' Create an instance of the NextLink object.
\par Set objNextLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve a description text for the previous item in
\par ' the Content Linking list file.
\par strPrevDesc = _
\par objNextLink.GetPreviousDescription("/MyContentLinkList.txt")
\par ' Display the previous description to the client.
\par %>
\par <%= strPrevDesc%>
\par <%
\par ' Free the memory consumed by the NextLink object.
\par Set objNextLink = Nothing
\par %>
\par ...[additional HTML and code]
\par Notes
\par If the current page cannot be found in the Content Linking list file, \par the description
\par text for the first item in the list file is returned by default. If the \par current item is the
\par first item in the list, calling GetPreviousDescription will return an \par empty string (“”).
\par In addition to the previous example, see the full example at the end of \par this
\par chapter.
\par GetPreviousURL
\par objNextLink.GetPreviousURL(strContentLinkList)
\par Returns a string containing the URL entry of the previous document listed \par in the
\par Content Linking list.
\par 298 Chapter 15 – Content Linking Component
\par GetPreviousURL
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par Parameters
\par strContentLinkList
\par A string value representing the virtual or relative pathname and filename \par of
\par your Content Linking file. You cannot use physical paths or absolute URLs
\par (those beginning with HTTP://, //, or \\\\) for this parameter.
\par Example
\par <%
\par ' Dimension local variables.
\par Dim objNextLink
\par Dim strPrevDesc
\par Dim strPrevURL
\par ' Create an instance of the NextLink object.
\par Set objNextLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve a description text for the previous item in
\par ' the Content Linking list file.
\par strPrevDesc = _
\par objNextLink.GetPreviousDescription("/MyContentLinkList.txt")
\par ' Retrieve a URL for the previous item in the Content
\par ' Linking list file.
\par strPrevURL = _
\par objNextLink.GetPreviousURL("/MyContentLinkList.txt")
\par ' Use strNextURL to create a link to the item whose
\par ' description you retrieved using GetPreviousDescription.
\par %>
\par <A HREF = "<%= strPrevURL %>"><%= strPrevDesc%></A>
\par <%
\par ' Free the memory consumed by the NextLink object.
\par Set objNextLink = Nothing
\par %>
\par ...[additional HTML and code]
\par Notes
\par If the current page cannot be found in the Content Linking list file, \par the URL text
\par for the first item in the list file is returned by default.
\par Using GetPreviousURL with a Content Linking file, you do not have to change \par the
\par code within your HTML to update a “PREVIOUS PAGE” hyperlink, \par for example.
\par You only have to change the Content Linking list, and the component will \par automatically
\par update this link for you.
\par In addition to the previous example, see the full example at the end of \par this
\par chapter.
\par Content Linking Component Example 299
\par Content Linking
\par Content Linking Component Example
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par Content Linking Component Example
\par The following example code demonstrates a complete Content Linking component
\par example in one place to illustrate the overall mechanism of the Content
\par Linking component and its accessory content list file.
\par The scenario is simple. The following set of scripts demonstrates the \par dynamic
\par construction of the first few pages of an online book introducing programming.
\par There are five content files (Content1.ASP through Content5.ASP). For \par each file,
\par you want to provide your users with an indicator of current page number \par (out of
\par the total number of pages), a previous-page link, and a next-page link. \par You know
\par the content files will change and pages will be inserted and removed often. \par This is
\par a good example of a programming problem in which the Content Linking component
\par can help.
\par The following script is the HTML version of the fourth page in our online \par book:
\par <HTML>
\par <HEAD><TITLE>Introduction to Programming: Lesson 4 Looping</
\par TITLE></HEAD>
\par <BODY>
\par Welcome to the Introduction to Programming, Lesson 4:
\par Looping.<BR>
\par [TEXT ABOUT LOOPING AND LOOP STRUCTURES]
\par <!-- Begin navigation section construction -->
\par <HR>
\par You are currently viewing page # 4 of 5.<BR>
\par Use the following links to navigate:<BR>
\par <A HREF "Content3.asp">Previous: Lesson 3 Variables</A><BR>
\par <A HREF "Content1.asp">Home: Lesson 1 Introduction</A><BR>
\par <A HREF "Content5.asp">Next: Lesson 5 Pointers</A><BR>
\par <!-- End navigation section construction -->
\par </BODY>
\par </HTML>
\par This HTML page could be easily created by hand and kept up-to-date manually
\par when pages are inserted and removed. However, you can see that with many
\par pages, such upkeep would be tedious, at best. For example, suppose we \par had to
\par insert a page (Lesson 3a: Advanced Variables) between lessons 3 and 4. \par To do this
\par manually, everything in bold in the previous example would have to be \par changed
\par by hand. If we removed the current home page and added a new one (with \par a
\par different name and description), we would have to make even more changes.
\par The Content Linking component can help us here. We start by creating a \par Content
\par Linking list, whose filename is ONLINE_CONTENT_LIST.TXT:
\par Content1.asp Lesson 1 Background
\par Content2.asp Lesson 2 Code Style
\par Content3.asp Lesson 3 Variables
\par Content4.asp Lesson 4 Looping
\par Content5.asp Lesson 5 Pointers
\par 300 Chapter 15 – Content Linking Component
\par Content Linking Component Example
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par The file contains one line for each of our five content pages. Each line \par consists of
\par a filename and a file description, separated by a Tab character. We can \par now add
\par the following code to the navigation section of each page in our online \par book:
\par <!-- Begin navigation section construction -->
\par <HR>
\par <%
\par Dim objContentLink
\par Set objContentLink = Server.CreateObject("MSWC.NextLink")
\par ' Retrieve the index of the current page.
\par intCurrentPageNumber = _
\par objContentLink.GetListIndex("ONLINE_CONTENT_LIST.TXT")
\par ' Retrieve the total number of pages.
\par intTotalPageCount = _
\par objContentLink.GetListCount("ONLINE_CONTENT_LIST.TXT")
\par ' Retrieve the URL for the first page in the series.
\par strHomeURL = _
\par objContentLink.GetNthURL("ONLINE_CONTENT_LIST.TXT", 1)
\par ' Retrieve the description for the first page in the series
\par strHomeDesc = objContentLink.GetNthDescription( _
\par "ONLINE_CONTENT_LIST.TXT", 1)
\par ' If the current page index is greater than 1 (i.e., it
\par ' is after the home page), then retrieve information
\par ' about the previous page.
\par If intCurrentPageNumber > 1 Then
\par ' Retrieve the description for the first page in the
\par ' series.
\par strPrevURL = objContentLink.GetPreviousURL( _
\par "ONLINE_CONTENT_LIST.TXT", 1)
\par ' Retrieve the description for the previous page in
\par ' the series.
\par strPrevDesc = objContentLink.GetPreviousDescription( _
\par "ONLINE_CONTENT_LIST.TXT", 1)
\par End If
\par ' If the current page index is less than the total page
\par ' count (i.e., it is before the last page), then retrieve
\par ' information about the next page.
\par If intCurrentPageNumber < intTotalPageCount Then
\par ' Retrieve the URL for the previous page in the series.
\par strNextURL = objContentLink.GetNextURL( _
\par "ONLINE_CONTENT_LIST.TXT", 1)
\par ' Retrieve the description for the next page in the series.
\par strNextDesc = objContentLink.GetNextDescription( _
\par "ONLINE_CONTENT_LIST.TXT", 1)
\par Content Linking Component Example 301
\par Content Linking
\par Content Linking Component Example
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par End If
\par ' Now use the preceding information to construct the
\par ' navigation section of the current page.
\par %>
\par You are currently viewing page #
\par <%=intCurrentPageNumber%> of
\par <%=intTotalPageCount%>.<BR>
\par Use the following links to navigate:<BR>
\par <%If intCurrentPageNumber > 1 Then%>
\par <A HREF "<%=strPrevURL%>">Previous:
\par <%=strPrevDesc%></A><BR>
\par <%End If%>
\par <A HREF "<%=strHomeURL%>">Home: <%=strHomeDesc%></A><BR>
\par <%If intCurrentPageNumber < intTotalPageCount%>
\par <A HREF "<%=strNextURL%>">Next: <%=strNextDesc%></A><BR>
\par <%End If%>
\par <!-- End navigation section construction -->
\par If we were to replace the following code in bold:
\par <HTML>
\par <HEAD><TITLE>Introduction to Programming: Lesson 4 Looping</
\par TITLE></HEAD>
\par <BODY>
\par Welcome to the Introduction to Programming, Lesson 4:
\par Looping.<BR>
\par [TEXT ABOUT LOOPING AND LOOP STRUCTURES]
\par <!-- Begin navigation section construction -->
\par <HR>
\par You are currently viewing page # 4 of 5.<BR>
\par Use the following links to navigate:<BR>
\par <A HREF "Content3.asp">Previous: Lesson 3 Variables</A><BR>
\par <A HREF "Content1.asp">Home: Lesson 1 Introduction</A><BR>
\par <A HREF "Content5.asp">Next: Lesson 5 Pointers</A><BR>
\par <!-- End navigation section construction -->
\par </BODY>
\par </HTML>
\par with the Content Linking component code segment preceding it, the result \par would
\par be a navigation links section that stays current with the Content Linking \par list. All
\par you would have to do to update the links is to update the Content Linking \par list file.
\par We could even go one step further and save the previous code as an include \par file
\par (called NavConstruct.INC) and include it anywhere in the content pages \par for our
\par online book:
\par <HTML>
\par <HEAD><TITLE>Introduction to Programming: Lesson 4 Looping
\par </TITLE></HEAD>
\par 302 Chapter 15 – Content Linking Component
\par Content Linking Component Example
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par <BODY>
\par Welcome to the Introduction to Programming, Lesson 4:
\par Looping.<BR>
\par [TEXT ABOUT LOOPING AND LOOP STRUCTURES]
\par <!-- #INCLUDE FILE = NavConstruct.INC-->
\par </BODY>
\par Now suppose we must add a page between pages 3 and 4. All that we must \par do,
\par after creating the page itself (and including our NavConstruct.INC include \par file), is
\par to update the ONLINE_CONTENT_LIST.TXT Content Linking list file:
\par Content1.asp Lesson 1 Background
\par Content2.asp Lesson 2 Code Style
\par Content3.asp Lesson 3 Variables
\par Content3a.asp Lession 3a Advanced Variables
\par Content4.asp Lesson 4 Looping
\par Content5.asp Lesson 5 Pointers
\par All the links constructed using the Content inking list component are \par updated
\par upon the code’s execution and the links stay correct. You can avoid \par the task of
\par going into each affected file and updating hardcoded links. It is all \par done for you.
\par 303
\par Content Rotator
\par ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
\par Copyright © 2000 O’Reilly & Associates, Inc. All rights \par reserved.
\par } pt r Capabilities
\par component. You use the Browser Capabilities component to create a
\par BrowserType object. When you create a BrowserType object, the web server
\par retrieves the HTTP USER AGENT header sent by the client. Using this information,
\par the BrowserType object compares the information from this header to entries \par in a
\par special file (BrowsCap.ini ). If a match for the current client’s \par browser is found, the
\par BrowserType object determines all the properties for the specific browser. \par Your
\par scripts can then reference the properties of the BrowserType object to \par determine
\par the capabilities of the user’s browser. The following steps summarize s haw i