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

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

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

640

 

Project 6

CREATING AN ATL SERVER APPLICATION

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Internet Information Server

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ISAPI DLL

 

 

 

 

 

 

 

 

 

Web

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Request

 

 

 

 

 

SRF

 

 

 

 

application

 

 

 

 

 

 

Web client

 

 

 

 

 

 

 

 

 

 

 

 

 

dispatcher

 

 

 

 

 

 

 

 

 

DLL

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

SRF

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Services

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

handling

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

code

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Y

 

 

 

 

FIGURE 20-1 The architecture of ATL Server

L

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

F

 

 

 

 

 

 

 

 

 

 

 

 

M

 

 

 

 

 

 

 

 

 

 

 

 

Web application DLL. This DLL contains methods that will be called

 

 

 

 

 

 

 

 

 

 

 

 

A

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

from the SRF, such as methods for generating dynamic content and con-

 

 

 

 

 

 

necting to a database.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

E

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

SRFs. Server Response Files (SRFs) are text files containing static

 

 

 

 

 

 

 

 

T

 

 

 

 

 

 

 

 

 

 

 

 

 

 

HTML code for the static portions of the Web page and special tags that invoke request handler methods from within the Web application DLLs. The following is a sample SRF:

<html>

{{handler application.dll/Default}} <head>

</head>

<body>

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

</body>

</html>

As you can see from the preceding code, an SRF is an HTML file with some special tags, which are to be processed by the Web application DLL. This code is discussed in depth next.

Consider the second line of the code:

{{handler application.dll/Default}}

This line specifies the name of the Web application DLL, application.dll, with which the ISAPI DLL interacts to process the requests. The term following the name of the DLL, /Default, indicates the class that contains the request handling methods that are invoked by the SRF to handle client requests. Default here indi-

Team-Fly®

INTRODUCTION TO ATL SERVER

Chapter 20

641

 

 

 

 

cates that the class marked with the default attribute in the application DLL contains the request handling methods.

Next, consider the sixth line of the code:

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

The {{Hello}} tag is a placeholder, which specifies that a request handler method is to be invoked at this point. At run time, this placeholder is substituted by the output of the request handler method.

Reconsider the second line of the code:

{{handler application.dll/Default}}

More than one class in a Web application DLL can have request handler methods, and an SRF can refer to more than a single Web application DLL. Therefore, when the stencil processor parses the code, apart from loading the Web application DLL file, it also looks for a class declared as default to locate the request handler methods. An SRF can refer to request handler methods in more than one Web application DLL. The following code illustrates the method by which to refer to multiple DLLs:

{{handler application.dll/Default}}

{{id=CalObj handler Cal.dll/Date}}

The special tags in the SRF referring to a request handler in a DLL will do so using the id property, as shown here:

{{id=CalObj handler Cal.dll/Date}}

In the preceding code, CalObj is the id property. All tags that refer to request handler methods defined in the Date class of Cal.dll will use the CalObj id. For example,

{{CalObj.DispDate}}

Here, DispDate is a tag within the SRF and is using the CalObj id to refer to a method within the Date class of Cal.dll.

642 Project 6 CREATING AN ATL SERVER APPLICATION

NOTE

You can use the {{include}} command to load another SRF from the current SRF. SRF also supports some of the basic programming constructs, such as if statements and loops:

{{if condition}} <!--HTML code --> {{else}}

<!--HTML code --> {{endif}} {{while}}

<!--HTML code --> {{endwhile}}

You will next look at the request handler that is defined within the Web application DLL.

Request Handlers

As stated earlier, a Web application DLL can have any number of classes, and each of these classes can have any number of request handler methods, but only one of them can be a default class. A class is declared as a default class by using the Default attribute just before the class declaration, as indicated in the following code:

[ request_handler(“Default”) ]

class CATLSample1Handler

{

}

All ATL Server request handler classes have to derive from the class CRequestHandlerT. This class contains methods to access the HTTP request and response streams and methods for initialization and validation. A request stream contains the data sent to the Web server by the client using either the Post or the Get method. The Web server uses the response stream to send data back to the client. The [request_handler(“Default”)] attribute generates an entry in the ATL Server map associating this class as the default handler. An ATL Server map

INTRODUCTION TO ATL SERVER

Chapter 20

643

 

 

 

 

is used to relate the replacement tags in SRFs to their corresponding request handler methods. All requests that don’t have a named handler will be directed to the

default class.

After you enter the placeholders in the SRF files, you will have to create an ATL Server map in the DLL to map a tag with its request handler method. The mapping can be done using MFC-style macros or it can be done using attributes, which the ATL Server Project Wizard also uses by default.

NOTE

You will learn to use the ATL Server Project Wizard in the next chapter.

Here is the code for creating an ATL Server map using macros instead of attributes:

BEGIN_REPLACEMENT_METHOD_MAP(CATLSample1Handler)

REPLACEMENT_METHOD_ENTRY(“Hello”,OnHello)

END_REPLACEMENT_METHOD_MAP()

The following code illustrates the use of attributes to create an ATL Server map:

[ tag_name(name=”Hello”) ] HTTP_CODE OnHello(void)

{

m_HttpResponse << “Hello World!”; return HTTP_SUCCESS;

}

The default handler class created by the wizard will have two functions: Vaidate-

AndExchange and OnHello. The ValidateAndExchange function is called automat-

ically before the processed SRF is displayed and is useful for initializing state variables. If this page is being launched in response to a POST request from an HTML form, you can read the form contents in this function. The library provides the CHttpRequestParams class to read values of form variables, as illustrated in the following code:

const CHttpRequestParams& FormFields = m_HttpRequest.GetFormVars();

szName=FormFields.Lookup(“Name”); //where Name Is the Id of a TextField In an HTML

form

644 Project 6 CREATING AN ATL SERVER APPLICATION

The OnHello method is the one invoked for the replacement tag {{Hello}} in the sample SRF. Similar methods can be added for other user-defined replacement tags:

[ tag_name(name=”Hello”) ] HTTP_CODE OnHello(void)

{

m_HttpResponse << “Hello World!”; return HTTP_SUCCESS;

}

To summarize, the following explains how these components work together in processing a client request:

1.A client requests an SRF from the Web server. The Web server passes the request to the ISAPI extension DLL that is registered for that Web site or virtual directory.

2.The ISAPI extension queues the request on its thread pool. This frees the Web server thread, allowing it to service another client request. This enables you to create scalable Web applications using ATL Server. The ISAPI extension will have an application DLL cache and an SRF cache.

3.A thread opens the SRF and determines which Web application DLL should handle the request, and the corresponding DLL is loaded (if not already available in the cache).

4.The SRF is parsed by a stencil processor, hence, are also called stencil files. As an SRF is parsed, any method calls from the application DLLs are executed and the values returned by these methods replace the placeholders.

5.The content is sent back to the client.

Other Features

The ATL Server library also provides classes for accessing the Crypto API, setting and retrieving cookie values, and OLE-DB templates for data access. Crypto API is a library that enables programmers using tools such as Visual C++ and Visual Basic to include cryptography functionality in their applications. OLE-DB is a low-level data access interface, as well as a specification, that enables users to build applications that can access data from various kinds of data providers,

m_pServiceProvider

INTRODUCTION TO ATL SERVER

Chapter 20

645

 

 

 

 

ranging from text files to RDBMS. In addition, the ATL Server library enables you to store session state data in a database or in a memory database provided by ATL.

Support for storing and retrieving session data can be added by selecting the Session Services check box in the Server Options page of the ATL Server Project Wizard. The wizard adds code to the ISAPI Extension DLL project as well as to the Web application project. The following code will be added to the header file of your Web application. The code will be commented initially. In the code, a reference to the session service object is being obtained. Because all the session service objects implement the ISessionStateService interface, you query the

object for an object implementing this interface.

CComPtr<ISessionStateService> m_spSessionSvc;

CComPtr<ISession> m_spSession;

if (FAILED(m_spServiceProvider->QueryService(__uuidof(ISessionStateService),

&m_spSessionSvc)))

return HTTP_FAIL;

Next, you need to retrieve the session pointer for the session. You can do so using the following code:

HRESULT hr = GetSession(m_spSessionSvc, &m_spSession);

if (FAILED(hr))

return HTTP_FAIL;

After retrieving the session pointer, you can use the SetVariable and GetVariable methods to store and retrieve Session data:

CComVariant varObj;

hr = m_spSession->GetVariable (“LoginName”, &varObj);

One significant advantage of ATL Server is the way in which it handles threading. IIS maintains an internal thread pool, which is used to service requests. When there are more requests than the available threads, IIS queues up the request. If the load is too heavy, it may even reject requests. However, when you use ATL Server, any request for an SRF is passed on to the ISAPI extension, which maintains its own thread pool, thus freeing IIS. The CISAPIExtension class has a function GetNumPoolThreads that can be overridden to change the number of threads in the extension’s thread pool. This function returns an integer indicating the pool size.

646 Project 6 CREATING AN ATL SERVER APPLICATION

The ATL Server Project Wizard can also add support for increasing performance of a site by using caching support. Support can be added to cache pages, data source, or binary data. You have classes, which simplify providing support for sending mail from your Web application. You can add performance counters that allow monitoring of the Web application using Windows System Monitor.

Summary

In this chapter, you learned about the basic features of the new ATL Server library in Visual C++.NET. ATL Server enables you to develop fast and efficient Web applications. It is flexible to use and provides a host of supporting classes for commonly used features of Web applications. In the next chapter, you will learn to create an ATL Server–based Web application.

Chapter 21

Creating a Guest

Book Application

In Chapter 20, you learned the basic concepts of ATL Server. Moving ahead, in this chapter, you’ll learn to create a simple Guest Book application using ATL Server. This project illustrates the use of ATL Server to create dynamic content

in Web pages.

Case Study

The owners of Art-Shop, the art gallery introduced in Chapter 20, have planned to promote their site, art-shop.com, as a community gathering point for all people interested in arts. One of their ideas is to have a guest book in which likeminded visitors and owners of other art-related sites can leave their contact information. By doing so, any art-related site owner can have a link to his or her site placed on the art-shop.com site. Because Art-Shop’s Web site is new, most of the sections are still under construction. The site is built mainly using ASP and HTML, with some sections developed specifically as components for better performance and scalability.

As stated in Chapter 19, the Code-Forge team is handling the development of Art-Shop’s Web site. The same team met to identify the features of the Guest Book application. The subsequent sections take you through the tasks performed by the development team in the various phases of this project.

Project Life Cycle

As discussed in Chapter 8, every project has three main phases in its life cycle: project initiation, project execution, and project deployment. The project assigned to Code-Forge has already been through the project initiation phase. The project is now in the second phase of the life cycle, the project execution phase. Recall that this phase consists of the following stages:

Requirements analysis

Design

Construction

CREATING A GUEST BOOK APPLICATION

Chapter 21

649

 

 

 

 

Integration and testing

User Acceptance test

While the tasks performed in the testing phases (both Integration and User Acceptance tests) of this project are similar to the testing phases of the remaining projects in the book, the other phases vary. Therefore, subsequent sections will discuss only those phases for which the tasks performed vary from one project to another.

Requirements Analysis

In the requirements analysis phase, the Code-Forge team decided to create a Guest Book application that can be used to gather information about the visitors to the site. To decide the features for this application, the team surveyed various other Web sites that provided the same feature. After analyzing the ideas gathered from those surveys, the team came up with a list of requirements that will be addressed by the Guest Book application. Thus, the Guest Book application should do the following:

Facilitate users to enter their name, e-mail address, Web site addressand comments.

Display a Thank You message for visiting the site and a message stating that the visitor’s Web site will be browsed and, if found appropriate, have a link to it added to Art-Shop’s Web site, www.art-shop.com.

Store a cookie on the client’s machine with the visitor’s name.

Store the data entered by the visitor in an XML file, so that no changes to the existing database are required.

Provide an option for the visitor to view a list of previous visitors and their comments.

Design

During this phase, the team identified the technologies to be used to implement the required features. The team decided to use the following technologies:

HTML to design the user interface

XML to store the data collated online

ATL Server library