使用delphi 开发 web(二)动态脚本的实现

   看了前面的文章同学,都会认为delphi 开发web比较麻烦,没有PHP 和ASP 方便。

因为每次要改动网页的内容,就要重新编译一次,重新发布一次,这样也太麻烦了。那么我们就

做一个类似PHP 的动态web 服务器吧,一次编译发布后,就不用再改了,网站内容需要变化时,只

需要修改脚本就可以了。

先看看下面的代码:

<%

var

   i:integer;

begin

for i:=1 to 10 do

  print('ok');

%>

 <p> 你好<p>

<%

 end.

%>

非常像PHP 吧,不过语法是Pascal.我们把这个代码保存成test.psp(psp=pascal script page).

那么由于要解释pascal 脚本,我们需要一个pascal 脚本解释器,目前支持delphi 的pascal 脚本解释器

主要有fastscript,pascalscript,tms script 和paxcompiler.我选择使用速度最快的、稳定性最好的paxcompiler.

当然需要把paxcompiler 封装一下,使其可以读入psp 文件并进行解释输出HTML.

unit paxWebScriptPP;

interface

uses
  SysUtils, Classes, HTTPProd , paxWebScripter,PaxCompiler, PaxProgram;

type
  TpaxPageProducer = class(TCustomPageProducer)
  private
    FcompileFile:Tfilename;
    FWebScripter: TpaxWebScripter;
    function GetOnPrint:  TPaxPrintEvent;
    procedure SetOnPrint(const Value:  TPaxPrintEvent );
     function GetOnInclude: TPaxCompilerIncludeEvent;
    procedure SetOnInclude(Value: TPaxCompilerIncludeEvent);

    procedure SetCompileFile(const Value: TFileName);

  protected

  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    function ContentFromStream(Stream: TStream): string; override;

    property WebScripter: TpaxWebScripter read FWebScripter;

    function ContentFromCompileFile:string;
    function CompileToFile(Aoutfilename:Tfilename):string;

  published
    property HTMLDoc;
    property HTMLFile;

    Property CompileFileName:Tfilename read FcompileFile write SetCompileFile;

    property OnPrint: TPaxPrintEvent read GetOnPrint write SetOnPrint;

    property OnInclude: TPaxCompilerIncludeEvent read GetOnInclude write SetOnInclude;

  end;

然后在webbroke 里面根据浏览器发送的请求处理,完成脚本的运行。当然了在系统初始化时先要注册一些

常用的函数和类。

    initialization

    g_UnitList := TUnitList.Create;
    g_UnitList.AddClass(Twm);
    g_UnitList.Sort;
    RegisterUnits(g_UnitList, GlobalImportTable);
  // 以上代码使用于delphi 2010 以后,直接利用delphi 本身的RTTI 功能,注册需要使用的类

  RegisterHeader(0,'function Utf8ToAnsi(const S: String): string;',@utf8toansi);
  RegisterHeader(0,'function myExtractStrings(Separators: Char; Content: string;var Strings: TStrings): Integer;',@myExtractStrings);
  RegisterHeader(0,'function getmin(date1,date2:string):integer;', @getmin);
  RegisterHeader(0,'function getstringbylen(src:string;len:integer):string;',@getstringbylen);
  RegisterHeader(0,'function MD5(const s: string): string;', @MD5);
  RegisterHeader(0, 'function IPValid(ip1,ip2,myip:string):boolean;', @IPValid);
  RegisterHeader(0, 'function Now: TDateTime;', @now);

// 注册自己的过程

 

加入现在URL的为 http://www.51delphi.com/web?path=test

 

处理URL

 procedure Twm.wmWebActionItem1Action(Sender: TObject; Request: TWebRequest;
  Response: TWebResponse; var Handled: Boolean);
var
  path, s, LFilename : string;
  fn: string;
  fnindex: string;
  ts: tstringlist;
  showtime: Boolean;
  istart, iend: LongWord;
  i:integer;
begin
 {$IFDEF INDYSERVER}
    pathname := pathnamefix + pathdelim +
      copy(UnixPathToDosPath(mypath), 2, 100);

{$ELSE}
    pathname := pathnamefix + pathdelim + copy(mypath, 2, 100);
{$ENDIF}

   fnindex := pathname + pathdelim + 'index.html';
   cookpath := webpath + mypath; // web 为路径
   path := Request.QueryFields.Values['path'];

  if path = '' then
    begin
      path := 'index';
      if FileExists(fnindex) then // 有index.html
      begin
         response.ContentStream:=TFileStream.Create(fnindex, fmOpenRead + fmShareDenyWrite);
         Exit;
      end;

    end;

      if path = 'genindex' then // 生成index 页
    begin
      procindex;
      Response.Content := '首页生成成功!';
      Exit;
    end;

    if path = 'prochtml' then // 生成静态页面
    begin
      if Request.QueryFields.Values['file'] = '' then
      begin
        Response.Content := '请输入文件名!';
        Exit;
      end;
      path := Request.QueryFields.Values['file'];
      fn := pathname + pathdelim + path + '.psp';
      if not FileExists(fn) then
      begin
        Response.Content := '文件名不存在!';
        Exit;
      end;
      fn := path;
      prochtml(fn);
      Response.Content := '页面生成成功!';
      Exit;
    end;

   qlist := TClasslist.Create; // 这个是用来在脚本里面实现动态生成Query.
   try

      show.WebScripter.Scripter.Reset;
      show.WebScripter.Scripter.RegisterVariable(0,'request:TWebRequest;',@Request);
      show.WebScripter.Scripter.RegisterVariable(0,'response:TWebResponse;',@Response); //注册request 和response,以便在脚本里面运行。
      show.WebScripter.Scripter.RegisterVariable(0,'wm:Twm;', @self);
      

    fn := pathname + pathdelim + path + '.html';
    if FileExists(fn) then
    begin
       response.ContentStream:=TFileStream.Create(fn, fmOpenRead + fmShareDenyWrite);
      Exit;
    end;

    fn := pathname + pathdelim + path + '.psp';

    if Request.QueryFields.Values['debug'] = 'true' then
      debug := True;
     showtime := False;
    if Request.QueryFields.Values['showtime'] = 'true' then
      showtime := True;

    if not FileExists(fn) then
    begin
      if debug then
      begin
        Response.Content := '找不到你要的文件:' + fn;
        Exit;
      end
      else
      begin
        Response.Content := '找不到你要的文件';
        Exit;
      end;
    end;
    show.HTMLFile := fn;
    if not showtime then
     begin
        Response.Content := show.Content;
    end
    else
    begin
      istart := GetTick;
      s := show.Content;
      iend := GetTick;
      Response.Content := s + '<p>' + IntToStr(iend - istart) + '毫秒<p>';

    end;
  
  finally
    for i := 0 to qlist.Count - 1 do
    begin
      if Twebquery(qlist[i]) <> nil then
        Twebquery(qlist[i]).Free;
    end;
    qlist.Free;
  end;

end;

OK,  大功告成。

以上就实现了脚本的运行,并可以处理request 和response 对象。

 

运行结果如下:

如果大家想体验一下更多的功能和效果,可以访问一下网站

www.xasyu.cn

 

 

 

时间: 2024-09-02 19:41:01

使用delphi 开发 web(二)动态脚本的实现的相关文章

Delphi开发Web Server程序返回图像的方法

Internet/Intranet在九十年代可能是最流行的计算机术语了,不管是计算机行业内的人士还是计算机外的人士,都会使用Internet/Intranet,有的查资料,有的是宣传自己和公司,甚至有许多以前从没有想到用Internet的东西现在也在用Internet来解决, 比如有的程控数字计算机维护用Internet来解决.在这一切应用之中,基于Web Server的应用程序的开发是基本点,但如何开发Web Server的程序呢? Delphi 3是Borland公司1997年推出的可视化.

使用delphi 开发 web(三)动态脚本里面使用数据库

在主程序里面把数据库访问控件设置并注册好,在脚本里面使用数据库就非常简单. 可以类似下面的脚本访问数据库. <%uses SysUtils, Classes;var   aaa,rndnum,bb:string;   i,num:integer; begin  with cx do    begin        sql.clear;        sql.add('select * from soft');        open;      while not cx.eof do     

Delphi开发Web应用程序打印组件

一.慨述 近些年来,随着互联网的普及和推广,传统的单机模式和局域中的c/s模式的应用程序越来越不能满足信息共享的要求.因此,一种新的基于浏览器的b/s的应用程序的开发方式被提了出来.新的开发方案以其客户端的免维护.免配置.程序能根据服务器的信息能够自动更新升级;服务器端多层模式的应有提高处理的效率和安全性越来越被广大的应用程序的开发者所看好.成为应用程序开发的一个新的发展方向.在windows的平台上,人们利用asp来开发服务的显示界面,而用组件来封装商业规则,在各种杂志上利用各种工具进行组件开

使用delphi 开发 web(五)Android 与delphi 服务器交互访问数据库

     近2年,随着智能手机及平板电脑的快速发展,同时伴随着3G 网络的逐渐普及,移动开发越来越受到开发人员的青睐,而谷歌推出的Android 系统以其开放.优秀很快受到开发人员的推崇,很多知名网站都推出了Android 客户端版本,例如腾讯.新浪.淘宝都有对应的版本.同时很多数据库应用程序都从原来的windows 桌面程序延伸到移动设备上,由于移动设备的性能及内存等肯定无法与台式机及笔记本电脑相比,因此访问数据库也不可能像桌面安装各种数据库的客户端,当然目前绝大多数数据库也没有直接运行在移动

使用delphi 开发 web(四)使用ajax 与extjs交互

     随着现在各种js 框架(例如extjs,Jquery)的迅猛发展,很多网站都开始使用这些框架开始设计 web 页面,而且为了提高用户体验,大量使用了AJAX 技术,可以动态实现很多网页内容, 本文就以extjs 的grid 为例,介绍一下使用webbroke的服务器 与js 框架的交互. 先看一下下面的页面: 这个页面动态显示一个人员工资表,并可以实现翻页,这是一个典型的extjs  的grid. 其页面代码如下:  <!DOCTYPE HTML PUBLIC "-//W3C//

用Delphi开发Web服务数据库程序

第一步:编写服务器的应用程序 首先在Delphi的IDE中选择"File|New|Other-",然后在WebServices页面中选择Soap Server Application图标. 然后在New Soap Server Application对话框中选择Web App Debugger executable. 点击OK按钮之后,Delphi会自动生成一个WebModule,在这个Module中会包含三个WebServices的控件. 然后然后在WebServices页面中选择S

javascript开发随笔二 动态加载js和文件_javascript技巧

这时候最好的做法就是按需引入,动态引入组件js和样式,文件load完成后调用callback,运行js.代码还是很简便的 1. 判断文件load完成.加载状态ie为onreadystatechange,其他为onload.onerror 复制代码 代码如下: if(isie){ Res.onreadystatechange = function(){ if(Res.readyState == 'complete' || Res.readyState == 'loaded'){ Res.onrea

最强大的Delphi RIA Web构架群,领略Delphi开发BS的强大威力,Java差远了

问题描述 CBXRIA框架几乎是国产多Delphi层框架中最好的一个,同时体现SmartClient和RIA思想.CBX的Demo服务器换地址了,所以请大家到这个地方体验,CBXRIADemo(需要客户端初始化):有关CBXRIA的评论:刚创建的CBXRIA讨论区CBXRIA案例:欢迎加入最强大的DelphiRIABS构架群,领略Delphi开发BS的强大威力,Java差远了主讨论群:53623431(已满)第二群:16169282(已满)第三群:63756040(已满)第四群:64740999

032_《Delphi下用Intraweb开发WEB程序应用实战(第二版)》

<Delphi下用Intraweb开发WEB程序应用实战第二版> Delphi 教程 系列书籍 (032) <Delphi下用Intraweb开发WEB程序应用实战第二版> 网友(邦)整理 EMail: shuaihj@163.com 下载地址: Pdf 作 者:高勇 内容简介 IntraWeb是Delphi自带的一套Web开发框架,它由Atozed Software公司(http://www.atozedsoftware.com)在2002年制作,并完美的植入Delphi7中.I