📄 cb200006ne_f.asp.htm
字号:
<HTML>
<HEAD>
<TITLE>Call It WebBuilder</TITLE>
</HEAD>
<BODY>
<TABLE border=0 width="100%" cellpadding=0 cellspacing=0>
<TR valign=top>
<TD width="100%">
<p class=ColumnTitle><font size="2">On the
'Net</font> </p>
<p class=ColumnSubtitle>Internet
/ Web Applications / CGI / HTML / C++Builder 4, 5</p>
<p class=BodyText> </p>
<p class=Byline>By Natalia
Elmanova, Ph.D. </p>
<p class=BodyText> </p>
<p class=StoryTitle><font size="2"><b>Call It
WebBuilder</b></font></p>
<p class=StorySubtitle><font size="2">Creating
Web Applications with C++Builder 4 and 5</font></p>
<p class=BodyText> </p>
<p class=BodyText> Web
applications (scripts) are executables or libraries operated by Web servers.
Their function is to generate HTML pages dynamically in response to user
requests, which will then be rendered by a Web browser. </p>
<p class=BodyText> </p>
<p class=BodyText> Using
the Internet means accessing resources available on the Internet. These
resources are identified by URLs (Uniform Resource Locators), which must be
typed into the URL edit control of a browser, or selected by clicking on a
hyperlink. Text or HTML documents, Java applets, ActiveX controls, and other
files, are examples of Internet resources. The result of executing an application
operated by a Web server can also be a resource. Such an application can
process parameters contained in the user request. If such a resource is
available, and browser security settings allow its use, this resource is
interpreted by a browser. </p>
<p class=BodyText> </p>
<p class=BodyText> Creating
Web applications is possible with virtually any development tool. The only
requirement is that the application be able to run in the operating system that
operates the Web server. Creating such applications, however, requires writing
code. Choosing a suitable development tool for creating Web applications means
minimizing the time it takes to write code. C++Builder 4/5 is a good choice in
this case, because it provides good visual tools for designing such
applications. </p>
<p class=BodyText> </p>
<p class=Subheads>How to
Create a Simple Web Application</p>
<p class=BodyText> To begin
creating a Web application, first select the File | New option from the C++Builder IDE
menu, and click the Web Server Application icon (see Figure 1). </p>
<p class=BodyText> </p>
<p class=Captions><img width=250
height=206 src="images/cb200006ne_f_image002.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image002.gif"> <br>
<b>Figure 1:</b>
Select Web Server Application from the New Items dialog box. </p>
<p class=BodyText> </p>
<p class=BodyText> Then
select an application type (CGI or Win-CGI executable, or ISAPI/NSAPI DLL, an
extension of Microsoft Internet Information Server or Netscape FastTrack). CGI
(Common Gateway Interface) script requires a separate process to run, while an
ISAPI/NSAPI DLL is executed inside the Web server process. ISAPI/NSAPI DLLs
require fewer resources than CGI script. In addition, a DLL remains in server
memory after its loaded, thus reducing the response time for user requests.
However, this feature prohibits debugging the DLL, because after changing its
code, you need to restart the Web server. Therefore, a good solution is to
create a CGI application, debug it, then turn it into an ISAPI/NSAPI DLL. </p>
<p class=BodyText> </p>
<p class=BodyText> So,
let's choose the CGI Stand-alone executable option, and create a Win32 console
application for HTML document generation. As a result, we obtain a <i
style='mso-bidi-font-style:normal'>TWebModule</i> object, as shown in Figure 2. </p>
<p class=BodyText> </p>
<p class=Captions><img width=179
height=121 src="images/cb200006ne_f_image003.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image003.gif"> <br>
<b>Figure 2:</b>
The <i>TWebModule</i> object. </p>
<p class=BodyText> </p>
<p class=BodyText> How does
the Web application work? Generally, the Web server receives a request from the
browser, meeting CGI specifications. The Web server can call the Web
application as necessary to process the request. If the request is correct, the
Web application processes it and generates a result, i.e. an HTML document that
can be sent by the Web server to the browser. The HTTP port is used for data
exchange between Web servers and Web browsers. </p>
<p class=BodyText> </p>
<p class=BodyText> When the
C++Builder Web application receives an HTTP request, it creates a <i
style='mso-bidi-font-style:normal'>TWebRequest</i> object to represent this
request, and a <i>TWebResponse</i> object to
represent an HTML response to the request (see Figure 3). </p>
<p class=BodyText> </p>
<p class=Captions><img width=250
height=119 src="images/cb200006ne_f_image005.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image005.gif"> <br>
<b>Figure 3:</b>
The Web application structure. </p>
<p class=BodyText> </p>
<p class=BodyText> <i>TWebModule</i> contains a set of <i
style='mso-bidi-font-style:normal'>TWebActionItem</i> objects, which store
rules for processing different types of user requests. After recognizing a
request, it chooses the necessary <i>TWebActionItem</i>
object, and executes its <i>OnAction</i>
event handler. This event handler contains code for processing the request, and
generating a response that will be sent to the Web browser by the Web server. </p>
<p class=BodyText> </p>
<p class=BodyText> To
create our first <i>TWebActionItem</i>,
right-click on the <i>TWebModule</i>, and
select the Action Editor item from the pop-up menu. Then press the Add
button in the Action
Editor form. After that, we must set up the <i>PathInfo</i>
and <i>Default</i> properties of the <i
style='mso-bidi-font-style:normal'>WebActionItem1</i> object. The <i
style='mso-bidi-font-style:normal'>PathInfo</i> property is a part of the URL,
the full Internet path of the resource (see Figure 4). </p>
<p class=BodyText> </p>
<p class=Captions><img width=250
height=45 src="images/cb200006ne_f_image007.jpg" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image007.jpg"> <br>
<b>Figure 4: </b>The
parts of the URL. </p>
<p class=BodyText> </p>
<p class=BodyText> The <i
style='mso-bidi-font-style:normal'>Default</i> property indicates whether this
Web action will execute if the <i>PathInfo</i>
of the request is empty (see Figure 5). </p>
<p class=BodyText> </p>
<p class=Captions><img width=250
height=105 src="images/cb200006ne_f_image009.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image009.gif"> <br>
<b>Figure 5:</b>
The Action Editor. </p>
<p class=BodyText> </p>
<p class=BodyText> Now we
can create the <i>OnAction</i> event handler
for the <i>TWebActionItem</i> component: </p>
<p class=BodyText> </p>
<p class=Code><span class=Code><b>void</b> <b
style='mso-bidi-font-weight:normal'>__fastcall</b>
TWebModule1::WebModule1WebActionItem1Action(</span></p>
<p class=Code><span class=Code> TObject *Sender, TWebRequest *Request,
TWebResponse *Response, </span></p>
<p class=Code><span class=Code> <b> bool</b>
&Handled) </span></p>
<p class=Code><span class=Code>{</span></p>
<p class=Code><span class=Code> AnsiString cont =
AnsiString("<HTML><BODY><H3>Hello!</H3>"); </span></p>
<p class=Code><span class=Code> cont = cont +
AnsiString("<BR>"); </span></p>
<p class=Code><span class=Code> cont = cont + AnsiString("<H2>Now
is ") + TimeToStr(Time()) +</span></p>
<p class=Code><span class=Code> AnsiString(" </H2>"); </span></p>
<p class=Code><span class=Code> cont = cont +
AnsiString("</BODY></HTML>"); </span></p>
<p class=Code><span class=Code> Response->Content = cont; </span></p>
<p class=Code><span class=Code>}</span></p>
<p class=BodyText> </p>
<p class=BodyText> This
event handler generates an HTML page. Figure 6 shows the source. </p>
<p class=BodyText> </p>
<p class=Captions><img width=250
height=28 src="images/cb200006ne_f_image011.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image011.gif"> <br>
<b>Figure 6:</b>
The source of a dynamically generated HTML page. </p>
<p class=BodyText> </p>
<p class=BodyText> Now we
can compile and save our executable in a script directory of our Web server, a
special place to store Web server executables. In the case of Microsoft
Internet Information Server (IIS), the directory is C:\Inetpub\scripts by
default. Now we can test our Web application by entering its URL. Pay attention
to the fact that the user of the browser cannot - and should not - see a full
structure of the server directories. He or she must input their aliases instead
of the real names of directories. This is necessary to provide data security
for a server computer; otherwise, any external user can find out about the
existence of files in a server PC outside the Web server directories. </p>
<p class=BodyText> </p>
<p class=Captions><img width=250
height=236 src="images/cb200006ne_f_image013.jpg" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image013.jpg"> <br>
<b>Figure 7:</b>
The Project Options dialog box. </p>
<p class=BodyText> </p>
<p class=BodyText> Another
important note: Before compiling, you must uncheck the Use
dynamic RTL option in
the Linker page of the Project Options dialog box (see Figure 7). It's also
better to uncheck the Build with runtime packages option of the Packages page (or
place them in the same directory). The reason is the same: You can run this
executable from a command prompt without any problem, and it can access RTLs
and packages. But, generally, they're located outside the Web server
directories, so the Web server cannot access them. This results in error
messages during attempts to run this application by means of a Web browser and
Web server: </p>
<p class=BodyText> </p>
<p class=Code><span class=Code>CGI Error</span></p>
<p class=Code><span class=Code> </span></p>
<p class=Code><span class=Code>The specified
CGI application misbehaved by not returning a complete set of HTTP headers. The
headers it did return are: ... </span></p>
<p class=BodyText> </p>
<p class=BodyText> If we
press the Reload button of the browser a few times, we can see that the time
value shown in the browser changes. It means that this page is created
dynamically (see Figure 8). </p>
<p class=BodyText> </p>
<p class=Captions><img width=250
height=136 src="images/cb200006ne_f_image015.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image015.gif"> <br>
<b>Figure 8:</b>
A dynamically generated HTML page. </p>
<p class=BodyText> </p>
<p class=BodyText> Of
course, we can create an ISAPI DLL from this application. To do it, we must
create a new ISAPI DLL, delete its Web module, and add another one from the
previous project. </p>
<p class=BodyText> </p>
<p class=Subheads>Creating
Forms and Processing User Input</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -