天天看點

用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);

end;

加入一下代碼

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

const

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

var

  s:string;

  yy,mm,dd:word;

    decodedate(date,yy,mm,dd);

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

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

    s:=s+'</font〉 歡迎你通路delphi窯洞!';

    Response.content := s;

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〉

 </table〉

取名為title.html,

回到delphi 裡面,在wm 裡面放置一個PageProducer控件,命名為title,

輕按兩下此控件,出現一下代碼

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

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

      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;

改寫Twm.wmindexAction過程為以下:

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

    Response.content := title.Content;

好!編譯,stop apache and start apache!

OK! yeah!

其他的東西你可以用類似方法添加,現在就取決于你的網頁設計能力了:)

今天就寫到這裡,下次寫存取資料庫的方法及動态網頁!byebye!