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

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

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

660 Project 6 CREATING AN ATL SERVER APPLICATION

m_HttpResponse << szWebsite;

return HTTP_SUCCESS;

}

The method ValidateAndExchange is executed automatically whenever the SRF is displayed. So, all initialization code can be added here. In addition, you can add the code for data transfer between the form and the XML file in this function:

//set the content type

 

 

Y

 

 

L

m_HttpResponse.SetContentType(“text/html”);

 

 

 

F

const CHttpRequestParams& FormFields = m HttpRequest.GetFormVars();

 

M

 

//CHttpRequestParams is a class, which allows storing of name-value pairs like the

//ones returned by a form post or an HT L query string.

E

 

 

szName=FormFields.Lookup(“Name”);

 

 

T

 

 

 

//Lookup

returns the value when

//passed

a name or a key inAa name-value collection.

szEmail=FormFields.Lookup(“Email”);

szWebsite=FormFields.Lookup(“Website”);

szComments=FormFields.Lookup(“Comments”);

Next, a Cookie has to be saved on the client’s machine. ATL Server provides an easy-to-use CCookie class for this. Add the following code to the ValidateAndExchange method:

CString strCookieString; strCookieString.Format(“%s”, szName); //the data you want to store in the cookie

CCookie cookie;

cookie.SetName(“visitor”); //name of the cookie

cookie.SetValue(strCookieString); //Save the data

cookie.SetPath(“/”);

Team-Fly®

CREATING A GUEST BOOK APPLICATION

Chapter 21

661

 

 

 

 

//This sets the URL whose subsets will be able to access the cookie. If set to the root, you can access the //cookie from any page on the site

m_HttpResponse.AppendCookie(&cookie); //Write the cookie.

Finally, you need to save the data in an XML file. To do this, use the SaveInXML() method. The GuestBook.xml file will have the following structure shown earlier in Figure 21-2.

As depicted in Figure 21-2, the root node of the XML file will be guestbook. (You will learn more about XML and nodes in Chapter 22.) Under this root node will be a Guest node for each visitor who creates an entry. Under each Guest node

will be Name, Email, Website, and Comments nodes.

You can create and modify XML documents using various parsers. The present example uses the Microsoft XML parser, because it has become a ubiquitous part of the Microsoft platform. The XML parser exposes the structure of an XML document to programming languages through the XML Document Object Model (DOM). (You will learn more about XML DOM in Chapter 14.) After you open an XML document, you can perform various operations, such as adding nodes to the document, altering the text within the nodes, altering the attributes of a node, and so on.

Here, you will be looking only at adding more Guest nodes to the GuestBook.xml document. To accomplish this task, you need to open the GuestBook.xml document using the parser. For this, you need to obtain the path of the document, which is done using the following code:

CString strPath;

//ATL 7 has a new Cstring class. m_HttpRequest.GetPhysicalPath(strPath); //Retrieve the path of the virtual folder

strPath.Append(“guestbook.xml”);

//Create the complete path for the XML file

Next, an instance of the XML parser is created and the GuestBook.XML document is loaded to it. The DOM exposes a function load, which accepts the path and name of the XML document.

CComPtr<IXMLDOMDocument> ptXMLDOM;

662 Project 6 CREATING AN ATL SERVER APPLICATION

//Create an instance of the XML parser

HRESULT hr=ptXMLDOM.CoCreateInstance(_uuidof(DOMDocument)); if(FAILED(hr))

return;

if(ptXMLDOM.p==NULL)

return;

VARIANT_BOOL bSuccess=false;

CComVariant strP(strPath.AllocSysString()); //Convert the path from CString to Variant type

hr=ptXMLDOM->load(strP,&bSuccess); //Load the XML file.

if(FAILED(hr))

//Check for error

return;

 

After the XML document is loaded, a reference to the root node is to be retrieved. To achieve this, the DOM object exposes a function, selectSingleNode, which enables you to search for a node by its name. This function returns a reference to the node if it exists. All the guest nodes that will be added for each visitor will be appended to the root node, which is guestbook node in GuestBook.xml.

//Remember COM requires strings in the BSTR type CComBSTR bstrRoot(L”guestbook”);

//Search for and initialize the root (<guestbook> node) CComPtr<IXMLDOMNode> ptRootNode; hr=ptXMLDOM->selectSingleNode(bstrRoot,&ptRootNode); if(FAILED(hr))

return;

if(ptRootNode.p==NULL)

return;

The next step is to create a new Guest node. For that you will use the DOM function createNode. This function accepts the type of the node to be created, the name for the node (“Guest” for all nodes in this application), and a variable to store the reference to the newly created node.

CREATING A GUEST BOOK APPLICATION

Chapter 21

663

 

 

 

 

CComBSTR bstrText;

//Create a new Guest node CComPtr<IXMLDOMNode> ptGuestNode;

hr=ptXMLDOM-createNode(CComVariant(NODE_ELEMENT), CComBSTR(“Guest”),NULL, &ptGuestNode);

The following are the steps to create a Name node. You will follow the same steps to create the Email, Website, and Comments nodes.

CComPtr<IXMLDOMNode> ptNameNode;

//Create a pointer to store the reference to the Name node CComPtr<IXMLDOMNode> ptInsertedNode;

hr=ptXMLDOM->createNode(CComVariant(NODE_ELEMENT),

//The syntax requires a dummy node to store the reference for the new node

CComBSTR(“Name”),NULL, &ptNameNode); //Create a node with the name Name

After the Name node has been created, you will have to create a text node, which will contain the name of the visitor. The text node, once created, will be appended to the Name node as a child node. Consider that you need to create a node, under the Guest node, with the following structure:

<Name>John Doe</Name>

The following are the steps required to accomplish this task:

1.Create a node with the name Name.

2.Create a text node with the text John Doe.

3.Append the text node to the Name node.

4.Append the Name node to the Guest node.

To add a new node, you will use the createNode method of the DOM. You can create a new text node using the createTextNode method and append the text node to another node using the appendChild method:

CComPtr<IXMLDOMText> ptTxtNode;

//create a pointer to store reference to a text node

664 Project 6 CREATING AN ATL SERVER APPLICATION

hr=ptXMLDOM->createTextNode(CComBSTR(szName),&ptTxtNode); //Create a text node with the name of visitor as the text

hr=ptNameNode->appendChild(ptTxtNode,&ptInsertedNode); //Append the text node to the Name node

ptGuestNode->appendChild(ptNameNode, &ptInsertedNode); //Append the Name node to the Guest node

Repeat these steps to create the Email, Website, and Comments nodes and add them to the Guest node.

Finally, append the Guest node to the root node and save the contents of the XML DOM into the file using the following code:

ptRootNode->appendChild(ptGuestNode, &ptInsertedNode);

hr=ptXMLDOM->save(CComVariant(strP));

The Guest Book application is now ready for testing and deployment. Once the project has been published, a virtual directory named GuestBook will be created in the inetpub/wwwroot folder of your IIS installation.

Creating the Empty XML File

Create an XML file using any text editor and add the following text to it:

<?xml version=”1.0”?>

<guestbook/>

Save the file as GuestBook.xml.

After you deploy the project, copy the GuestBook.html (created earlier) and GuestBook.xml files to the inetpub/wwwroot/GuestBook folder. Set GuestBook.html file as the default document using the IIS Manager (alternatively, rename the GuestBook.html as Default.htm). Run Internet Explorer and connect to http://localhost/GuestBook and verify the functioning of the Guest Book application.

CREATING A GUEST BOOK APPLICATION

Chapter 21

665

 

 

 

 

Summary

In this chapter, you created a Guest Book application using ATL Server. You learned about the various components of the ATL Server, such as the SRF, Web application DLLs, and the ISAPI extension DLL. You also learned to use these components to create high-performance Web sites and Web applications.

This page intentionally left blank

PARTVIIIProfessional

Project – 7

Project 7

Creating Web

Services