When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Wednesday, December 05, 2001

How to Build a Low-Budget Web Service with Classic ASP

By Jason Salas

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.

- continued -

Also, a concern of yours being a service provider should be what clients who use your remote content use for their apps. There are a plethora of platforms in use by remote clients, and it's unconscionable for you as a developer to create a custom solution for each. And much to my chagrin (and Microsoft's), not everyone out there uses ASP, so performing screen scraping with VBScript's XMLHTTP object is not feasible for everyone, likewise with complex DCOM solutions with remote procedure calls (RPCs). Fortunately, the portability of the resultant JavaScript text file solves this, being nearly universally compatible with whatever the client is running on their end. This results in better chances for you to get your information out and about. (For more information on screen scraping, see: Treating Another Web Site's Web Page as Data On Your Own Site.)

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. In fact, there's already an article here on 4Guys that shows how to create Web Services in classic ASP using Microsoft's SOAP Toolkit. (Read Creating Web Services with ASP.) In this article, I will show how to create a much simpler Web Service that utilizes a JavaScript file and some clever ASP code.

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 Web Service solution. What we're going to write emulates the concept of the XML Web service, using JavaScript and classic ASP.

Your Most Powerful Asset - Information (or Understanding Why Web Services Rule)
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>' +
    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 HTML into their Web page(s):

<table border="0" cellpadding="0" cellspacing="0" width="100%"><
  tr><td width="100%"><font face="Arial" size="2">
    <script language="JavaScript" 

Plan your Code, Code your Plan
An example of the Web service output through a remote Web site. The code needed to create this .JS file is fairly simple and straightforward. All we need to do is first open a text file to dump out the JavaScript contents to. This text file is opened and manipulated using the FileSystemObject object. To learn more about this object, be sure to check out the FileSystemObject FAQs on ASPFAQs.com.

Next, a connection to the database is made, and the headlines and their information are loaded into a Recordset. This Recordset is iterated through, and each record's contents are outputted as a line in the JavaScript file, checking to see if the description is longer than 165 characters - if it is, only the first 165 characters (and perhaps some more, up until the next word) are displayed, followed by a ....

That's essentially all there is to it! The code is not extremely long, but to lengthy to place in this article. Therefore, you can view the source code in a separate window. Feel free to modify this code however you see fit, such as just displaying headline information and forgoing the description. You can see an example of the output my script generates here; to the right is an image of the headlines as they might appear on a Web site utilizing this Web service I created.

The only drawback with this approach is that the script must be run whenever you wish to update the JavaScript file. Fortunately there are a couple of articles here on 4Guys that examine how to schedule the execution of an ASP script. See Scheduling the Execution of an ASP Script and Automating Tasks with WSH.

.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. To learn more about Web Services with ASP.NET, check out this Web Service Tutorial.

Happy Programming!

  • By Jason Salas

    Jason Salas is Web Development Manager 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 also owns IntelliBiz, a consulting company focusing on developing XML Web services. 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.

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article