用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" border="0" 〉
<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"〉 首頁 </a〉<span class="L"〉|</span〉
<a href="http://www.51delphi.com/soft.asp" class="title"〉 軟件下載 </a〉<span class="L"〉|</span〉
<a href="http://www.51delphi.com/book.asp" class="title"〉 技術資料 </a〉<font color="#FFFFFF" class="L"〉|</font〉
<a href="http://www.51delphi.com/chat.asp" class="title"〉 交流天地 </a〉<span class="L"〉|</span〉
<a href="http://www.51delphi.com/link.asp" class="title"〉 網絡資源 </a〉<span class="L"〉|</span〉
<a href="http://www.51delphi.com/tl.asp" class="title"〉 技術論壇 </a〉<span class="L"〉|</span〉
<a href="http://www.51delphi.com/ly.asp" class="title"〉 給我留言 </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!