Using Delphi to develop Web (2) Dynamic scripts

Source: Internet
Author: User

After reading the previous article, I Think That Delphi is difficult to develop Web, without the convenience of PHP and ASP.

Because every time you change the content of a webpage, you have to re-compile and re-publish it. This is too much trouble. Then we will

Make a dynamic Web Server similar to PhP. After a compilation and release, you don't need to change it any more. When the website content needs to change

You can modify the script.

Let's take a look at the following code:

<%

VaR

I: integer;

Begin

For I: = 1 to 10 do

Print ('OK ');

%>

<P> Hello <p>

<%

End.

%>

It is very similar to PhP, but the syntax is Pascal. We save this code as test. psp (PSP = Pascal script page ).

To interpret the Pascal script, we need a Pascal script interpreter. Currently, the Pascal script interpreter of Delphi is supported.

Fastscript, pascalscript, TMS script, and paxcompiler are the most efficient and stable paxcompiler.

Of course, you need to encapsulate paxcompiler so that it can read the PSP file and interpret and output 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;

In webbroke, the script is run according to the request sent by the browser. Of course, you must register some

Common functions and classes.

Initialization

G_unitlist: = tunitlist. Create;
G_unitlist.addclass (TWM );
G_unitlist.sort;
Registerunits (g_unitlist, globalimporttable );
// The code above is used after delphi 2010 and uses the rtti function of Delphi to register the class to be used.

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

// Register yourself

 

Add a http://www.51delphi.com/web to the current URL? Path = test

 

Processing 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; // The web is the path.
Path: = request. queryfields. Values ['path'];

If Path = ''then
Begin
Path: = 'index ';
If fileexists (fnindex) Then // There are index.html
Begin
Response. contentstream: = tfilestream. Create (fnindex, fmopenread + fmsharedenywrite );
Exit;
End;

End;

If Path = 'genindex' then // generate the index page
Begin
Procindex;
Response. Content: = 'homepage generated successfully! ';
Exit;
End;

If Path = 'proptml' then // generate a static page
Begin
If request. queryfields. Values ['file'] = ''then
Begin
Response. Content: = 'enter the file name! ';
Exit;
End;
Path: = request. queryfields. Values ['file'];
FN: = pathname + pathdelim + path + '. psp ';
If not fileexists (FN) then
Begin
Response. Content: = 'the file name does not exist! ';
Exit;
End;
FN: = path;
Prochtml (FN );
Response. Content: = 'page generated successfully! ';
Exit;
End;

Qlist: = tclasslist. Create; // This is used to dynamically generate a query in the script.
Try

Show. webscripter. scripter. Reset;
Show. webscripter. scripter. registervariable (0, 'request: twebrequest; ', @ request );
Show. webscripter. scripter. registervariable (0, 'response: twebresponse; ', @ response); // register the request and response for running in the script.
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: = 'the file you want cannot be found:' + FN;
Exit;
End
Else
Begin
Response. Content: = 'file not found ';
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) + 'millisecond <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.

The above code runs the script and can process the request and response objects.

 

The running result is as follows:

If you want to experience more functions and effects, visit the website.

Www.xasyu.cn

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.