The ValidateAndExchange Method


The first opportunity to access your user s input is in the CRequestHandlerT::ValidateAndExchange method. This method is called by ATL Server when a request is first received and before the stencil processor begins processing your SRF file. You should override this method and add logic to process your user s input. You ll look at two examples that use your request handler s m_HttpRequest member to access your user s input. First consider the example in Listing 6-2.

Listing 6.2: Display All Input Values in ValidateAndExchange
start example
 1 HTTP_CODE ValidateAndExchange()  2 {  3  // Set the content-type  4  m_HttpResponse.SetContentType("text/html");  5  6  if (m_HttpRequest.GetMethod() == CHttpRequest::HTTP_METHOD::HTTP_METHOD_GET)  7  {  8    // we are using GET  9    LPCSTR  name(NULL);  10   LPCSTR  value(NULL);  11  12    POSITION pos = m_HttpRequest.GetFirstQueryParam(&name, &value);  13    while (pos)  14    {  15      m_HttpResponse << name << " = " << value << "<br>";  16      pos = m_HttpRequest.GetNextQueryParam(pos, &name, &value);  17    }  18  }  19  else  20  {  21    // we are using POST  22    LPCSTR  name(NULL);  23    LPCSTR  value(NULL);  24  25    POSITION pos = m_HttpRequest.GetFirstFormVar(&name, &value);  26  27    while (pos)  28    {  29     m_HttpResponse << name << " = " << value << "<br>";  30     pos = m_HttpRequest.GetNextFormVar(pos, &name, &value);  31    }  32  }  33  34 return HTTP_SUCCESS;  35 } 
end example
 

This is an example of a trivial implementation of ValidateAndExchange . If you re using Visual Studio .NET, you ll notice that the Visual C++ ATL Server Project Wizard will generate a ValidateAndExchange method that s similar. Let s take a closer look at this example line-by-line :

  • Line 4: This line isn t directly related to what we discuss in this chapter, but it s worthwhile having a quick look at it. Before sending any data back to your client, you have to set the content type. This tells the client how the client should display the data that you send. In this case, you re simply setting the content type to HTML text.

  • Line 7: GetMethod() returns the method that was used to submit the data. You should recall that POST means that your data is in form variables , and GET means that your data is in a query string. You need this information so that you know where to look for your data. In practice, you should make sure that your application works with both GET and POST .

  • Lines 9 and 10: Declare variables to hold the name and value of your input data. Recall that your user s input will be sent in name/value pairs (e.g., fruit=apple ).

  • Line 12: If you re using GET , you can get your first query string parameter by calling GetFirstQueryStringParam() . You can then use the return value of this method in subsequent calls to get other query string parameters.

  • Line 13: pos will be NULL when there are no query string parameters.

  • Line 15: You can use the m_HttpResponse member to send data back to your client. The > > operator for CHttpRequest is overloaded to handle most common data types and convert them to strings.

  • Line 16: GetNextQueryStringParam will advance your iterator to the next query string parameter.

  • Lines 22 and 23: Here you declare variables to hold the name and value of your input data. Recall that your user s input will be sent in name/value pairs (e.g., input1=Hello World! ).

  • Line 25: If you re using POST , you can get your first form variable by calling GetFirstFormVar() . You can then use the return value of this method in subsequent calls to get other form variables.

  • Line 29: You can use the m_HttpResponse member to send data back to your client. The > > operator for CHttpRequest is overloaded to handle most common data types and convert them to strings.

  • Line 30: GetNextFormVar() will advance your iterator to the next form variable.

  • Line 34: You ll return success; this will tell ATL Server to process the SRF file. Returning an error ( HTTP_S_FALSE ) from ValidateAndExchange is the fastest way to tell ATL Server to stop processing the current request.

Now you ve seen the easiest way to iterate over the values that your user has entered. Let s quickly look at another example in Listing 6-3.

Listing 6.3: Display a Specific Input Value in ValidateAndExchange
start example
 1  HTTP_CODE ValidateAndExchange()  2  {  3     // set the content type  4     m_HttpResponse.SetContentType("text/html");  5     const CHttpRequestParams *requestParams(NULL);  6  7     // determine what method our form is using  8     if (m_HttpRequest.GetMethod() == CHttpRequest::HTTP_METHOD::HTTP_METHOD_POST)  9     {  10      // using POST  11      requestParams = &(this->m_HttpRequest.GetFormVars());  12     }  13     else  14     {  15       // using GET  16       requestParams = &(this->m_HttpRequest.GetQueryParams());  17     }  18  19     LPCSTR value(NULL);  20     value = requestParams->Lookup("fruit");  21  22     if (value)  23     {  24       m_HttpResponse << "The fruit you chose was: " << value;  25     }  26     return HTTP_SUCCESS;  27 } 
end example
 

This example is very similar to the previous example, so we just focus on the differences in the following list, rather than looking at the code line-by-line:

  • Line 5: You declare a pointer to your user s input. You don t know yet whether this data will be sent as a query string or as form variables, but in either case, this data will be exposed as a CHttpRequestParams object.

  • Line 11: If you re using POST , then GetFormVars will return a pointer to your data.

  • Line 16: If you re using GET , then GetQueryParams will return a pointer to your data.

  • Line 20: Call Lookup to get the value of an input parameter named fruit .

  • Line 24: If there s a value for the fruit parameter, then output its value using m_HttpResponse .

Now you ve seen the first two basic ways of accessing your user s input. Either method is fine for this simple case, but you may have noticed that some functionality is lacking, for example:

  • There s no way to set constraints on the values of your data.

  • All the data is handled as strings ”there s no data type conversion done for you.

  • There s no way to keep track of which parameters are missing and which are valid.

Once again, ISAPI developers will probably be able to remember having to write tedious and error-prone code to implement this sort of functionality. In the next few sections, you ll look at how ATL Server addresses these issues and makes your life as a Web developer much easier.




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