Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
CREATING A GUEST BOOK APPLICATION |
Chapter 21 |
651 |
|
|
|
|
|
Construction
During this phase, the Code-Forge team will construct the application. The primary tasks that the team needs to accomplish are the following:
Create an HTML form-based interface
Create an ATL Server application
Create an XML file with the structure shown in Figure 21-2 Each of the above tasks is discussed in detail in the following sections.
Creating the HTML Form-Based
Interface
The opening page of the Guest Book application is an HTML form-based page (refer to Figure 21-1). This page has the text fields necessary to gather the visitors’ details and a Submit button, which the user can click on to save the data. This section describes what you need to do to create the HTML form-based interface.
Create an HTML page and name it GuestBook.html. Add the following code to the file:
<html>
<body>
<form NAME=”GBfrm” method=”post” action=”http://localhost/GuestBook/GuestBook.srf”>
The action tag specifies that when the user clicks on the Submit button, the GuestBook.srf file will be launched. Here, it is assumed that the ATL Server that you will create later in this chapter will be named GuestBook. When this project is built and deployed, a folder named GuestBook will be created under the inetpub/wwwroot folder of the local IIS installation, and the SRF created will have the same name as the project.
The following is the code for the rest of the HTML file. Here the various controls of the form are created.
<table ID=”GuestBookfrm” CELLSPACING=”2” CELLPADDING=”7”>
<CAPTION>Please enter your details</CAPTION>
<tr VALIGN=”top”>
652 Project 6 CREATING AN ATL SERVER APPLICATION
<td><label for=”Name” ACCESSKEY=”N”><U>N</U>ame: </label></td> <td><INPUT TYPE=”text” NAME=”Name” ID=”Name”></td>
</tr>
<tr VALIGN=”top”>
<td><label for=”Email” ACCESSKEY=”E”><U>E</U>mail: </label></td> <td><INPUT TYPE=”text” NAME=”Email” ID=”Email”></td>
</tr>
<tr VALIGN=”top”>
<td><label for=”Website” ACCESSKEY=”W”><U>W</U>eb Site: </label></td> <td><INPUT TYPE=”text” NAME=”Website” ID=”Website”></td>
</tr>
<tr VALIGN=”top”>
<td><label for=”Comment” ACCESSKEY=”c”><U>C</U>omment: </label></td> <td><TextArea NAME=”Comment” ID=”Comment”> </TextArea></td>
</tr>
<tr><td> </td></tr>
<tr VALIGN=”top”> <td></td>
<td><center><INPUT TYPE=”submit” NAME=”Submit” ID=”Submit” value=”Submit”></center></td>
</tr>
</table>
</form>
Save the HTML file. This file needs to be copied to the inetpub/wwwroot/GuestBook folder once the ATL server is built and deployed.
Creating the ATL Server Application
To create an ATL Server project, you need to perform the following steps:
1.Start Visual Studio .NET and, in the Project Types pane of the New Project dialog box (see Figure 21-3), select Visual C++ Projects.
654Project 6 CREATING AN ATL SERVER APPLICATION
4.Click on Project Settings to open the Project Settings page of the wizard, shown in Figure 21-5. You can use this page to customize your project.
FIGURE 21-5 The Project Settings page of the ATL Server Project Wizard
The Project Settings page gives you the option of creating a Web application DLL and/or an ISAPI extension DLL. The Web application DLL will contain the request handlers for the SRF files to be serviced. The virtual root will be the folder under inetpub/wwwroot (the default Web site folder of the local IIS installation) where the DLL files and the SRF files will be deployed after each build. Deploying involves copying the respective files and registering the DLLs.
5.Click on Server Options to view the Server Options page, shown in Figure 21-6.
This page shows all services that can be provided by the ISAPI extension DLL. Based on the service required, you can select the appropriate check box. The wizard automatically generates the code required to implement that service to the ISAPI extension DLL file. The various caching features help to increase the performance of a Web application. The predefined performance counters enable you to evaluate performance of your Web application using the performance monitor. The
656 Project 6 CREATING AN ATL SERVER APPLICATION
When you check the Validation support option, the wizard adds the ValidateAndExchange method to the default request handler class. Similarly, when you check the Stencil processing support option, the wizard adds the sample code that illustrates the usage of attributes to handle requests.
7.Click on Developer Support Options to open the Developer Support Option page, shown in Figure 21-8. This page has options to add comment entries indicating the task to be done, add support for attributes, and add support for customized assert and trace handling. If you are not familiar with attributed coding, which is new in Visual C++ .NET, and prefer not to use it, you can deselect the Attributed code check box. However, remember that attributed coding is a feature that simplifies C++ coding a lot. For example, consider the code for mapping a request handler to the default {{Hello}} tag added to the SRF file created by the wizard. Without the attributed code, you would have to use the following code to create the method map:
BEGIN_REPLACEMENT_METHOD_MAP(CGuestBookHandler)
REPLACEMENT_METHOD_ENTRY(“Hello”, OnHello)
END_REPLACEMENT_METHOD_MAP()
The equivalent code using attributed coding will be this simple line:
[tag_name(name=”Hello”) ]
8.Click on Finish. Figure 21-9 displays the Solution Explorer window with all the files created by the wizard.
Understanding the GuestBook Project
As Figure 21-9 shows, the Solution Explorer displays a list of files created for the GuestBook project and the GuestBookISAPI project. The code in the GuestBook.srf file will be discussed first.
In the Solution Explorer window, double-click on GuestBook.srf to open it. You can see that the SRF file contains the markup for a simple HTML file:
<html>
{{// use MSDN’s “ATL Server Response File Reference” to learn about SRF
files.}}
{{handler GuestBook.dll/Default}}
CREATING A GUEST BOOK APPLICATION |
Chapter 21 |
659 |
|
|
|
|
|
<CENTER>
Thank you {{Name}} for your comments. We will take a look at your website {{Website}} and, if appropriate, add a link to it from our site. <br><br>
Click <a href=”Guestbook.xml”>here</a> to view a list of other visitors to this site.
</CENTER>
In the preceding code, {{Name}} and {{Website}} are tags that will be replaced at run time with corresponding entries from the Guest Book page. Also, a link is added to the XML file where the list of visitors is maintained.
Form Handling
The data entered by the visitor has to be retrieved from the form and stored in an XML file. For this, declare the following variables as private class members in the CGuestBookHandler class in the GuestBook.h file:
LPCSTR szName;
LPCSTR szEmail;
LPCSTR szWebsite;
LPCSTR szComments;
Adding Methods to the Default Handler
The SRF that you modified earlier has two replacement tags; hence, two handler methods should be added to the CGuestBookHandler class. Add the following code to the GuestBook.h file:
[ tag_name(name=”Name”) ] HTTP_CODE OnName(void)
{
m_HttpResponse << szName; return HTTP_SUCCESS;
}
[ tag_name(name=”Website”) ]
HTTP_CODE OnWebsite(void)
{
