HTML Forms in MUSIC/SP - An Introduction

MUSIC/SP's web server (HTTPD) supports CGI (Common Gateway Interface) processing of HTML forms, using Rexx as the server-side script language. Form field values and various CGI environment items (such as the IP address of the browser client) are made available to the Rexx program as Rexx variables with names such as  ff_fieldname  and  cgi_remote_addr . The Rexx program uses these variables to generate an HTML response that is sent back to the client's browser.

For more info on Rexx, type  help rexx  as a MUSIC/SP command.

As a simple example, we will set up a web form that asks for the user's name, and responds with the current date and time and the client's IP address.

NOTE: In order to run this example on the MUSIC/SP 6.1 Demo system, you need to restore the files from archive  misc4.arc  (and the previous archives), and you must update the Load Library members, as instructed in the description file. If you have the 6.2 Demo system, the required support is already there.

In general, an HTML file to be served by MUSIC/SP's web server is stored in a MUSIC/SP file named  xxx.htm  (the extension  .html  can also be used). The file is stored in subdirectory  http  of the user's library. The corresponding URL is  http://hostname/userid/http/xxx.htm . In this example, we will use $000 as the MUSIC/SP userid.

For this example, here is the contents of MUSIC/SP file  $000:http\testform1.htm  :


<html>
<head>
<title>Test Form 1</title>
</head>
<body bgcolor="#FFFFFF">
<h2>This is a sample form to show CGI processing by the MUSIC/SP
web server</h2>
<form action="/$000/httpexec/testform1.rex" method="post">
Please enter your first name:
<input type="text" name="firstname" size=25 maxlength=50>
<p>
<input type="submit" name="submit" value="Submit">
<input type="reset" name="reset" value="Reset form">
</form>
</body>
</html>

The  <form>  tag should specify  method="post" . The Rexx program that acts as the CGI processor is stored in a MUSIC/SP file named  yyy.rex  in subdirectory  httpexec . The corresponding URL is  http://hostname/userid/httpexec/yyy.rex .

For this example, here is the contents of MUSIC/SP file  $000:httpexec\testform1.rex  :


/inc rexx
/* This is the CGI processor for the sample HTML form
http\testform1.htm */
/inc rexx.forms

/* Initialize the form variables */
ff_firstname=''

/* Get the info from the form */
call load_form_variables

/* Generate the HTML response page */
call send_line '<html>'
call send_line '<head><title>Response</title></head>'
call send_line '<body bgcolor="#FFFFFF">'
call send_line '<h2>Response to HTML form</h2>'
call send_line 'Hello' strip(ff_firstname)
call send_line '<p>The date is' date()
call send_line '<br>The time is' time()
call send_line '<p>Client IP address is' cgi_remote_addr
call send_line '</body>'
call send_line '</html>'
call send_close 0

As you can see, the Rexx program starts with  /inc rexx.forms , which includes the required CGI processing routines. Next it initializes the  ff_fieldname  variables that the HTML form uses, then calls the routine  load_form_variables , which sets the variables. Finally, it calls  send_line  to generate the lines of the HTML response, and ends with a call to  send_close .

The following screen image shows the sample web form as displayed by the user's browser. We have assumed that the client browser is connecting to the MUSIC/SP demo system, running under the Sim390 emulator, on the same machine (localhost), and that the port number the MUSIC/SP web server is listening on is 801 (rather than the standard port 80); this port number is specified in MUSIC/SP file  $tcp:inetd.ports .

testform1.htm browser window

The following screen image shows the response.

testform1 response browser window

For more details about MUSIC/SP's web server and forms handling, see Web Support on MUSIC/SP.