📄 cb200006ne_f.asp.htm
字号:
<p class=BodyText> </p>
<p class=Code><span class=Code><form
method="POST" </span></p>
<p class=Code><span class=Code> action="http://elmanova/scripts/formproc.exe/t3"> </span></p>
<p class=BodyText> </p>
<p class=BodyText> The <i
style='mso-bidi-font-style:normal'>action</i> parameter contains a URL for the
resource, which should be requested by pressing the Submit button. In this case, it's the same application, but with <i
style='mso-bidi-font-style:normal'>PathInfo</i> assigned the value "/t3". </p>
<p class=BodyText> </p>
<p class=BodyText> Save and
compile this project. After filling in this form and pressing the Submit button, we obtain a dynamically generated page whose content uses
the data of the form (see Figure 14). </p>
<p class=BodyText> </p>
<p class=Captions><img
width=250 height=131 src="images/cb200006ne_f_image025.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image025.gif"><br>
<b>Figure 14:</b> The result of processing the user
entry. </p>
<p class=BodyText> </p>
<p class=BodyText> Of
course, we can create an ISAPI DLL using this application. To do so, we must
create a new Web server application, delete its Web module, and add another one
from the previous project. </p>
<p class=BodyText> </p>
<p class=Subheads>
Using Databases in Web
Applications</p>
<p class=BodyText> We can
insert code for adding records with user data to the database table to <i
style='mso-bidi-font-style:normal'>WebModule1WebActionItem3Action</i> (and thus
create a simple mailing list). To do this, we can add some data access
components to <i>WebModule1</i>, and bind
them to each other by setting the appropriate properties (see Figure 15). </p>
<p class=BodyText> </p>
<p class=Captions> <img
width=221 height=169 src="images/cb200006ne_f_image026.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image026.gif"><br>
<b>Figure 15:</b>
<i>TWebModule</i> with DataAccess components
for collecting user entries. </p>
<p class=BodyText> </p>
<p class=BodyText> Now, we
can rewrite the third <i>OnAction</i> event
handler: </p>
<p class=BodyText> </p>
<p class=Code><span class=Code><b>void</b> <b
style='mso-bidi-font-weight:normal'>__fastcall</b>
TWebModule1::WebModule1WebActionItem3Action(</span></p>
<p class=Code><span class=Code> TObject *Sender, TWebRequest *Request, </span></p>
<p class=Code><span class=Code> TWebResponse *Response, <b>bool</b> &Handled) </span></p>
<p class=Code><span class=Code>{</span></p>
<p class=Code><span class=Code> AnsiString CustName =
Request->QueryFields->Values["T1"] +</span></p>
<p class=Code><span class=Code> Request->ContentFields->Values["T1"]; </span></p>
<p class=Code><span class=Code> AnsiString CustEmail =
Request->QueryFields->Values["T2"] +</span></p>
<p class=Code><span class=Code> Request->ContentFields->Values["T2"]; </span></p>
<p class=Code><span class=Code> Table1->Open();</span></p>
<p class=Code><span class=Code> Table1->Append();</span></p>
<p class=Code><span class=Code> Table1->FieldByName("Name")->Value = CustName; </span></p>
<p class=Code><span class=Code> Table1->FieldByName("E-mail")->Value = CustEmail; </span></p>
<p class=Code><span class=Code> Table1->Post();</span></p>
<p class=Code><span class=Code> Table1->Close();</span></p>
<p class=Code><span class=Code> Response->Content =
PageProducer2->Content();</span></p>
<p class=Code><span class=Code>}</span></p>
<p class=BodyText> </p>
<p class=BodyText> Now our
application adds records with user data to <i>Table1</i>.
Compile, save, test, and close the project. </p>
<p class=BodyText> </p>
<p class=BodyText> That was
a simple example of using databases. More often, it's interesting to present
the data from databases in a browser. For this purpose, we can use the <i
style='mso-bidi-font-style:normal'>TDataSetTableProducer</i> component, which
is responsible for obtaining data from <i>TDataSet</i>
components and presenting them in the form of HTML tables. </p>
<p class=BodyText> </p>
<p class=BodyText> To do
this, we can use the DB Web Application Wizard from the New Items dialog box,
as shown in Figure 16. </p>
<p class=BodyText> </p>
<p class=Captions> <img
width=250 height=206 src="images/cb200006ne_f_image028.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image028.gif"><br>
<b>Figure 16:</b>
Selecting the DB Web Application Wizard from the New Items dialog box. </p>
<p class=BodyText> </p>
<p class=BodyText> This
wizard will display a series of dialog boxes similar to those of the Database
Form Wizard. We must fill them in, choosing the database, the table, and the
fields to publish (see Figure 17). </p>
<p class=BodyText> </p>
<p class=Captions> <img
width=250 height=161 src="images/cb200006ne_f_image030.jpg" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image030.jpg"> <br>
<b>Figure 17:</b>
Selecting the fields to represent in a browser. </p>
<p class=BodyText> </p>
<p class=BodyText> After
filling in all the forms, we'll obtain a WebModule with Table (or Query),
Session, and DataSetPageProducer components. This is enough to create a simple
Web application, like the one shown in Figure 18. </p>
<p class=BodyText> </p>
<p class=Captions> <img
width=250 height=208 src="images/cb200006ne_f_image032.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image032.gif"><br>
<b>Figure 18:</b>
The result of publishing data in the Web. </p>
<p class=BodyText> </p>
<p class=BodyText> This
table (biolife.db from the BCDEMOS database) contains a graphic field, and it
would be interesting to view it in a table. How can we do this? </p>
<p class=BodyText> </p>
<p class=BodyText> One
solution would be to create a calculated field that contains an appropriate
HTML tag with a reference to the image obtained from this graphic field. So
let's create a new calculated string field for <i>Table1</i> that will contain a reference to the image that was
previously extracted from the database. We also need to take into account that
there could be several users that call our Web application simultaneously, so
we should prevent downloading image files produced by another user. One
mechanism for this would be to give unique names to image files. For example,
the <i>OnCalcField</i> event handler for the
<i>Table1</i> component can be the
following: </p>
<p class=BodyText> </p>
<p class=Code><span class=Code><b>void</b> <b
style='mso-bidi-font-weight:normal'>__fastcall</b>
TWebModule1::Table1CalcFields(</span></p>
<p class=Code><span class=Code> TDataSet *DataSet) </span></p>
<p class=Code><span class=Code>{</span></p>
<p class=Code><span class=Code> TDateTime DT = Now();</span></p>
<p class=Code><span class=Code> Word hour, min, sec, msec; </span></p>
<p class=Code><span class=Code> DecodeTime(DT, hour, min, sec, msec); </span></p>
<p class=Code><span class=Code> <i> <span Class=CodeBlue>// Create a unique ID based on current time. </span></i></span></p>
<p class=Code><span class=Code> UserID = IntToStr(hour) + IntToStr(min) +</span></p>
<p class=Code><span class=Code> IntToStr(sec) + IntToStr(msec); </span></p>
<p class=Code><span class=Code> <i> <span Class=CodeBlue>// Create a unique image name. </span></i></span></p>
<p class=Code><span class=Code> <b> int</b>
i = random(1000000000); </span></p>
<p class=Code><span class=Code> AnsiString FileName = UserID + "_"
+ IntToStr(i) + ".JPG"; </span></p>
<p class=Code><span class=Code> <i> <span Class=CodeBlue>// Save an image to a file. </span></i></span></p>
<p class=Code><span class=Code> TJPEGImage *JPG = <b>new</b> TJPEGImage; </span></p>
<p class=Code><span class=Code> TPicture *P = <b>new</b> TPicture();</span></p>
<p class=Code><span class=Code> P->Assign(Table1->FieldByName("Graphic")); </span></p>
<p class=Code><span class=Code> JPG->Assign(P->Graphic); </span></p>
<p class=Code><span class=Code> JPG->SaveToFile(FileName); </span></p>
<p class=Code><span class=Code> Table1Picture->Value = "<IMG
SRC='" + FileName + "'>"; </span></p>
<p class=Code><span class=Code> &P->Free; </span></p>
<p class=Code><span class=Code> &JPG->Free; </span></p>
<p class=Code><span class=Code>}</span></p>
<p class=BodyText> </p>
<p class=BodyText> We must
also refer to the appropriate header file: </p>
<p class=BodyText> </p>
<p class=Code><span class=Code><span Class=CodeGrn>#include
<vcl\jpeg.hpp> </span></span></p>
<p class=BodyText> </p>
<p class=BodyText> This
code supposes that any two users cannot extract the same image file from the
database during the same millisecond. In this case, we can generate the image
file name combined from a millisecond-based "timestamp" and a random value that
is unique for this user session, but unfortunately is not unique for several
users. </p>
<p class=BodyText> </p>
<p class=BodyText> Why is
the image file not deleted in this event handler? Image files can be downloaded
for a long time - especially if the user of the application uses a dial-up
connection. This means that these files must stay on the Web server hard drive,
even when the Web application has finished its job. Therefore, they should be
deleted by another application, or, if you have created the only application
for your Web server, by another instance of the same application that is
running later. This example shows how, assuming that ten minutes is enough time
to download all the necessary images: </p>
<p class=BodyText> </p>
<p class=Code><span class=Code><b>void
__fastcall</b>
TWebModule1::WebModuleCreate(TObject *Sender) </span></p>
<p class=Code><span class=Code>{</span></p>
<p class=Code><span class=Code><i><span Class=CodeBlue> // Delete old
*.JPG files</span></i></span></p>
<p class=Code><span class=Code> TSearchRec sr; </span></p>
<p class=Code><span class=Code> <b> if</b>
(FindFirst("*.JPG", faAnyFile, sr) == 0) </span></p>
<p class=Code><span class=Code> { </span></p>
<p class=Code><span class=Code> <b> do</b></span></p>
<p class=Code><span class=Code> { </span></p>
<p class=Code><span class=Code> <b> if</b>
(Double(Now()) - Double(FileDateToDateTime(sr.Time)) </span></p>
<p class=Code><span class=Code> > 0.00694) </span></p>
<p class=Code><span class=Code> <i> <span Class=CodeBlue>// Ten minutes is approximately</span></i></span></p>
<p class=Code><span class=Code><i><span Class=CodeBlue> // a
0.00694 part of a day</span></i></span></p>
<p class=Code><span class=Code> DeleteFile(sr.Name); </span></p>
<p class=Code><span class=Code> } </span></p>
<p class=Code><span class=Code> <b> while</b>
(FindNext(sr) == 0); </span></p>
<p class=Code><span class=Code> FindClose(sr); </span></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -