Creating a Request Handler


In this section you ll create a request handler. You ll first examine what it looks like in traditional C++, and then you ll learn how it s implemented using the new Visual C++ attributes.

You can generate a basic request handler by using the ATL Server Project Wizard. Simply deselect the Generate Attributed Code option under the Developer Support Options tab to disable attributed code generation. For the time being, ignore the ISAPI project and focus strictly on the request handler project itself (application DLL).

Your request handler will reside in a DLL. In order to accomplish this, you ll require a certain minimal amount of plumbing code. If you open the .cpp file for your project, you ll see the code used to create the DllMain entry point. In addition to this code, you ll also see the handler map (which we discussed earlier). That s all that s required in the .cpp file. The real application logic infrastructure is waiting for you in the header file.

When you open the header file for your application DLL, the first thing you ll see is the CMyHandler class. This class derives from CRequestHandlerT , which provides some base functionality to your handler, such as SRF support.

In the CMyHandler class that follows you can see that there s a replacement method map. This is identical to your handler map, except that instead of mapping strings to classes, this map maps strings to methods in your class. In this case it maps the string Hello to the method OnHello . If you take a moment to look at the project s SRF file (in Visual Studio .NET you can switch to HTML view using the tabs at the bottom of the window to see the HTML; with the 2003 release it only works in this mode), things will begin to make sense.

Basically, you have an SRF file that looks something like this:

 <html>  {{handler MyHandler.dll/Default}}  <body>    This is a test: {{Hello}}  </body>  </html> 

and a request handler map that looks something like this:

 BEGIN_HANDLER_MAP()    HANDLER_ENTRY("Default", CMyHandler);  END_HANDLER_MAP() 

which combine with your actual request handler to form the following:

 class CMyHandler : public CRequestHandlerT<CMyHandler>  {  public:    BEGIN_REPLACEMENT_METHOD_MAP(CMyHandler)      REPLACEMENT_METHOD_ENTRY("Hello", OnHello)    END_REPLACEMENT_METHOD_MAP()   

So MyHandler.dll is your application DLL. The Default string is mapped to the CMyHandler class, indicating that requests on the SRF page will be handled by methods in this class. Finally, you have the string Hello mapped to the method OnHello , indicating that whenever you see the tag {{Hello}} in the SRF file, you ll call the OnHello() method in the CMyHandler class.

The relationship among the SRF tags, the maps, and the various parts of the request handler should now be clear. To complete the picture, simply implement the OnHello method as follows:

 HTTP_CODE OnHello(void)  {    m_HttpResponse << "Hello World!";    return HTTP_SUCCESS;  } 

If you build this application (note that the build step will also deploy your application) and run it by navigating to http://localhost/My/My.srf , you ll see the following on the Web page:

 This is a test: Hello World! 

You can see that the OnHello method simply spits the string Hello World! into the response buffer returned to the client. Notice that if you use your browser s View Source command, no ATL Server tags are left in the data passed to the client. That s because they re all stripped by the stencil processor on the server side.

There s only one piece of information we still need to cover to help you understand the entire wizard-generated request handler, and that s the ValidateAndExchange method, which we ve overlooked until now.

The ValidateAndExchange method is the method called before your page is rendered (before the stencil processor begins processing your page). This makes it the perfect place for any initialization code, as well as any processing of HTML form or query data (see Chapter 6 for more information on working with this data). In the case of the wizard code, you simply set the content type.

Now you understand the basic parts of an SRF file and a request handler. In the next section you ll learn how all this works with attributes.




ATL Server. High Performance C++ on. NET
Observing the User Experience: A Practitioners Guide to User Research
ISBN: B006Z372QQ
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net