73 lines · 2.7 KB · Text
RawDownload
Example 1: A simple HTML form and a CGI program in Moscow ML------------------------------------------------------------ An HTML form has an associated action, which is usually a call to aCGI program, such as 	ACTION="http://www.dina.kvl.dk/cgi-bin/sestoft/cgiex1" When the user submits the form, the webbrowser will send a request tothe indicated webserver (www.dina.kvl.dk).  The server will invoke theCGI program WWWSERVERPREFIX/cgi-bin/sestoft/cgiex1 to handle therequest (the WWWSERVERPREFIX is an installation-specific path;typically /var/lib/httpd).  To send a form from the client (browser) to a WWW server, and make theserver respond properly, you need to write the following components: 	(A) An HTML page containing a FORM	(B) A CGI program to receive the form, process it, and respond  (A) You need an HTML page containing a FORM.  The FORM must have 	- METHOD="POST" 	- an ACTION equalling the URL of a CGI program	- any number of named INPUT, SELECT, or TEXTAREA fields	- one or more INPUT fields with TYPE=SUBMIT See file `htmlform.html' for an example HTML form.  With that form,you can type in a text in a TEXTAREA (on the system running thebrowser) and choose whether you want to count the number of charactersin the textarea, count the number of lines in the textarea, or sortthe lines of the textarea. Note that in htmlform.html, the textarea is named "texttoprocess" andthe select menu is named "action". (B) You need a CGI program See file `cgiex1.sml' for an example CGI program.  That program willhandle requests sent from the above-mentioned example form.  Withinthe example CGI program, the contents of the HTML form's textarea isretrieved as	Mosmlcgi.cgi_field_string "texttoprocess"and the chosen select menu option is retrieved as	Mosmlcgi.cgi_field_string "action"  INSTRUCTIONS FOR USE (1) Edit the FORM in file `htmlform.html' so that ACTION refers to acgi-bin directory on your local webserver. (2) Compile the CGI program:    mosmlc -c cgiex1.sml   mosmlc -o cgiex1 cgiex1.uo (3) Install the compiled CGI program on your local webserver:    cat `which camlrunm` cgiex1 > /var/lib/httpd/cgi-bin/sestoft/cgiex1   chmod a+x /var/lib/httpd/cgi-bin/sestoft/cgiex1 The above assumes that your webserver lives in /var/lib/httpd/ so thatyour CGI program directory is /var/lib/httpd/cgi-bin/sestoft/, andthat you are allowed to install CGI programs on your webserver.Change the path as appropriate.  (Note that the runtime system issimply prepended to the ML bytecode, so that the webserver need notlook for it.) The included Makefile automates steps (2) and (3) above. (4) Open the HTML file `htmlform.html' with your webbrowser, fill inthe form, and click on `Send'.