用Delphi制作留言板

留言板网页不同于一般网页,它不但有网页部分,而且还要有数据存储功能。用Delphi的的CGI应用程序可以轻松地制作留言板网页,不但可以实现ASP、HTML很难实现的底层操作,而且简化了CGI应用程序开发过程。
  #1 步骤如下:
   一、选择Delphi的菜单“File/New”在New标签中选择“Web Server Application”。然后再选择“CGI Stand-alone executable”创建一个CGI应用程序。
   二、系统自动创建一个非可视的WebModule1组件,我们按以下步骤给程序添加控制和代码:
   (1) 双击WebModule1的Actions属性,弹出Actions属性对话框

(2) 单击Add按钮,添加一个新行WebActionItem1

(3) 将WebActionItem1的Default属性设置为True,这样设置可在访问CGI.EXE时直接访问此页面

(4) 单击Add按钮,添加一个新行WebActionItem2

(5) 将WebActionItem2的PathInfo属性设置为“\Info”,即访问此页面的路径为CGI.EXE\Info

(6) 增加WebActionItem1的OnAction事件,添加代码,将留言板页面的HTML语句赋给Response.Content属性,让用户在访问WEB服务器应用程序时显示

(7)增加WebActionItem2的OnAction事件,添加代码处理留言板页面传回的信息,同时显示相关信息通知用户。其中涉及Request.ContentFields.Values['index'],及Request.ContentFields.Strings[counts],两个属性的应用。
   Request.ContentFields.Values ['UserName']是指留言板页面中Name=“UserName”的控件中的数据信息。
   Request.ContentFields.Strings [Counts]是指留言板页面传过来的各项数据及对应的控件名。以Counts区分它为哪项数据,其数值为留言板页面传输数据项的顺序号。
   数据存储方式采用文本文件存储,如果网友的主页人气鼎盛也可考虑改用数据库存储。
   以下是完整的程序代码:
   unit Html;
   interface
   uses
   Windows,Messages,SysUtils,Classes,HTTPApp;
   type
   TWebModule1 =class(TWebModule)
   procedure
   WebModule1WebActionItem1Action(Sender: TObject;

Request: TWebRequest; Response: TWebResponse;
   var Handled: Boolean);

procedure
   WebModule1WebActionItem2Action(Sender: TObject;
   Request: TWebRequest; Response: TWebResponse;
   var Handled: Boolean);

private
   {Private declarations}

public
   {Public declarations}

end;
   var
   WebModule1: TWebModule1;
   implementation
   {$R *.DFM}
   procedure
   TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;Request: TWebRequest; Response:
   TWebResponse; var Handled: Boolean);

var html:string;
   begin
   html:=';'

html:=html+'会员注册<br>   

html:=html+'

会员注册

';
   html:=html+'';
   html:=html+'
   method=post" id=form1" name=form1" style=
   FONT-SIZE: larger">';
   html:=html+'

姓名:    maxlength=10" name=UserName">

';
   html:=html+'

性别:    name=Sex" value=Man" checked>男';
   html:=html+'    value=Woman">女

';
   html:=html+'

年龄:    maxlength=3" name=Age">

';
   html:=html+'

所在城市:    maxlength=16" name=City">

';
   html:=html+'

E-mail:    maxlength=30" name=E-mail">

';
   html:=html+'

留言:

';
   html:=html+'
';
   html:=html+'';
   html:=html+'
';
   html:=html+'';
   Response.Content :=html;
   end;
   procedure
   TWebModule1.WebModule1WebActionItem2Action(Sender: TObject;Request: TWebRequest; Response:
   TWebResponse; var Handled: Boolean);
   var
   html:string;
   Output: TextFile;
   Counts,line:integer;
   begin
   Assignfile(Output,'List.Txt');{如果没有list.txt文件会报错,须先手工创建。}

Append(output);

for counts:=0 to Request.ContentFields.Count-2 do
   begin
   if counts    begin
   Write(Output,
   Request.ContentFields.Strings[counts]);

for line:=0 to 20-length(Request.
   ContentFields.Strings[counts]) do
   begin
   Write(Output,' ');

end;
   end
   else
   begin
   Write(Output,
   Request.ContentFields.Strings[counts]);

Writeln(Output,' ');
   end;
   end;
   CloseFile(output);

html:='';
   html:=html+'Thank You!';
   html:=html+'

'+
   Request.ContentFields.Values['UserName'];

html:=html+'你好!你已注册成功!

';

html:=html+'点击这里返回';
   html:=html+'

';
   Response.Content:=html;
   end;
   end.
   注:以上代码在Delphi4、IIS4.0/PWS、IE5.0中运行通过。