ASP 101 - Active Server Pages 101 - Web06
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
VBScript Classes: Part 1 of N
Migrating to ASP.NET
Getting Scripts to Run on a Schedule

QUICK TIP:
Navigation Stars *
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


Make Your Content Portable to External Sites

(aka, "How to build a low-budget Web service with Classic ASP")

by Jason Salas

A quick and effective (and low cost) solution for maximizing your site's exposure using Classic ASP

Technologies used:

  • Active Server Pages 3.0 (Classic ASP)
    • Data access
    • FileSystemObject object
  • Server-side VBScript
  • Client-side JavaScript

Level of difficulty:

  • Beginner/Intermediate

Summary

With the increasing interest in XML Web Services platforms from the various major vendors (i.e., Microsoft's .NET Framework, Sun's J2EE, IBM's WebSphere), many people have expressed an interest in getting their content to be shared by site owners and entities external to their organization. This ranges in function from the simple co-branding marketing service all the way up to an advanced e-commerce app that allows for the exchange of data between partners in a B2B relationship. And up until this point, accessing data from resources not within the same Web or network was extremely difficult to do...if not completely impossible.

While the demands are going to be great for advanced access to data through XML Web services, not many companies have been so quick to migrate to the technology... but this doesn't mean you can't pull off something similar with Classic ASP.

Working in the NeverNeverLand of high technology (Guam), especially in these tough economic times, I represent the "low-budget-to-no-budget" class of devs. The company I work for isn't directly involved in technology, and as such we've got very little to spend on IT. So I came up with a solution that does work...but is a little more scaled down than your normal SQL Server 2000 ==> BizTalk Server ==> 1,000-node server farm ==> monster datacenter solution. What we're going to write emulates the concept of the XML Web service, using Classic ASP.

Your most powerful asset - information

It's a very competitive advantage if you can offer your content to partners for use on their own sites... and it serves you very well. Large, dominating sites such as MSNBC.COM do this to stay ahead of the competition in the online news game by offering their headlines and story abstracts to other sites. The end result is that the content provider ultimately gets the traffic, which is a win for justifying traffic levels, which leads to revenue gains. And smaller sites love to incorporate remote content on their pages to stimulate traffic increases by way of association with larger entities. But the added overhead due to additional database connections is an unwanted headache for developers.

So, our "Web service" consists of a very simple script which works with the following:

  • JavaScript text files (files with a ".JS" extension) are "portable" in the sense that they can be referenced from within the HTML source code of a remote client page, with their contents being displayed. Thus, we can write a very simple code construct to get content portable.
  • Publishing the .JS files can be done by writing a script which connects to a database, writes a new text file outputting JavaScript code, and saves it to the server (overwriting the previous file with the same name).
  • This service never relies on external clients directly connecting to your database, so there isn't an additional overhead concern other than the made by you when running the script. This prevents your main site from being bogged down. That's the beauty of it you can control how many times the script is invoked if you embedded the script in an administration page, which would be executed by a staffer in your organization. So you could literally have tens, hundreds, or thousands of clients using your service and the load would at be the same minimal level, because you're essentially only connecting to your database once.
  • The code construct we'll be using for our JavaScript file (portablecontent.js) is as follows:
    <!--
    document.writeln('<a target="_blank" href="http://www.yourdomainname.com/filename.asp?id=8675309"><b>Senators debate during session sale of GTA</b></a><br>');
    document.writeln('The privatization of the Guam Telephone Authority is being touted as a top priority of the Gutierrez administration, as its sale is expected to bring some major</a><br>');
    .....more content as needed.....
    //-->

This has benefits for the client, in that:

  • The outputted JavaScript file will conform to CSS rules, so it's somewhat customizable by the client to match the flow and theme of their page(s).
  • The script also truncates a "description" field and appends "..." if the string is too long. This saves you from having to modify your table structure in an existing database.
  • The remote site manager never has to do anything modification-wise...he/she just references the JavaScript file and then never touches it again. The remote site manager also has peace of mind about not sacrificing traffic to their own if the user clicks away, because we'll open a new browser window by using the HTML attribute "_blank" within the <A> tag.

The only assumption is that you will have a file sitting on your server with the path (you can edit this to match your own site's settings).

  • http://www.yourdomainname.com/filename.asp?id=8675309

There is one concern you'll want to watch out for, however (I learned this the hard way), in that single quotation marks " ' " in ASCII format represent a line break in JavaScript, and this in turn will generate an error on the client's page, making for an ugly and dysfunctional service. And with much database-driven content being generated by forms - transmitting data in ASCII - this could run into problems. Fortunately, Unicode characters don't have this problem. As a workaround, I employ VBScript's Replace function to check for the existence of ASCII-type single-quotes, swapping each one with a Unicode single-quote, which is acceptable in JavaScript. (Note: To make this script run right, I had to type in the ASCII single quote character using NotePad, and the Unicode single-quote character using MS Word.)

Get the word out promoting your "Web service"

You can then promote the heck out of your new "Web service" by having interested users insert the following into the HTML of their page(s):

<!-- START PORTABLE HEADLINES -->
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr><td width="100%"><font face="Arial" size="2">
<script language="JavaScript" src="http://www.domainname.com/dirname/portablecontent.js"></script>
</font></td></tr>
</table>
<!-- END PORTABLE HEADLINES -->

Plan your code, code your plan

Here's the code you'll need for the script:

<% @ Language=VBScript %>
<html><body><head><META NAME="ROBOTS" CONTENT="NOINDEX,NOFOLLOW"></head>
<% 
' ************************************************************************
' 
' Portable Content Generator Script 
' Written by Jason Salas: jason@kuam.com 
' November 6, 2001
' This script connects to a data store, creates a new text file and saves
' it to the Web server.  The headlines can then be referenced in the
' underlying HTML code of a remote client page, using the absolute path
' to your file, such as:
'
'   <!-- START PORTABLE HEADLINES -->
'   <table border="0" cellpadding="0" cellspacing="0" width="100%">
'   <tr><td width="100%"><font face="Arial" size="2">
'   <script language="JavaScript"
'    src="http://www.domainname.com/filename.js"></script>
'   </font></td></tr></table>
'   <!-- END PORTABLE HEADLINES -->
'
' The only assumptions in this example are that you will have an ASP page
' feeding data with the following path:
'
' http://www.yourdomainname.com/filename.asp?id=8675309 
'
' ...and that your database has a table ("Content") with the
' following fields:
'
' DB Fields: StoryID, Title, Teaser
'
' ************************************************************************
' declare variables and connect to the data store
Dim objConn, objRS, FSO, Headlines
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "dsn=yourDSNnamehere;uid=sa;pwd=password;"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open "SELECT TOP 10 StoryID, Title, Teaser FROM Content " _
    & "ORDER BY StoryID DESC", objConn
Set FSO = CreateObject("Scripting.FileSystemObject")
' the default path for IIS, you may need to change as needed based on
' the setup of your Web server
Set Headlines = FSO.CreateTextFile("C:\inetpub\wwwroot\portablecontent.js", _
    True,False)
' write all the new headlines to a text file from the recordset
' (overwriting an existing file), and replace ASCII-type single
' quotes with UNICODE-style single quotes so the resultant
' JavaScript file won't generate an error
Headlines.Write("<!--" & vbcrlf)
Do While Not objRS.EOF
    trimmed = Replace(Trim(Left(objRS("Teaser"),165)),"'","",1,-1, _
        vbTextCompare)
    ' change this to reflect the path to your normal content files
    Headlines.Write("document.writeln('<a target=""_blank"" " _
        & "href=""http://www.yourdomainname.com/filename.asp?id=" _
        & objRS("StoryID") & """><b>" & Replace(objRS("Title"),"'", _
        "",1,-1,vbTextCompare) & "</b></a><br>');")
    Headlines.Write(vbcrlf & "document.writeln('")
    ' crop the description and add "... (more)" if the description
    ' string is longer than 165 characters.
    ' if not, then just write out the string
    If Len(trimmed) < 165 Then
        Headlines.Write(trimmed)
    Else
        Position1 = InStr(trimmed," ")
        Position2 = 2
            While Position1 > 1
                If Position2 > 1 Then
                    Position1 = Position1+1
                    Position2 = InStr(Position1, trimmed, " ")
                Else
                    Position2 = Position1
                    Position1 = 0
                End If
            Wend
        Position2 = Position2-1
        trimmed = left(trimmed, Position2)
        trimmed = trimmed & "... <a target=""_blank"" " _
            & "href=""http://www.yourdomainname.com/filename.asp?id=" _
            & objRS("StoryID") & """>(more)</a>"
        Headlines.Write(trimmed)
    End If
    Headlines.Write("<br>');")
    Headlines.Write(vbcrlf)
    objRS.MoveNext
Loop
Headlines.Write("//-->")
Headlines.Close
' destroy any open object references and close open connections
Set Headlines = nothing
Set FSO = nothing
objRS.Close
Set objRS = nothing
objConn.Close
Set objConn = nothing
' display a confirmation message
Response.Write("The headlines have been updated!")
%>
</body></html>
Headlines Only Sample

See the result of this script in action by clicking here. You could also modify the script to output only headline-type information as shown at right.

.NET Web Services allow for greater flexibility and choice

The only limitation is that with a system such as this, the client has very little say in what he/she can do with the resultant data (e.g., filtering for content, subscription-driven services, etc.). The client can modify formatting for presentation purposes, but that's about it. MSNBC has a very powerful portable product that it makes available to its affiliate stations across the U.S. wherein site managers can pick-and-choose which newsfeeds they wish to receive as MSNBC updates its site.

This is the big draw of XML Web services with the .NET Framework the service provider exposes access to the data to the client, and the client decides what to do with it to best fit his situation.

But for now, this is just an example of one of the many things you can do to drive more traffic to your site by leveraging the single-most important asset you've got information.

Have fun! :-D

About The Author

Jason Salas is Manager of the Web Development group for KUAM.COM, one of Guam's most popular Web sites. He also writes business and technology-oriented columns for KUAM.COM, and hosts the weekly segment "Tech Talk" on KUAM-TV, a whimsical look at what's new on the technical front and the Internet subculture. He has worked in the information technology industry for more than 11 years as a salesperson, marketing manager, developer, consultant, and author.

He is a graduate of Simon Sanchez High School, has a Bachelor's degree in Marketing from the University of Guam, a degree in Music Theory with Emphasis in Guitar from the Sutton Institute in Texas, and a Master's degree in Management of Applied Technologies from the University of Phoenix. He is also a Microsoft Certified Professional.

Jason can be reached at jason@kuam.com.

Full Size Sample