用delphi6+Apache 开发动态网站,创建一个简单的网页

用delphi6+Apache 开发动态网站(一)

-------创建一个简单的网页

随着Internet 的飞速发展,动态网页的开发方法五花八门,有ASP,JSP,PHP,CGI等等,其中web server主要

集中在IIS,Apache等上,MS 的IIS 以其强大的功能吸引了很多的开发者,ASP 是IIS 上用的最多的网页开发

方法,可是ASP 对开发非MS 产品的数据库程序时,简直就是一场恶梦,同时IIS 的漏洞越来越多,系统管理员

每天都得注意最新的漏洞补丁,否则不是被黑客攻翻就是被病毒感染,最近的redcode,bluecode,nimda就是例子。

具有速度快,安全性好,跨平台而且是完全免费的apache http server吸引了越来越多的开发者,而borland 新推

出的delphi6 开始支持Apache,功能强大的delphi 和 快速安全的apache 是一对非常好的动态网站开发方式,而且

delphi6 的工程源码几乎不用动就可以用kylix 编译,并在Linux 下的apache 上运行,厉害吧,动手吧,让

我们开始做一个简单的网页吧!

1.环境设置

先从http://www.apache.org 上下载apache http server for windows 版,最新的版本是1.3.22,

然后安装到服务器上,调试阶段最好现在本机上安装,反正apache 支持win9x:),安装完毕后,在IE里面输入

http://localhost,会正常显示apache 默认页,说明安装成功!

2.现在打开delphi6 ,点菜单file-new-other,在new页里面选web server aplication,出现下面图片

选择Apache shared Module(DLL),系统就会建立一个空的工程.把工程另存为test,unit1另存为wmp.pas.

delphi 就会生成以下的工程代码

library test;

uses

WebBroker,

ApacheApp,

wmp in 'wmp.pas' {WebModule1: TWebModule};

{$R *.res}

exports

apache_module name 'Project1_module';

begin

Application.Initialize;

Application.CreateForm(TWebModule1, WebModule1);

Application.Run;

end.

把'Project1_module' 改为'test_module'

然后在applicaiotn.Initilize 后面加入一下代码(很重要),

ModuleName:='test_module';

ContentType:= 'test-handler';

以上是让apache 加载dll是用的,如果与apache 的配置不一样的话,是无法运行的.

然后设置WebModule1的name 为wm(这仅仅是为了以后编程简单^_^),

打开wm的actions,出现一个editing wm.actions窗口,点击add new,出现一个新的item,

设置其name 为index,pathinfo 为/index,default 为true,

然后到events 页双击onaction

code editor出现一下代码:

procedure Twm.wmindexAction(Sender: TObject; Request: TWebRequest;

Response: TWebResponse; var Handled: Boolean);

begin

end;

加入一下代码

procedure Twm.wmindexAction(Sender: TObject; Request: TWebRequest;

Response: TWebResponse; var Handled: Boolean);

const

chweek:array [1..7]of string[2]=('日','一','二','三','四','五','六');

var

s:string;

yy,mm,dd:word;

begin

decodedate(date,yy,mm,dd);

s:='今天是'+inttostr(yy)+'年'+inttostr(mm)+'月'+inttostr(dd)+'日';

s:=s+' 星期'+chweek[dayofweek(date)];

s:=s+'</font〉 欢迎你访问delphi窑洞!';

Response.content := s;

end;

end;

OK! 编译,如果按F9 会出现错误!(因为它是个DLL呀!)

把编译出来的test.dll 拷到apache 目录下的modules目录

然后用文本编辑器打开apache 目录下的conf目录里面的httpd.conf

最上面加入一下文本

LoadModule test_module "C:/Program Files/Apache Group/Apache/modules/test.dll"

<Location /test〉

SetHandler test-Handler

</Location〉

如果httpd.conf 里面的ClearModuleList 前面没有# 的话,一定要加上!!!!

现在先stop apache server,然后再start apache server

在IE 里面输入http://localhost/test

怎样出来了吗?

浏览器里面是不是显示

今天是2001年10月30日 星期二 欢迎你访问delphi窑洞!

如果你看到这样的话,那么你成功了!

还算简单吧!

下一步,显示一个静态网页,先输入以下的网页代码到一个html文件里面

<head〉

<meta http-equiv="Content-Type" content="text/html; charset=gb2312"〉

</head〉

<title〉delphi窑洞</title〉

<style type="text/css"〉

<!-- td { font-family: "宋体"; font-size: 9pt}

body { font-family: "宋体"; font-size: 9pt}

select { font-family: "宋体"; font-size: 9pt}

a.news {color:#ff0033;text-decoration:none}

a.news:hover {color:#000000;text-decoration:underline}

A {text-decoration: none; color: navy; font-family: "宋体"; font-size: 9pt}

A:hover {text-decoration: none; color: red; font-family: "宋体"; font-size: 9pt;}

.white {color: white}

.silver {color: silver}

.ourfont {font-size: 9pt ; line-height:9pt; }

--〉

</style〉

<script〉

<!-- Beginning of JavaScript Applet -------------------

function scrollit_r2l(seed)

{ var m1 = "欢 迎 访 问 delphi 窑 洞" ;

var m2 =" ";

var msg=m1+m2;

var out = " ";

var c = 1;

if (seed 〉 100)

{ seed-=2;

var cmd="scrollit_r2l(" + seed + ")";

timerTwo=window.setTimeout(cmd,200);}

else if (seed <= 100 && seed 〉 0)

{ for (c=0 ; c < seed ; c++)

{ out+=" ";}

out+=msg; seed-=2;

var cmd="scrollit_r2l(" + seed + ")";

window.status=out;

timerTwo=window.setTimeout(cmd,200); }

else if (seed <= 0)

{ if (-seed < msg.length)

{

out+=msg.substring(-seed,msg.length);

seed-=2;

var cmd="scrollit_r2l(" + seed + ")";

window.status=out;

timerTwo=window.setTimeout(cmd,200);}

else { window.status=" ";

timerTwo=window.setTimeout("scrollit_r2l(100)",75);

}

}

}

// -- End of JavaScript code --〉

</script〉

<body topmargin="0" leftmargin="0" bgcolor="#F1FEFE" bgproperties="fixed" onload="timerONE=window.setTimeout('scrollit_r2l(100)',500);"〉

<table width="100%" valign="top" cellspacing="0" cellpadding="1" border=0〉

<tr valign="top" border=0 〉

<td height=96 align="center" 〉 <img src="http://www.51delphi.com/logo.jpg" border=0 〉 </td〉

</tr〉

<tr height=16〉

<td width="100%" 〉

<#date〉

</td〉

</tr〉

<tr〉

<td width="100%"〉

<table width="100%" valign="top" 〉

<td width="7" valign="left" bgcolor="#FFCF60"〉<img src="http://www.51delphi.com/topleft.gif" width="7" height="7" border=0 〉</td〉

<td valign="bottom" bgcolor="#FFCF60" align="center" class="L17"〉

<a href="http://www.51delphi.com" class="title"〉&nbsp;&nbsp;首页&nbsp;&nbsp;</a〉<span class="L"〉|</span〉

<a href="http://www.51delphi.com/soft.asp" class="title"〉&nbsp;&nbsp;软件下载&nbsp;&nbsp;</a〉<span class="L"〉|</span〉

<a href="http://www.51delphi.com/book.asp" class="title"〉&nbsp;&nbsp;技术资料&nbsp;&nbsp;</a〉<font color="#FFFFFF" class="L"〉|</font〉

<a href="http://www.51delphi.com/chat.asp" class="title"〉&nbsp;&nbsp;交流天地&nbsp;&nbsp;</a〉<span class="L"〉|</span〉

<a href="http://www.51delphi.com/link.asp" class="title"〉&nbsp;&nbsp;网络资源&nbsp;&nbsp;</a〉<span class="L"〉|</span〉

<a href="http://www.51delphi.com/tl.asp" class="title"〉&nbsp;&nbsp;技术论坛&nbsp;&nbsp;</a〉<span class="L"〉|</span〉

<a href="http://www.51delphi.com/ly.asp" class="title"〉&nbsp;&nbsp;给我留言&nbsp;&nbsp;</a〉

</td〉

<td valign="top" align="right" bgcolor="#FFCF60" width="7"〉<img src="http://www.51delphi.com/topright.gif" width="7" height="7" border=0 〉</td〉

</table〉

</td〉

</tr〉

</table〉

取名为title.html,

回到delphi 里面,在wm 里面放置一个PageProducer控件,命名为title,

双击此控件,出现一下代码

procedure Twm.PageProducer1HTMLTag(Sender: TObject; Tag: TTag;

const TagString: String; TagParams: TStrings; var ReplaceText: String);

const

chweek:array [1..7]of string[2]=('日','一','二','三','四','五','六');

var

s:string;

yy,mm,dd:word;

begin

if tagstring='date' then

begin

decodedate(date,yy,mm,dd);

s:='今天是'+inttostr(yy)+'年'+inttostr(mm)+'月'+inttostr(dd)+'日';

s:=s+' 星期'+chweek[dayofweek(date)];

s:=s+'欢迎你访问delphi窑洞';

ReplaceText:=s;

end;

end;

改写Twm.wmindexAction过程为以下:

procedure Twm.wmindexAction(Sender: TObject; Request: TWebRequest;

Response: TWebResponse; var Handled: Boolean);

begin

title.HTMLFile:=defpathname+'/title.html';

Response.content := title.Content;

end;

好!编译,stop apache and start apache!

在IE 里面输入http://localhost/test

OK! yeah!

其他的东西你可以用类似方法添加,现在就取决于你的网页设计能力了:)

今天就写到这里,下次写存取数据库的方法及动态网页!byebye!