|
||||||
Professional CGI Web Development with VBScriptUsing VBScript as a CGI Programming LanguageCGI (the Common Gateway Interface) is traditionally the domain of languages such as Perl. However, there is no need for the VBScript programmer to set aside their skills
The article How to Create a Simple VBScript CGI Web Page shows just how easily the web application developer can use VBScript as a CGI (Common Gateway Interface) programming language. The techniques is quite simple:
However, there is one (slight) drawback to this technique: two files are needed for each CGI script (the CGI code file and the calling file). A much better solution would be for:
Two files will still be needed, but at least the same shebang line can be used in every CGI file rather than a different shebang line for each VBScript file. An Example VBScript CGI ScriptThe first line of the CGI file must be the shebang line. This calls the "header.vbs" file which, in turn, returns control to the calling CGI file: #!c:\WINDOWS\system32\cscript.exe /nologo "C:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin\header.vbs"
Next the obligatory HTTP header must be sent to the web browser: wscript.echo "Content-type: text/html" & vbcrlf
And then the actual CGI code can be added: wscript.echo "<h1>Hello World</h1>"
wscript.echo "This is is an output from VBScript"
If this code is saved into the server's cgi-bin directory (as, for example, "hello.vbs") then it is almost ready to use. The next step is to create the "header.vbs" file. Passing Control Between VBScript CGI ScriptsThe VBScript CGI technique is quite simple:
Therefore the first job that the second file must do is to create a File System Object: Set fso = CreateObject("Scripting.FileSystemObject")
That's then used to open the original file (the name of which is passes to the new file as an argument): Set original = FSO.OpenTextFile(WScript.Arguments(0))
Once the original file is open then its contents can be processed. Of course, the first line is the shebang line, and so that is simply read in (since it's already been processed by the web server): firstline = original.ReadLine
The remainder of the file is then executed by using VBScript's ExecuteGlobal method: ExecuteGlobal cgi.ReadAll
If the "hello.vbs" file is now entered as a Url into a web browser( for example http://localhost/cgi-bin/hello.vbs) then the VBScript will be run on the server and the HTML from the code will be displayed in the web browser (as shown in Figure 1 at the bottom of this article). One final advantage to using this technique is, of course, that any additional processing that is required (such as handling the GET and POST queries) can be placed in the "header.vbs" file and that will automatically be passed on to of the CGI files that use it. And so, the VBScript programmer can smoothly and effortlessly glide into the world of the CGI web page.
The copyright of the article Professional CGI Web Development with VBScript in Windows Programming is owned by Mark Alexander Bain. Permission to republish Professional CGI Web Development with VBScript in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||