效果:
点击确定按钮,调用CGI程序,实现CGI的调用。
<
html
>
< head >
< title > CGI:Test </ title >
</ head >
< body >
< form action ="/cgi-bin/demo1.cgi" >
< p > 这是一个CGI的Test程序 </ p >
< input type ="submit" value ="确定" >
</ form >
</ body >
</ html >
< head >
< title > CGI:Test </ title >
</ head >
< body >
< form action ="/cgi-bin/demo1.cgi" >
< p > 这是一个CGI的Test程序 </ p >
< input type ="submit" value ="确定" >
</ form >
</ body >
</ html >
分析:
HTML语言是一种描述性语言。html代码被包含在<html></html>之间。
<
head
>
< title > CGI:Test </ title >
</ head >
< title > CGI:Test </ title >
</ head >
这里是一个标题头。如:
这里是正文部分:
<
body
>
< form action ="/cgi-bin/demo1.cgi" >
< p > 这是一个CGI的Test程序 </ p >
< input type ="submit" value ="确定" >
</ form >
</ body >
< form action ="/cgi-bin/demo1.cgi" >
< p > 这是一个CGI的Test程序 </ p >
< input type ="submit" value ="确定" >
</ form >
</ body >
正文部分就只有一个表单。
action部分表示,点击确定按钮后,他会调用/cgi-bin/demo1.cgi程序。这里没有给表单指定类型。
<
input
type
="submit"
value
="确定"
>
创建一个按钮。
下面我们看一下。CGI部分的C代码:
#include
<
stdio.h
>
#include < stdlib.h >
int main( void )
{
printf( " %s\r\n\r\n " , " Content-Type:text/html " );
printf( " <html>\n<TITLE>CGI1:CGI hello!</TITLE>\n " );
printf( " <center><H1>hello, this is frist CGI demo!</H1></center>\n</html> " );
return 0 ;
}
#include < stdlib.h >
int main( void )
{
printf( " %s\r\n\r\n " , " Content-Type:text/html " );
printf( " <html>\n<TITLE>CGI1:CGI hello!</TITLE>\n " );
printf( " <center><H1>hello, this is frist CGI demo!</H1></center>\n</html> " );
return 0 ;
}
很简单。就是3个printf。
这里的printf不会打印到服务器上,它已经做了重定向。指向了。浏览器.
当我们点击HTML页面上的"确定"按钮时就会调用这个CGI程序.这段代码由GCC编译.
点击后就会跳出一个页面.我们来看看这个页面的原代码再和这个C语言程序进行比较.
<
html
>
< TITLE > CGI1:CGI hello! </ TITLE >
< center >< H1 > hello, this is frist CGI demo! </ H1 ></ center >
</ html >
< TITLE > CGI1:CGI hello! </ TITLE >
< center >< H1 > hello, this is frist CGI demo! </ H1 ></ center >
</ html >
现象说明:
除开第一个printf其他的都是原样输出了.
就是说在我们的C语言程序中够成一个HTML页面代码.
本实验测试通过.
注意:编译好的CGI程序是放在服务器上的.
wenhao@20110602