Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
180
Добавлен:
24.05.2014
Размер:
26 Мб
Скачать

650 Project 6 CREATING AN ATL SERVER APPLICATION

Besides identifying the technologies required, the team also designed the user interface for the Guest Book application, shown in Figure 21-1.

 

 

Y

 

L

 

F

 

M

 

A

 

E

 

FIGURE 21-1 The Guest Book screen

 

T

 

 

In addition, the team designed the structure of the XML file that will store the data about the visitors, as shown in Figure 21-2.

FIGURE 21-2 The structure of the XML file

Team-Fly®

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.

CREATING A GUEST BOOK APPLICATION

Chapter 21

653

 

 

 

 

2.In the Templates pane of the dialog box, select ATL Server project. Enter the name for the project as GuestBook and select a suitable path for storing the files.

FIGURE 21-3 The settings for creating an ATL Server project

3.Click on the OK button to invoke the ATL Server Project Wizard. The Overview page with the default settings for the project is displayed, as shown in Figure 21-4.

FIGURE 21-4 The Overview page of the ATL Server Project Wizard

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

CREATING A GUEST BOOK APPLICATION

Chapter 21

655

 

 

 

 

FIGURE 21-6 The Server Options page of the ATL Server Project Wizard

session state services enable you to store session data either in an OLEDB-compliant database or an in-memory database. For this project, we will not be changing the default settings.

6.Click on Application Options to view the Application Options page, shown in Figure 21-7.

FIGURE 21-7 The Application Options page of the ATL Server Project Wizard

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

657

 

 

 

 

<head>

</head>

<body>

This is a test: {{Hello}}<br>

</body>

</html>

In the preceding HTML code, the default class in GuestBook.dll is identified as the handler used to render the replacement tags in the file. {{Hello}} is the only replacement tag in the sample created by the wizard. Remember that these tags are replaced at run time by the value returned by the request handler method for this tag.

Next, open the file GuestBook.h. You see that this file contains the declaration for the CGuestBookHandler. This is the default handler for the SRF indicated by the following attribute before the class declaration:

[ request_handler(“Default”) ]

FIGURE 21-8 The Developer Options page of the ATL Server Project Wizard

658 Project 6 CREATING AN ATL SERVER APPLICATION

FIGURE 21-9 The Solution Explorer window

The method, OnHello, which will be invoked from the SRF when the tag {{Hello}} is parsed, is declared as follows:

HTTP_CODE OnHello(void)

{

m_HttpResponse << “Hello World!”;

return HTTP_SUCCESS;

}

The attribute code [ tag_name(name=”Hello”) ] just before the method declaration indicates that this method will be invoked whenever the stencil processor encounters the tag Hello. The output of the method, “Hello World”, will replace the tag at run time.

Modifying the SRF

When a visitor enters data into Art-Shop’s guest book, you would like to thank him or her and add any other appropriate message. For example, you could add the following code within the body of the HTML file:

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)

{