Step 3 - Adding our own Request Method


So how does it work this far? The URLs we looked at in Step 2 are actually telling your Web Connection server that the request wants to call the HelloWorld method of WebProcess class. The two parameters following the ? on the URL, known as the QueryString, are used for routing the request to the method in your newly created class. Remember, the Wizard generated this stub test method so we can easily test the class before adding our own code as we'll do in a sec.

The process class is the one that actually handles the incoming request. The Wizard generated this class, which is essentially a template to which we can add new methods, for us. The generated class looks like this. Note the HelloWorld method:

************************************************************************ *PROCEDURE WebProcess **************************** LPARAMETER loServer LOCAL loProcess PRIVATE Request, Response, Server, Session, Process STORE .null. TO Request, Response, Server, Session, Process #INCLUDE WCONNECT.H loProcess=CREATE("WebProcess",loServer) loProcess.lShowRequestData = loServer.lShowRequestData IF VARTYPE(loProcess)#"O" *** All we can do is return... WAIT WINDOW NOWAIT "Unable to create Process object..." RETURN .F. ENDIF *** Call the Process Method that handles the request loProcess.Process() RETURN ************************************************************* DEFINE CLASS WebProcess AS WWC_PROCESS ************************************************************* cResponseClass = [wwPageResponse40] && Original generated: [WWC_PAGERESPONSE] ********************************************************************* FUNCTION TestPage() ************************ THIS.StandardPage("Hello World from the WebProcess process",; "If you got here, everything should be working fine.<p>" + ; "Time: <b>" + TIME()+ "</b>") ENDFUNC * EOF WebProcess::TestPage ENDDEFINE

I actually stripped out a couple of things that aren't important right now. Basically what we have here, is our request class with a HelloWorld method that's called when the request hits. Every method that you create can be accessed through the Web page by using either the Method.wp or wc.dll?WebProcess~Method syntax.

Web Connection Version Notice
These samples use the older classic way of doing things in Web Connection which are more low level. These samples include a few Response method functions that are no longer available by default in the wwPageResponse class. To make sure all the demos work correctly add the following at the top of your generated Process class:

*** Use Classic Response object functions for this old class cResponseClass = "wwPageResponse40"

This covers methods Response.FormTextbox, Response.FormHeader etc. which no longer existing with the wwPageResponse class.

Adding a new request handler method

So, to add a new request all we have to do is add a method to this class, so let's give that a shot. Open up WebProcess.prg jump to the bottom of the file just before the ENDDEFINE and add the following code. This request runs a query against a VFP table and displays the data back as HTML:

************************************************************************ * WebDemo :: CustomerList **************************************** FUNCTION CustomerList() *** Run a static query SELECT company, careof as Name, address, phone ; FROM wwDemo\TT_CUST ; ORDER BY Company ; INTO CURSOR TQuery *** Create HTTP Header, HEAD section, HTML/BODY tags and header text Response.HTMLHeader("Customer Data Accces") Response.Write("This demo displays data from a customer file.<p>") loSC = CREATEOBJECT("wwShowCursor") loSC.ShowCursor() *** Write the output from the ShowCursor Object to the Web Response.Write( loSC.GetOutput() ) *** Generate </body></html> Response.HTMLFooter() RETURN

Then run:

http://localhost/CustomerList.wp
http://localhost/WebDemo/wc.dll?WebProcess~CustomerList

Make sure that the Web Connection server is running as before. When you do this you'll get a result like this:

Pretty cool, right? With a couple of lines of code, we've run a request on the server and have displayed the result of a Web query, and we really haven't written any HTML specific code. Note that you can however, write raw HTML code very easily, simply by using the Response.Write() method.

Let's look a little close at what that code does. We start with a plain VFP SQL query that generates a cursor. Next we start to build an HTML document using code. The Response object is used to handle all HTTP/HTML output to the Web server. The Write() method is the lowest level method that sends output directly to the output stream. Above I use the Write method to write out a simple text string that's displayed in the document:

Response.Write("This demo displays data from a customer file.<p>")

All the other methods used in this request are higher level methods that generate compound HTML with single method calls. HTMLHeader() is a routine that creates a typical header for an HTML document. It generates a default HTTP Header, an HTML <HEAD> section containing a title, the <HTML><BODY> tags and some HTML that writes out a string in big text as the display header of the HTML document.

The most interesting code of the request above is the ShowCursor object and calls, which are responsible for taking the currently open cursor or table and rendering it into an HTML document. It basically runs a SCAN loop through the data in TQuery (the open cursor) and generates an HTML table row for every record it finds. The result is the HTML table output above. One line of code can be very productive in generating a lot of HTML - ShowCursor() is great for quickly prototyping data output and with some query manipulation can be used for almost all list display purpose. More on this in a moment.

HTMLFooter() then closes out the HTML document by putting end </body></html> tags at the bottom.

It's important to understand at this point that Web Connection offers a number of ways to generate output. This example uses a handful of high level methods to perform all the output generation. As we'll see later on, you can generate HTML in a number of other ways as well.

The next step will be to add user input to your request so the request becomes truly dynamic.


  Last Updated: 2/11/2008 | © West Wind Technologies, 2008