Mega Code Archive

 
Categories / Delphi / Examples
 

Making an active web-page, part 1

This code example demonstrates how to make an active webpage using Delphis Web-dispatcher. To run this example you'll need a Web-server. I recommend Apache (Win 95, 98, ME, 2000, NT), OmniHttpD (Win 95, 98) or MicroSoft Personal WebServer. They are all freeware servers that seems to work fine. I'm using an Apache-server running on a 2000-machine. The important thing is that the server can handle CGI-scripts (most servers can). Well lets get into the subject. We're gonna make a simple name-input page. First we start with the HTML-code for our inputform. It should look something like this: <HTML> <HEAD> <TITLE>CGITest</TITLE> </HEAD> <BODY> <P><FONT FACE="Arial"><B><FONT SIZE="+2">CGITest</FONT></B></FONT></P> <P><FONT FACE="Arial"><B><FONT SIZE="+2"></FONT></B> ;;;</FONT></P> <FORM ACTION="/cgi-bin/test.cgi/nametest" METHOD="POST"> <TABLE WIDTH="50%"> <!-- Text1 input --> <TR> <TD WIDTH="70">Name:</TD> <TD WIDTH="300"><P> <INPUT TYPE="TEXT" NAME="Text1"></P> </TD> </TR> <!-- Select1 input --> <TR> <TD WIDTH="70">Gender:</TD> <TD WIDTH="300"><P> <SELECT NAME="Select1" SIZE="1"><OPTION>Male </OPTION> <OPTION>Female </OPTION></SELECT> </P> </TD> </TR> <!-- Text2 input --> <TR> <TD WIDTH="70">Adress:</TD> <TD WIDTH="300"><P> <INPUT TYPE="TEXT" NAME="Text2"></P> </TD> </TR> <!-- Text3 input --> <TR> <TD WIDTH="70">ZIP:</TD> <TD WIDTH="300"><P> <INPUT TYPE="TEXT" NAME="Text3"></P> </TD> </TR> <!-- Text4 input --> <TR> <TD WIDTH="70">Town:</TD> <TD WIDTH="300"> <P><INPUT TYPE="TEXT" NAME="Text4"></P> </TD> </TR> <!-- Text5 input --> <TR> <TD WIDTH="70">Country:</TD> <TD WIDTH="300"><P> <INPUT TYPE="TEXT" NAME="Text5"></P> </TD> </TR> </TABLE> <P><INPUT TYPE="SUBMIT" NAME="Submit1"></P> </FORM> </BODY> </HTML> In a WebBrowser this code will look like this: The most important tags for this example to function is the FORM tags: <FORM ACTION="/cgi-bin/test.cgi/nametest" METHOD="POST"> and </FORM> All inputs made between these two tags, will be sent to the server and handled by the test.cgi code.Test.cgi is the response-code we're going to make with Delphi. Start Delphi and select File / New / Web Server Application. Select :CGI stand-alone executable and Delphi presents a WebModule for us to work with. Select: Project / Options / Application and write "cgi" into Target file extension. Select: Project / Options / Application / Directories/Conditionals and write your servers cgi-bin library into Output Directory Save the project as "test.dpr" First we're going to produce the input form: Put a Pageproducer from the palettegroup Internet on the WebModule, and paste the HTML-code above into the PageProducers HTMLDoc-property. The WebModule is a dispather, which can have several actions. Lets make the first action called: input . Right-click on the WebModule and select: Action Editor Select: Add New Write: "/input" into the Action:s PathInfo - property Select: Events and double-click in the OnAction - Event This will put us in the code, and now we'll have to write something. In this event we just want to show the inputform to user. We'll use the PageProducer:s Content-property for this. The code would be: Response.Content:= PageProducer1.Content; Compile the code and test if it works. Put: "http://<your servername>/cgi-bin/test.cgi/input" into your browser. Did it work ? At least it did for me. Well, lets continue with the other action named: "nametest". Right-click on the WebModule and select: Action Editor Select: Add New Write: "/nametest" into the Action:s PathInfo - property Select: Events and double-click in the OnAction - Events Back in the actioncode again. Lets say we want the response to look like this: Your name is <name>. You are a <male/femal>. Your address is:< address> Time is: <current time> Who asked about time? - I just put it in to show you that this page is living. As you can see in the actions procedure declaration, there is an object called: Request. That's where the inputforms parameters could be found. Looking at: Request.ContentFields.Values['Text1'] for example, would return the users name. Well lets make the code: Put another PageProducer on the WebModule Put this code into the action procedure: procedure TWebModule1.WebModule1WebActionItem2Action(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: boolean); var Name, Gender, Time: string; Address: TStringList; begin Address:= TStringList.Create; try try // Extract the input-parameters from Request with Request.ContentFields do begin Name:= Values['Text1']; Gender:= Values['Select1']; Address.Add(Values['Text2'] + '<BR>'); Address.Add(Values['Text3'] + '<BR>'); Address.Add(Values['Text4'] + '<BR>'); Address.Add(Values['Text5'] + '<BR><BR>'); Time:= DateTimeToStr(Now); end; // Make the header PageProducer2.HTMLDoc.Clear; PageProducer2.HTMLDoc.Add('<HTML><HEAD><TITLE>CGITest</TITLE></HEAD><BODY>' + '<P><FONT FACE="Arial"><B><FONT SIZE="+2">CGITest</FONT></B></FONT></P>' + '<FONT FACE="Arial"><FONT SIZE="-1">'); // Make the response HTML PageProducer2.HTMLDoc.Add('<BR><BR>Your name is ' + Name + '<BR>You are a ' + Gender + '<BR>Your address is:<BR>' + Address.Text + 'Time is: ' + Time); // Make the footer PageProducer2.HTMLDoc.Add('</HEAD><HTML>'); //Respond Response.Content:= PageProducer2.Content; except // If something went wrong, take care of it on E: Exception do Response.Content:= 'Error Message: ' + E.Message; end; finally Address.Free; end; end; Compile the code and test if it works. Put: "http://<your servername>/cgi-bin/test.cgi/input" into your browser.