Let's see now how to create a multi-page cgi.
The two GSP sources page. |
This page is the same as the one shown in example 1.
<html>
<%@ language="c++"
mime type = "text/html"
%>
<%!
/*
* a comment to test the ! tag
*/
%>
<body>
<%
const char hello[]="Hello world";
int a=20;
int b=22;
int c=a+b;
%>
<h1><%=hello%></h1>
<p>The answer is <%=c%></p>
</body>
</html>
A second page containing a link to the first.
<html>
<%@ language="c++"
mime type = "text/html"
%>
<body>
<%
const char hello[]="test page";
%>
<h1><%=hello%></h1>
<p><a href="cpp.gsp">link to main page</a></p>
</body>
</html>
Invoking GSP to produce the cgi C++ source code |
bash# gsp -o gsp-cpp.cc -m cpp.gsp cpp1.gsp
This command will produce one file :
gsp-cpp.cc : the cgi source code
gsp-cpp.cc
#include <stdlib.h>
#include <iostream.h>
/*
* a comment to test the ! tag
*/
void cpp_gsp() {
cout << "Content-Type: ";
cout << "text/html";
cout << "\n\n";
cout << "<html>\n";
cout << "\n";
cout << "\n<body>\n";
const char hello[]="Hello world";
int a=20;
int b=22;
int c=a+b;
cout << "\n<h1>";
cout << hello;
cout << "</h1>\n<p>The answer is ";
cout << c;
cout << "</p>\n</body>\n</html>\n";
}
void cpp1_gsp() {
cout << "Content-Type: ";
cout << "text/html";
cout << "\n\n";
cout << "<html>\n";
cout << "\n<body>\n";
const char hello[]="test page";
cout << "\n<h1>";
cout << hello;
cout << "</h1>\n<p><a href=\"cpp.gsp\">link to main page</a></p>\n</body>\n</html>\n";
}
int main(int argc,char ** argv) {
char defaultPage[]="cpp.gsp";
char * requestedPage=getenv("PATH_INFO");
if (requestedPage==NULL) requestedPage=defaultPage;
if (requestedPage[0]=='/') requestedPage=&(requestedPage[1]);
if (strcmp(requestedPage,"cpp.gsp")==0||strcmp(requestedPage,"")==0)
cpp_gsp();
else if (strcmp(requestedPage,"cpp1.gsp")==0)
cpp1_gsp();
exit(0);
}
 |
bash# gcc -o mp-gsp.cgi gsp-cpp.cc -lstdc++
|
last update 01/18/2004 - 17:27 |