GSP - A simple exemple

 GSP Home  Next

Here is a simple example showing what gsp can do... This example explains how to create a cgi from a page mixing html and C++ code.

the GSP source page : cpp.gsp 

<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>

Invoking GSP to produce the cgi C++ source code 

bash# gsp -o gsp-cpp.cc -m cpp.gsp

This command will produce one file :

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";
}
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();
exit(0);
}

Compiling the program 

bash# gcc -o gsp-cpp.cgi gsp-cpp.cc -lstdc++

Copy it in your http server cgi-bin folder, then you can test it.

 début de page  GSP Home  Next last update 01/18/2004 - 17:27