⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch26.htm

📁 好书《C++ Builder高级编程技术》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<p><textarea name="SendData" rows="16" cols="74"></textarea></p>

    <p>Enter More Information</p>

    <p><input type="text" size="76" 
name="SendData"></p>

    <p><input type="submit" name="SendData" value="Submit"></p>

</form>

</body>

</html>

</FONT></PRE>
<P>You can use this code to test the 
<TT>HttpExtensionProc</TT> that mirrors back
the value of <TT>lpbData</TT>. Note that the HTML form contains two fields for entering
text, the first called <TT>SendData</TT> and the second called <TT>MoreData</TT>.
If you have only one field and you 
type <TT>Fast</TT> into it, the DLL would mirror
back the following to the user:</P>
<PRE><FONT COLOR="#0066FF">SendData=Fast&amp;SendData=Submit

</FONT></PRE>
<P>If you have two fields, as in the HTML code shown here, then the following would
be 
mirrored back if the first field contains <TT>Fast</TT> and the second contains
<TT>Loose</TT>:</P>
<PRE><FONT COLOR="#0066FF">SendData=Fast&amp;MoreData=Loose&amp;SendData=Submit

</FONT></PRE>
<P>If you break down this text into three separate 
fields, they would look like this:</P>
<PRE><FONT COLOR="#0066FF">SendData=Fast

&amp;MoreData=Loose

&amp;SendData=Submit

</FONT></PRE>
<P>This text says, in effect, the <TT>SendData</TT> field has the word <TT>Fast</TT>
in it, the <TT>MoreData</TT> 
field has the word <TT>Loose</TT> in it, and the SendData
button is a Submit button.
<H3><A NAME="Heading21"></A><FONT COLOR="#000077">Responding to a Submit Button</FONT></H3>
<P>Assume that you have an HTML form with the code shown in Listing 
26.3.<BR>
<BR>
<A NAME="Heading22"></A><FONT COLOR="#000077"><B>Listing 26.3. The HTML code for
a Web page that uses ISAPI to interact with the user.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">&lt;html&gt;

&lt;head&gt;

&lt;title&gt;Talking ISAPI 
Test&lt;/title&gt;

&lt;/head&gt;

&lt;body bgcolor=&quot;#0000FF&quot; text=&quot;#00FFFF&quot;&gt;

&lt;h1&gt;Talking ISAPI Test&lt;/h1&gt;

&lt;p&gt;Press this button to see more of the simple ISAPI test:&lt;/p&gt;

&lt;form 
action=&quot;/scripts/Books/BUnleash/IsapiTalk.dll&quot;

method=&quot;POST&quot;&gt;

    &lt;p&gt;Enter your name: &lt;input type=&quot;text&quot; size=&quot;20&quot;

    name=&quot;SendName&quot;&gt;&lt;/p&gt;

    &lt;p&gt;&lt;input 
type=&quot;submit&quot; name=&quot;SendName&quot; value=&quot;Submit&quot;&gt;&lt;/p&gt;

&lt;/form&gt;

&lt;/body&gt;

&lt;/html&gt;

</FONT></PRE>
<P>This code will produce a form that contains a text area where the user can enter
his or her name 
and a button called Submit. Given this form, you can expect the <TT>lpbData</TT>
field to contain the following string, assuming the user enters the word <TT>Sammy</TT>
in the name field:</P>
<PRE><FONT 
COLOR="#0066FF">SendName=Sammy&amp;SendName=Submit

</FONT></PRE>
<P>To understand what is happening here, note the <TT>BODY</TT> of the HTML statement
composed on the server as reflected in the following excerpt from the <TT>SetUpResString</TT>

function shown previously:</P>
<PRE><FONT COLOR="#0066FF">`&lt;BODY&gt;lpbData = %s &lt;/BODY&gt;' +

</FONT></PRE>
<P>If you study the code in the <TT>HttpExtensionProc</TT> function, you will see
that it uses the <TT>Format</TT> routine to 
substitute the value of <TT>ECB.lpbData</TT>
for the <TT>%s</TT> variable in the preceding piece of code. (If you don't understand
how <TT>Format</TT> works, see the BCB documentation, or my references to this method
in Chapter 3, &quot;C++Builder and 
the VCL.&quot;)</P>
<P>After you get the information from the form in the <TT>lpbData</TT> parameter,
you can parse it and return information to the user. For example, you could extract
the number 23 from the preceding example and then square it and 
return it to the
user. Doing so would in effect allow you to get information from the user, namely
a number, perform a mathematical action on the number, and then return the result
to the user. This means you're creating dynamic, interactive Web pages 
on-the-fly,
which is the current sine qua non of Internet programming!</P>
<P>Listing 26.4 shows the complete code for a program that will reply to a user who
enters his or her name into a page of a Web browser and submits it to the DLL. The
HTML that 
accompanies this program is shown in Listing 26.5.<BR>
<BR>
<A NAME="Heading23"></A><FONT COLOR="#000077"><B>Listing 26.4. The code for the ISAPITalk
DLL.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////

// 
IsapiTalk.cpp

// Mirror back the information sent to an ISAPI DLL by the server

// Copyright (c) 1997 by Charlie Calvert

//

#include &lt;vcl\vcl.h&gt;

#include &lt;fstream.h&gt;

#include &lt;dir.h&gt;

#pragma hdrstop

#include 
&quot;..\..\utils\Httpext.h&quot;



USERES(&quot;IsapiTalk.res&quot;);



BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer)

{

  pVer-&gt;dwExtensionVersion = MAKELONG(HSE_VERSION_MINOR, HSE_VERSION_MAJOR);

  
strcpy(pVer-&gt;lpszExtensionDesc, &quot;C++ Builder ISAPI DLL&quot;);

  return (TRUE);

};



// Remember: IOStream deals with OS paths, not relative path to server!

BOOL GetResultString(AnsiString &amp;Result, AnsiString Path)

{

  fstream 
InFile;

  AnsiString FileName(Path + &quot;\\BUnleash\\TalkTest\\TalkReply.htm&quot;);

  char ch;

  char S[500];



  InFile.open(FileName.c_str(), ios::in, filebuf::openprot);

  if (!InFile)

  {

    // If we couldn't get file, then what 
directory were we in?

    Result = &quot;&lt;H&gt;Error reading stream!&lt;/H&gt;&quot;;

    FileName = ExtractFilePath(FileName);

    if (chdir(FileName.c_str()) == 0)

    {

      getcurdir(0, S);

      Result += S;

    }

    else

      
Result += &quot;Could not get file, nor find current directory!&quot;;

    return FALSE;

  }

  while (InFile.get(ch))

    Result += ch;

  return TRUE;

}



AnsiString Parse(AnsiString &amp;S)

{

  int i = S.Pos(&quot;&amp;&quot;);

  
S.SetLength(i - 1);

  S = strrev(S.c_str());

  i = S.Pos(&quot;=&quot;);

  S.SetLength(i - 1);

  S = strrev(S.c_str());

  return S;

}



//HttpExtensionProc callback definition

DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK *pECB)

{

  
AnsiString Header(&quot;HTTP/1.0 200 OK\nContent-Type: text/html\nContent-Length:%d\nContent:\n\n&quot;);

  AnsiString ResultString;

  int resultLen;



  char *IsapiLogText = &quot;ISAPI Talk&quot;;

  strcpy(pECB-&gt;lpszLogData,IsapiLogText);

  
pECB-&gt;dwHttpStatusCode = 200;



  // build the HTML result string

  AnsiString S;

  if (pECB-&gt;lpbData != NULL)

  {

    S = (char *)pECB-&gt;lpbData;

    S = Parse(S);

  }

  else

    S = &quot;Error occurred get lpbData field&quot;;

    

  
AnsiString Date(DateToStr(Now()));

  AnsiString Time(TimeToStr(Now()));



  GetResultString(ResultString, pECB-&gt;lpszPathTranslated);

  ResultString = Format(ResultString, OPENARRAY(TVarRec, (S, Date, Time)));

  resultLen = 
ResultString.Length();

  Header = Format(Header, OPENARRAY(TVarRec, (resultLen)));



  pECB-&gt;WriteClient(pECB-&gt;ConnID, ResultString.c_str(), &amp;(DWORD)resultLen, 0);



  return (HSE_STATUS_SUCCESS);

}



int WINAPI DllEntryPoint(HINSTANCE 
hinst, unsigned long reason, void*)

{

  return 1;

} 

</FONT></PRE>
<H3 ALIGN="CENTER"><FONT COLOR="#0066FF"></FONT></H3>
<P><A NAME="Heading24"></A><FONT COLOR="#000077"><B>Listing 26.5. The HTML file,
called TalkReply.htm, used in the reply 
generated by the ISAPITalk DLL.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">&lt;html&gt;

&lt;head&gt;

&lt;title&gt;This is a simple htm file&lt;/title&gt;

&lt;/head&gt;

&lt;body bgcolor=&quot;#0000FF&quot; text=&quot;#00FFFF&quot;&gt;


&lt;h1&gt;Talking HTML Reply&lt;/h1&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Welcome, %s, to the talking HTML file!&lt;/p&gt;

&lt;p&gt;It's remarkable that you should drop by today, %s, at %s (PST), as

I was just thinking about you. &lt;/p&gt;


&lt;/body&gt;

&lt;/html&gt;

</FONT></PRE>
<P>This program allows an HTML browser to work interactively with a Web server. Screen
shots of before and after a query is made are shown in Figure 26.3 and Figure 26.4,
respectively.</P>
<P>When you're 
working with this program, note that I assume you have placed the
<TT>TalkReply.htm</TT> program in the following location relative to the root of
your Web:</P>
<PRE><FONT COLOR="#0066FF">\\BUnleash\\TalkTest\\TalkReply.htm

</FONT></PRE>
<P>The 
<TT>ISAPITalk</TT> DLL might receive the following string from the user who
clicks the Submit button, asking that a number be squared:</P>
<PRE><FONT COLOR="#0066FF">SendName=Sammy&amp;SendName=Submit

</FONT></PRE>
<P><FONT COLOR="#0066FF"><BR>
<A 
NAME="Heading25"></A></FONT><A HREF="26ebu03.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/26/26ebu03.jpg">FIGURE 26.3.</A><FONT
COLOR="#000077"> </FONT><I>Preparing to query the <TT>ISAPITalk</TT> DLL.</I>
<H6></H6>
<P><A NAME="Heading26"></A><A HREF="26ebu04.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/26/26ebu04.jpg">FIGURE 26.4.</A><FONT 
COLOR="#000077">
</FONT><I>The results of querying the <TT>ISAPITalk</TT> DLL.</I></P>
<P>Given this input, the preceding code would return the following string to the
user across the Internet:</P>
<PRE><FONT COLOR="#0066FF">Welcome, Sammy, to the 
talking HTML file!

It's remarkable that you should drop by today, 2/15/97,

at 10:39:29 PM (PST), as I was just thinking about you.

</FONT></PRE>
<P>In short, the user e

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -