用delphi+Apache 开发动态网站(二)

 -------创建数据库应用

时间过的真快,距离上次写Delphi+Apache 开发动态网站已经快一年了,Borland 的Delphi 7都发布快半年了,一直没有时间静下写文章,今天就写一下怎么创建数据库应用,其实网站的数据库和普通的客户端应用差不多,唯一的不同就是那些可视化控件不能再用了,虽然delphi 6 和7里面提供的websnap 功能比较强大,但是在实际应用中定制性太差,可控制的东西复杂,因此本文还是以web broker 为例。

一般的网页都是以上、中、下来设计的,即上面是基本固定的LOGO 和联接部分,中间是内容部分,下面是版权等基本信息,因此我们可以把网页分成三部分,即上、中、下部分,由于上、下部分相对固定,我们就建立两个PageProducer1 元件与之相对应,中间的部分再根据情况动态设定。

   我们以delphi 7 和 apache 2.0.43 和 Interbase为例,delphi7 不支持 Apache 2.0.43 ,具体解决办法请看我的文章使Delphi 6 支持Apache 2.0。首先建立web server 应用程序。如图一,

 

  这样就建立起一个Apache 2.x 的 DSO 模块,放置dbexpress 数据库联接和访问控件和PageProducer 元件,如图二,

 

并保证 Dbexpress 控件可以正常访问数据库,具体访问方式超出本文章范围,这里不再详细讨论。在Apache  2 的主目录(一般为htdocs目录)建立一下两个文件,title.html,foot.html.

title.html:

<#getalluser>

<BODY  topmargin="0" leftmargin="0" > 

 

<table align="center" border="0" width="90%"  cellSpacing="1" cellPadding="1" >

<td align="left" width="20%" >

   <a href="http://www.51delphi.com"><img src="/image/logo.jpg" border=0></a>

 </td>

<td align="left">

<table width="100%" height=20 align="left">

<form method="POST" action="/delphi/login<#wz>"   >

   <td bgColor="#FEFDED"  align="center">

<FONT class="pt9">用户名<input text name="user"  class="edit" size=12>

密码<input type=password name="pass"  class="edit" size=12 >

<input type =submit value="登陆" class="bt"  ></font></td>

<td bgColor="#FEFDED" align="center">

<FONT class="pt9"><a href="/delphi/userreg" target="_blank"> 注册新用户</font>

 </td>

 <td bgColor="#FEFDED" align="center">

 <FONT class="pt9">

 <#username>

   </font>

 </td>

</form>

 </table>

 

</td>

</table>

<table  align="center" border="0"  width="90%" cellspacing="0" cellpadding="0">

      <td align="left"   bgcolor="white"><a href="/delphi/fwtj"> <#alluser></a></td>

      <td align="center" ><#date></td> 

      <td align="right" >当前人数:<#curuser></td> 

      </table>

    </td>

 

</table> 

 

<table align="center" border="0" width="95%" height=24 bgcolor="#FFCF60" cellSpacing="0" cellPadding="0" > 

<td width="7" valign="left" bgcolor="#FFCF60"><img src="/topleft.gif" width="7" height="7"></td>

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

<a href="/delphi/" class="title">&nbsp;&nbsp;首页&nbsp;&nbsp;</a><span class="L">|</span>

<a href="/delphi/soft" class="title">&nbsp;&nbsp;软件下载&nbsp;&nbsp;</a><span class="L">|</span>

<a href="/delphi/book" class="title">&nbsp;&nbsp;技术资料&nbsp;&nbsp;</a><font color="#FFFFFF" class="L">|</font>

<a href="/delphi/jswz" class="title">&nbsp;&nbsp;技术文章&nbsp;&nbsp;</a><span class="L">|</span>

<a href="/delphi/link" class="title">&nbsp;&nbsp;网络资源&nbsp;&nbsp;</a><span class="L">|</span>

<a href="/delphi/jstl" class="title">&nbsp;&nbsp;技术论坛&nbsp;&nbsp;</a><span class="L">|</span>

<a href="/delphi/ly" class="title">&nbsp;&nbsp;给我留言&nbsp;&nbsp;</a>

            </td>

<td valign="top" align="right" bgcolor="#FFCF60" width="7"><img src="/topright.gif" width="7" height="7"></td>

</table>

 

foot.html:

  <br>

<TABLE align="center" cellSpacing=1 cellPadding=1 width="95%" border=0>           

  <TBODY>           

  <TR>           

    <TD align=center bgcolor="#FFFFFF" colspan="3"><FONT class=pt9 color="#800000">谢谢光临</FONT></TD>          

  </TR>           

  <TR bgColor="#FFFFCC">           

    <TD align=middle ><A            

      href="/delphi/ly"><font class="pt9" color="#0000FF">给我留言</font></A></TD>         

    <TD align=middle ><A href="/"><font class="pt9" color="#0000FF">返回主页</font></A></TD>         

    <TD align=middle ><A href="mailto:xalion@51delphi.com"><font class="pt9" color="#0000FF">给我写信</font></A></TD></TR></TBODY></TABLE>          

<TABLE cellSpacing=1 cellPadding=1 width="100%" border=0 height="20">         

  <TBODY>         

  <TR>         

    <TD align=middle height="1"><FONT class="pt9">1999-2001 delphi 窑洞              

  copyright</FONT></TD></TR>            

  <TR>            

    <TD align=middle height="14">窑洞洞主:xalion&nbsp;&nbsp;&nbsp;&nbsp; <FONT class="pt9">合作伙伴:<A            

      href="mailto:richard@51delphi.com">阿土</A></FONT></TD></TR>           

  </TBODY></TABLE>

   </BODY></HTML>  

 

 

好,开始我们的程序之旅:

系统常量:

const

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

 defpathname:string='d:/Apache Group/Apache2/htdocs';

 defsoftpath:string='ftp://202.117.213.5/file/';

 defbookpath:string='ftp://202.117.213.5/book/';

gridhead:string=' <table width="100%" border="0" cellspacing="1" cellpadding="1" bgColor="#609000">';

 gridheadline:string='  <tr bgcolor="#ffffcc">';

 gridline:string=' <tr bgcolor="#fefded">';

 gridtdhead:string=' <td ><font class="pt9">';

 gridtdheadnowrap:string=' <td  nowrap><font class=pt9>';

 gridtdend:string=' </font></td>';

 webModule 的建立事件:

  procedure Twm.WebModuleCreate(Sender: TObject);

var

   fconfig:Tinifile;

   bbsopen:string;

   fname:pchar;

   path:string;

begin

   getmem(fname,256);

   GetModuleFileName( 0,fname,256);

   path:=ExtractFilePath(fname);

   Freemem(fname);

  看是否有设置文件

   fconfig:=Tinifile.create(path+'51delphi.ini');    //in folder

   pathname:=fconfig.readstring('PATH','HTMLROOT',defpathname);

   softpath:=fconfig.readstring('PATH','SOFTPATH',defsoftpath);

   bookpath:=fconfig.readstring('PATH','BOOKPATH',defbookpath);

   // defpathname);

   fconfig.Free;

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

   foot.HTMLFile:=pathname+'/foot.html';

   center.HTMLFile:=pathname+'/center.html';

bookpage.HTMLFile:=pathname+'/book.html';

 

end;

 

在title 的onhtmltag 事件写:

var

   yy,mm,dd:word;

   s,img,rc:string;

   i:integer;

   sip:string;

   allnum:integer;

begin

    // 加入IP 计数

   if  tagstring='getalluser' then

    begin

    sip:=request.RemoteAddr;  //浏览器地址

    with cx do

     begin

        sql.Clear;

        SQL.Add('select max(id)+1 AS NUM  from ip '); //取最大IP 值

        OPen;

        allnum:=fields[0].asinteger;

        sql.Clear;

        SQL.Add('select max(iptime) as iptime from ip where ip='''+sip+'''');  // 是否访问过本站

        OPen;

       if Isempty then

      //没有访问过本站

          begin

           SQL.Clear;

           SQL.Add('INSERT INTO IP (ID,IP ,IPTIME ,IPTYPE )  VALUES (:1,:2, CURRENT_TIMESTAMP, :3)');

           Params[0].AsInteger:=allnum;

           Params[1].AsString:=sip;

           Params[2].asstring:=request.UserAgent;

           try

              ExecSQL;   //写入访问时间

           except

             exit;

           end;

          end

       else

           begin

               //访问过本站

              if (now-fields[0].asdatetime)>(1/12) then

              //是否两小时内访问过

               begin

                SQL.Clear;

                 SQL.Add('INSERT INTO IP (ID,IP ,IPTIME ,IPTYPE )  VALUES (:1,:2,CURRENT_TIMESTAMP, :3)');

                 Params[0].AsInteger:=allnum;

                 Params[1].asstring:=sip;

                 Params[2].asstring:=request.UserAgent;

                 try

                  ExecSQL;

                 except

                   exit;

                 end;  

              end;

           end;

       end;

      ReplaceText:=''; //仅仅是计数,不返回结果

      end;

     if tagstring='curuser' then //显示当前五分钟内在线用户

       begin

         with cx do

           begin

             SQL.Clear;

             SQL.Add(' select count(*) as num from IP');

             SQL.Add(' where  (CURRENT_TIMESTAMP-iptime)<0.005');

             Open;

             ReplaceText:=fields[0].AsString;

           end;

       end;

 

    

    if tagstring='date' then

       begin

           decodedate(date,yy,mm,dd);

           s:='<font color="blue">';

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

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

           s:=s+'</font>';

           ReplaceText:=s;

       end;

     if tagstring='alluser' then //显示访问人次

       begin

           s:='';

           with cx do

            begin

             SQL.Clear;

             SQL.Add('select * from ALLUSER');

             Open;

             allnum:=fields[0].AsInteger;

             SQL.clear;

             sql.Add('select max(ID) from IP');

             open;

             rc:=inttostr(fields[0].asinteger+allnum);

            end;

           img:='';

           if length(rc)<7 then

             begin

               for i:=1 to (7-length(rc)) do

               rc:='0'+rc;

             end;

            for i:=1 to length(rc) do

                 img:=img+'<img src ="/image/'+rc[i]+'.gif" border=0>';

           s:=s+img;

           ReplaceText:=s;

   end;

end;

 

建立显示技术资料事件(http://www.51delphi.com/delphi/book)

在webmodule 里面建立一个action ,name为book,pathinfo 为/book.

建立action 事件:

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

  Response: TWebResponse; var Handled: Boolean);

begin

     Response.content:=title.Content+bookpage.Content+foot.Content;

end;

建立book.html

book.html:

   <title>delphi 窑洞------技术资料</title>

<table align="center"  width="95%">

<td>页数:<#bookpage></td>

 </table> 

<table  align="center"  cellspacing="2" cellpadding="2" border="0"  width="95%" >

 

  <TR align=left vAlign=top>

     <TD align=left background="/image/shadow.gif"

          width=5>&nbsp;</TD>

 

   <td  font class="pt9">

   <#showbook>

   </td></TR>

</table>

 

在 web module 里面放置一个PageProdure , name为 book,

在book 的onhtmltag 里面写以下事件:

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

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

const

  showcount=25;

var

  no:integer;

  s:string;

  where:string;

  stype:string;

begin

  if request.QueryFields.Values['no']='' then

        no:=1

        else

          begin

            s:=request.QueryFields.Values['no'];

            try

              no:=strtoint(s);

            except

               no:=1;

            end;

            if no<1 then no:=1;

          end;

  if TagString='bookpage' then

     begin

         with cx do

           begin

             SQL.Clear;

             SQL.Add('select count(*) from BOOK');

             SQL.Add(where);

             Open;

             ReplaceText:=showpage(fields[0].asinteger,showcount,no,'&type='+stype);

         end;

     end;

   if TagString='showbook' then

      begin

       ReplaceText:=showbook(showcount,no-1,where)

      end;

end;

里面用了一个showpage 函数,该函数为

function Twm.showpage(allnum, showcount, no:integer;stype:string): string;

var

   lt:integer;

   ys:integer;

   i,start:integer;

   s:string;

begin

   start:=(no-1)*showcount;

   lt:=allnum mod showcount;

   ys:= allnum div showcount;

   if lt<>0 then

     ys:=ys+1;

   s:='';

   for i:=1 to ys do

    begin

     if i<>(start div showcount+1) then

         s:=s+' <a href="/delphi'+request.Pathinfo+'?no='+inttostr(i)+stype+'">['+inttostr(i)+']</a>'

       else

         s:=s+' <font color=red><b>'+inttostr(i)+'</b></font>';

    end;

    result:=s;

end;

里面用了一个showbook 函数,该函数为

function Twm.showbook(showcount, no: integer;where:string): string;

var

  s:string;

  wz:integer;

  news,jys:string;

begin

    with cx do

     begin

      SQL.Clear;

      SQL.Add('select num,name,indate,stype,dnum,jy,fsize from book');

      SQL.Add(where);

      SQL.Add('order by indate desc');

      Open;

     s:=gridhead+gridheadline;

     s:=s+gridtdhead+'日期'+gridtdend;

     s:=s+gridtdhead+'名称'+gridtdend;

     s:=s+gridtdhead+'类别'+gridtdend;

     s:=s+gridtdhead+'推荐度'+gridtdend;

     s:=s+gridtdhead+'大小'+gridtdend;

     s:=s+gridtdhead+'下载'+gridtdend;

     s:=s+' </tr>';

     wz:=0;

    while ((wz<showcount) and  (not eof)) do

      begin

         news:='';

      if (now-fieldbyname('indate').AsDateTime)<3 then

        news:='<img src="/image/new9.gif">';

      jys:='<img src="/image/hx.gif"><img src="/image/hx.gif">';

      if fieldbyname('jy').AsInteger>=80 then

          jys:=jys+'<img src="/image/hx.gif">';

      if fieldbyname('jy').AsInteger>=85 then

          jys:=jys+'<img src="/image/hx.gif">';

      if fieldbyname('jy').AsInteger>=90 then

          jys:=jys+'<img src="/image/hx.gif">';

      s:=s+gridline;

      s:=s+gridtdhead+fieldbyname('indate').asstring+gridtdend;

      s:=s+gridtdhead+'<font color="#0000ff">'+news+fieldbyname('name').asstring+'</font>'+gridtdend;

      s:=s+gridtdhead+fieldbyname('stype').asstring+gridtdend;

      s:=s+gridtdhead+jys+gridtdend;

      s:=s+gridtdhead+fieldbyname('FSIZE').asstring+gridtdend;

      if getsession('delphiuser')<>'' then

        s:=s+gridtdhead+'<font color="blue">'+fieldbyname('dnum').asstring+'</font>'

             +'<a href="/delphi/downbook?num='+fieldbyname('num').asstring+ '"><img src="/image/download.gif" border=0> </a> '+gridtdend

       else

        s:=s+gridtdhead+'<font color="blue">'+fieldbyname('dnum').asstring+'</font>'+gridtdend;

 

      s:=s+' </tr>';

      next;

      wz:=wz+1;

    end;

   s:=s+' </table>';

  end;

  result:=s;

 

 

end;

 

好了,编译该工程,然后加载生成的dll,就可以正确显示数据的内容了。

 

本篇文章具体介绍了web module 里面的数据库的读写以及显示,希望可以给大家一点帮助,

谢谢大家的阅读!

                                       Xalion

                                              2002-11-27

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我们都知道,Bolrand 从Delphi 6.0 以后开始支持Apache web 服务器,而Apache 以其出色的性能及安全性是很多人青睐。Apache 现在最新的版本为2.0.43。虽然Borland 今年新出的Delphi 7 开始支持Apache 2.0 版本,但是Delphi 6 只支持Apache 1.x 版本,而Delphi 7 发行的版本也只支持到Apache 2.0.39 版,对Apache 2.0.40 以后的版本无法支持,本文给出了让Delphi 6和Delphi7 支持最新的Apache 2.0.43 的方法。首先让我们对Delphi 7 进行改造,Delphi 7.0 不支持 Apache 2.0.40 以后的版本的原因是Apache 2.0.40 以后版本修改了接口文件,因此要对Delphi 7.0 对应的接口文件进行修改,具体的修改文件为: HTTPD2.pas,修改内容为:打开 HTTPD2.pas (在c: Program Filesorlanddelphi7source Internet下) 修改一下常数: · MODULE_MAGIC_NUMBER_MAJOR = 20020628; { Apache 2.0.40 }· 如果是Apache 2.0.43 的话,应该修改为· MODULE_MAGIC_NUMBER_MAJOR = 20020903; { Apache 2.0.43 } MODULE_MAGIC_NUMBER_MINOR = 0; (* 0...n *)在结构 conn_rec 的定义里加入以下: · ap_conn_keepalive_e = (AP_CONN_UNKNOWN, AP_CONN_CLOSE, AP_CONN_KEEPALIVE);在结构 conn_rec 的定义里替换: · (** Are we still talking? *)· flags: Cardinal;· { The following are in the flags bitset:· unsigned aborted:1;· · (** Are we going to keep the connection alive for another request?· * -1 fatal error, 0 undecided, 1 yes *)· signed int keepalive:2;· · (** have we done double-reverse DNS? -1 yes/failure, 0 not yet,· * 1 yes/success *)· signed int double_reverse:2;· }为: (** Are we still talking? *) flags1: Cardinal; { The following are in the flags bitset: unsigned aborted:1; } (** Are we going to keep the connection alive for another request? * @see ap_conn_keepalive_e *) keepalive: ap_conn_keepalive_e; flags2: Cardinal; { The following are in the flags bitset: (** have we done double-reverse DNS? -1 yes/failure, 0 not yet, * 1 yes/success *) signed int double_reverse:2; }好了,保存这个文件,然后拷入c: Program Filesorlanddelphi7lib 目录。重新编译你的程序,加入相应的Apache 的配置(具体配置方法请参照我以前的文章),启动Apache,打开浏览器。 ok! 没问题了吧!好。现在我们看看怎么在Delphi 6 里面实现Apache 2.0 的支持,实际上很简单,只要把Delphi 7 里面相应的文件拷入 Delphi 6 的LIB 目录就可以了,具体为以下三个文件: ApacheTwoHTTP.pas, ApacheTwoApp.pas, HTTPD2.pas,然后打开你的现有的Apache 1.x 的程序,修改project 上面的 use 部分,把 Apacheapp改为Apachetwoapp,再把下面的ContentType 改为handler,好了,所有手术完成,现在你编译出来的就是支持Apache 2.0.43 的动态共享模块了。注意:以上的修改为非官方修改,不能保证不出问题,请大家慎重处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值