How to Create a Simple VBScript CGI Web Page

Using VBScript for as a CGI Internet Programming Language

© Mark Alexander Bain

Jul 20, 2009
How to Create a Simple VBScript CGI Web Page, Mark Alexander Bain
VBScript is not normally considered as a cgi programming language. However, with just a little tweeking, it is as easy (if not easier) to use as Perl.

Microsoft VBScript is a very powerful and adaptable programming language. However, when it comes to web site programming there are limitations to VBScript. Not in it what it can do, but in what it is allowed to do. For example:

  • web browsers such as Firefox do not allow VBScript to run at all
  • web browsers such as Internet Explorer only allow VBScript to run at the user's discretion (so that VBScript may or may not be allowed to run, or may only run once the user as answered 'Yes' in an input box)

Obviously these tend to be show stoppers for VBScript programmers wanting to create applications for the Internet. The VBScript programmer, therefore, has very few options:

  • develop their application in VBScript and just hope that their users will have the correct web browser with the appropriate security settings
  • move across to other scripting languages (such as Javascript) which will be run by any web browser
  • Move to server languages such as Perl or ASP

Or, on the other hand, they could just start creating CGI web pages with VBScript.

CGI Web Pages and Web Servers

CGI (Common Gateway Interface) web pages all contain code and they normally reside in the server's cgi-bin directory. However, that code is run on the server instead of in the user's web browser.

The CGI pages simply returns an HTML output to the browser. And web servers, such as Apache, don't even care what language is to be uses to interpret the code. All they need to know is where to find the code interpreter, and that's done by setting the shebang line.

Setting the Shebang Line for a VBScript CGI Web Page

The shebang line must always be the very first line of a CGI web page. It starts with a #! prefix and contains the location of the interpreter to be used. Obviously, in the case in VBScript this will be the Cscript executable. The shebang line should, in theory, be something like:

#!c:\WINDOWS\system32\cscript.exe

However this actually outputs some text (which will stop the CGI page from working). The next stage is to turn off this text:

#!c:\WINDOWS\system32\cscript.exe /nologo

At this point Cscript expects a file to be input, and this is the file that will contain the actual web programming code, for example the file could be called call_hello_world.vbs and would look like:

#!c:\WINDOWS\system32\cscript.exe /nologo "C:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin\hello_world.vbs"

The purpose of this first file is simply to call the second file (hello_world.vbs) with Cscript. It's this second file that contains the CGI code.

A Simple VBScript CGI File

A VBScript CGI file must start with the line:

wscript.echo "Content-type: text/html" & vbcrlf

This sets the header information for the web page. Any information for the body of the web page must be placed after that line, for example:

wscript.echo "<h1>Hello World</h1>"
wscript.echo "This is is an output from VBScript"

And the end result of viewing "call_hello_world.vbs" can be seen in figure 1 at the bottom of this article. That end result is, of course, a CGI web page written completely in VBScript. Even so, many programmers may not be happy that each CGI web page will need to consist of two files and would refer just a single file. Therefore, that's covered in Professional CGI Web Development with VBScript.


The copyright of the article How to Create a Simple VBScript CGI Web Page in Windows Programming is owned by Mark Alexander Bain. Permission to republish How to Create a Simple VBScript CGI Web Page in print or online must be granted by the author in writing.


How to Create a Simple VBScript CGI Web Page, Mark Alexander Bain
 Figure 1 A Simple VBScript CGI Web Page, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo