|
|
|
This C++ tutorial shows how to develop Ajax applications by making use of CGI programs
In today’s web world of Ajax (Asynchronous Javascript and XML), C++ programmers may feel out of place, but there’s not need for that - they just need to make use of that old favourite: CGI - the Common Gateway Interface. Basic Requirements for C++ and CGIThe C++ programmer doesn’t really need that much - just a server that has:
Creating a Simple CGI C++ ProgramA simple CGI program consists of C++ code that will produce an output. Something like the following will suffice: #define QUERY_STRING getenv
("QUERY_STRING")
#include <iostream>
using namespace std;
int main() {
cout << "Content-Type: text/plain\n\n"
<< "The Query String is: "
<< QUERY_STRING << "\n";
return 0;/div } This C++ code does three things:
It is then just a matter of compiling the C++ code, for example: g++ -o response response.cpp The compiled program must be placed in the CGI bin directory for the server. The developer may have to check the Apache2 configuration files to find out where that is, but once it's in the correct location then the C++ program can be accessed via the web browser: http://<my server>/cgi-bin/response?input="A test" Of course, where this gets interesting is when this standard CGI program is turned into a state of the art Ajax application. Turning a C++ CGI Program into an Ajax ApplicationThe key to Ajax is not some new technology - it is quite simple Javascript code: <script type = "text/javascript"> var XMLHttp; if(navigator.appName == "Microsoft Internet Explorer") { XMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else { XMLHttp = new XMLHttpRequest(); } function getresponse () { XMLHttp.open ("GET", "/cgi-bin/response?" + "fname=" + document.getElementById('fname').value + "&sname=" + document.getElementById('sname').value ,true); XMLHttp.onreadystatechange=function() { document.getElementById('response_area').innerHTML = XMLHttp.responseText; } XMLHttp.send(null); } </script> First Names(s)<input onkeyup = "javascript: getresponse ()" id=fname><br> Surname<input onkeyup = "javascript: getresponse ()" id=sname> <div id = "response_area"> </div> This HTML and Javascript code does the following:
In this example the two variables are sent to the CGI program; however, the string that is returned contains all of the data as well as some control characters - further processing is required before that data is usable. Processing Ajax Data in a C++ ProgramAt this point the C++ programmer could spend a few hours writing code to process the QUERY_STRING variable, however this is not necessary - there are any number of C++ libraries freely available, for instance the CGI libraries can be installed on a Debian Linux server by logging on as root and typing: apt-get install libcgicc1 Then, the CGI library can be used in the following way: #include <iostream> #include "cgicc/Cgicc.h" using namespace std; using namespace cgicc; int main() { Cgicc cgi; form_iterator fname = cgi.getElement("fname"); form_iterator sname = cgi.getElement("sname"); cout << "Content-Type: text/plain\n\n" << "You've input:" << " First names(s): " << **fname << " Surname: " << **sname << endl; return 0; } This new file can be compiled (after ensuring that the complier knows which library to use and where it is) as follows: g++ -o response -I/usr/include -lcgicc response2.c With these few easy steps the C++ programmer, with the aid of just a little Javascript, has moved into the world of Ajax applications.
The copyright of the article Ajax and the C++ Programmer in C Programming is owned by Mark Alexander Bain. Permission to republish Ajax and the C++ Programmer in print or online must be granted by the author in writing.
Comments
Oct 13, 2008 7:06 AM
Guest
:
1 Comment:
|
|
|
|
|
|
|
|