Developing web programs with C + + Builder

Source: Internet
Author: User
Tags bool html page http request requires microsoft frontpage

Web applications are executable programs or dynamic link libraries that run on the server side. They can respond to user requirements, dynamically generate hypertext pages, and provide information to the client browser.

Because the standards of Web applications are not the same, and the program is written in a variety of different directions, developers are not bothered by the fact that developers are less likely to know how to write each standard. and C + + Builder can solve this problem very well. C + + Builder The development of Web applications into components, allowing developers to face a consistent development interface, using consistent development principles, the only difference being the program structure that was chosen when the program was started, as well as the details and manner in which the program was written. This article introduces the specific steps of developing a Web application using C + + Builder through two examples.

A simple Web program

First, click on the C + + Builder IDE menu item file| New option, select the Web Server application icon.

Because the CGI (Common Gateway Interface) runtime requires a separate process, and the ISAPI/NSAPI Dynamic link Library runtime is mapped to a Web server process, ISAPI/NSAPI requires fewer resources than CGI. However, this feature of ISAPI/NSAPI dynamic link Library has caused some difficulties for debugging programs, so it is better to create a CGI program and debug it before converting it into a isapi/nsapi dynamic link library. So choose "CGI standalone executable" here to generate a Twebmodule object.

The Web application is actually an extension of the functionality of the Web server, just as Windows applications are functionally extensible. When a Web application retrieves an HTTP request message from a Web server, it analyzes the HTTP request message, generates an HTML page to be passed to the Web server, and is passed to the customer by the Web server. A key component of C + + Builder Web applications is Web Module, which collects and manages a set of Twebactionitem objects that describe HTTP request messages with Twebrequest objects. And according to the HTTP request message to assign one of the actions to respond to the customer's request, is to fill in the Twebresponse object's content characteristics.

A Web application can create several action items for assignment by the Web Scheduler (twebdispatcher). C + + Builder is a specialized action item editor (action Editor) to create and manage action items. Right-click the Web module and select Action Editor from the menu that pops up. An action item is then added, with its PathInfo property to set the entry path of the action item on the Web server, and the default property sets whether the action item executes when the PathInfo property is empty.

Write the following code for the action item:

void __fastcall TWebModule1::WebModule1WebActionItem1Action(TObject *Sender, TWebRequest *Request, TWebResponse *Response,bool &Handled)
{
AnsiString cont = AnsiString("<HTML>
<BODY><H3>Hello!</H3>");
cont = cont + AnsiString("<BR>");
cont = cont + AnsiString("<H2>Now is") + TimeToStr(Time()) +AnsiString("</H2>");
cont = cont + AnsiString("</BODY>
</HTML>");
Response->Content = cont;
}

In the handle to the OnAction event that handles an action item, you can access the client's request message through the requests parameter. To respond to a customer's request, you actually assign the HTML-described page to the response content attribute, and the Web Scheduler automatically passes the corresponding results to the Web server, which is passed to the customer by the Web server.

Now that a simple Web application has been created, you can test it through a Web browser (the running interface is shown in Figure 3). Note that the path to the Web application should have executable permissions.

Web programs that handle user input

On the basis of the above example, you continue to create a Web program that handles user input. Add an action item Twebactionitem. Join the Tpageproducer object in WebModule1 to generate an HTML document using its prepared HTML template.

First, use Microsoft FrontPage to do a user input table (HTML code) that is run as shown in Figure 5.

Write the following code for the TWEBACTIONITEM2 onaction event:

void __fastcall TWebModule1::WebModule1WebActionItem2Action(TObject *Sender, TWebRequest *Request, TWebResponse *Response,bool &Handled)
{
Response->Content=PageProducer1->Content();
}

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.