Get thee to the WEB with your RPG/400
by dr. G.B. Perotti, iSeries (AS/400), ITALY
gb_perotti@easy400.net


  If you know RPG and you want to develop Web applications you do not need to learn Java, Visual Basic, Perl, Net.Data, or even Visual RPG!

It is about thirty years that RPG is a popular development language for IBM midrange computers. System/3, System/32, System/34, System/38, System/36 and iSeries (AS/400) encompass a full generation of RPG programmers.
No wonder that while Java and Websphere make the news, RPG makes the business. According to a some research (see The Future of RPG, by Jim Utsler, associate editor of AS/400 Magazine, in the 400Resource.com) RPG development is remaining constant even as Java development increases.
However not many RPG programmers know that may easily enter the e-business area just using their current skills and knowledges.
This is what this paper is about.

Common Gateway Interface (CGI)

As any other system participating in the Internet arena, iSeries (AS/400) is subject to some standards. One of these, the Common Gateway Interface (CGI) standard establishes the way a program should be invoked from a remote web browser (Internet navigator), and is fully supported by iSeries (AS/400). This standard allows iSeries (AS/400) programmers to build their own dynamic web pages using their preferred languages: C++, ILE C, ILE COBOL, and ILE RPG.

My experience with RPG CGI

My initial experience with AS/400 RPG CGI is dated Spring 1997. At that time I was desperate looking for some tool to develop dynamic WEB pages on AS/400. After navigating for a while in the Internet, I landed on a site from IBM Rochester PID (Partners In Development, today's PWD) where Mel Rothman used to make available a service program. I found it easy-to-use and very flexible. In a very short time I was able to build an incredible number of web pages. I was so enthusiastic about it, that they had to assign me a WEB site to propagate this method, originally named CGIDEV, now CGIDEV2 .
Today (May 2005) this site
http://www.easy400.net
is very well known world-wide, and accounts for about 20,000 users from some 130 countries (for an uptodate status please see http://www.easy400.net/easy400/shwdowner1.htm).
From here you can download and install on your iSeries (AS/400) the CGIDEV2 engine, along with demos, sample code, tutorials (on RPG CGI, HTML, JavaScript), that enable a quick start on WEB programming.
There are no prerequisites, both on the iSeries (AS/400) and on the RPG programmer. Your OS/400 can be as old as V4R5, your RPG III has no problems to a smooth upgrading to ILE RPG.
This is why our users (the ones who download from us) are simply enthusiatic about this technique.
Besides, everyone registering for downloads is entitled to free remote support from me: an e-mail to gb_perotti@easy400.net stating the problem is enough to start our support.

The CGI cycle

The CGI program cycle is somehow different from what we are used to in normal interactive programming.
The CGI program is transaction-oriented: it receives a request, process it, provides a response to the remote browser and quits, that is: optionally sets on the LR indicator and returns to the caller.
If LR is not raised, that same CGI copy is available for a subsequent call. However, as it could be called by another remote browser, it should re-initialize all its variables. This rises a first problem. At the end or at the beginning of each piece of "conversation", the "memory" of the program is reset. The question would then be, how the CGI can maintain a dialog if it doesn't even remember the previous question.
This is an easy problem, with a standard solution. The way out is to hide (using input fields type hidden), in the HTML response to the browser, some relevant information from the original request, so that in the next request the web browser sends also the information needed by the CGI to resume the dialog.
The degree of complexity is not higher than that of using hidden fields in the DDS (we all have done that for an entire life).

Let us now come to the cycle.
It is important to know that all the I/O operations from/to the remote browser must be performed through calls to HTTP server API's.
(However, with CGIDEV2, you do not even need to know the names of those API's: everything is done by calling simple CGIDEV2 procedures, as explained later on). The program cycle is as follow:
  1. get the input string containing the request (this is in concepts similar to a parameter list)
  2. parse the input string into program variables
  3. override and open database files, if not yet opened
  4. program specific process logic
  5. build the response HTML string
  6. send the response HTML string
  7. optionally close database files
  8. optionally seton LR
  9. return

Rothman's service program

Mel's CGIDEV2 service program enables RPG programmers to develop GCI in a simplified way, while providing enhanced maintenance flexibility.
This comes from two simple facts. By using this service program
  1. you do not have to call the HTTP API's directly, as you can invoke them through simplified calls to Mel's subprocedures. This results into easier coding and about no test requirements as compared to the native API's invocation
  2. in a native CGI you would have to build the response HTML string within your RPG program. In other words, you would merge editing responsibilities (typical of an externally defined device file) into the program coding. This would result into the practical impossibility to maintain that coding in the long range.
    With Mel's service program you are allowed to define your HTML script outside your program, in a source member. You can divide your script in sections (i.e. record formats). Sections usually contain both text and variables. This is very similar to the DDS method, with the advantage that there is no level check.
    Mel's subprocedures exist to
    • load the source member in core (same as opening a display file)
    • substitute the variables in a section
    • write a section to the output buffer
    • send the output buffer to the remote browser
Therefore, with Mel's service programs your CGI's become even easier than traditional 5250 interactive programming.

The whole of my site is based on this technique. Nuts and bolts of Mel's tecnique are explained in detail in my tutorials. This is why there is no need to attend a class to learn this method.
About the easiness and the productivity of it, you may judge by yourself after three days of self training.

A small example

A small example may help in understanding how this CGI technique works.
Let's have a look at the following program which I suggest to use to validate the status of OS/400 HTTP PTFs for CGI support.
A bootstrap HTML (http://www.easy400.net/cgidev2o/hello.htm) displays the following page:

Your first name
Your last name

The HTML of this page contains a form which is used to invoke the CGI program hello1 which will process the input data:

<form method="POST"   action="http://www.easy400.net/cgidev2p/hello1.pgm">
Your first name
  <input type="text" name="cgiinp01"
   size="20" maxlength="40"><br>
Your last name
  <input type="text" name="cgiinp02"
   size="20" maxlength="40"><br>
<center>
<input type="submit" value="Send">
</center>
</form>

In other words, program hello1 will be invoked with the following input string:
firstname=Horst&lastname=Gruber
according to the CGI standard.

Let's now see how program hello1 is developed.

As usual, before writing a program one should care for its externally defined device file (DSPF). If we use Mel's service program, instead of a device file, we must write the response HTML in a source member, as follow:

<as400>all
Content-type: text/html

<HTML><BODY>
HELLO!<br>
your first name is <b>/%FIRSTNAME%/</b> and
your last name is <b>/%LASTNAME%/</b>.
</BODY></HTML>

Please note that the above HTML source contains a single record format (section) named all and two variables: FIRSTNAME and LASTNAME.

Let's now take a look at the source of ILE RPG CGI program hello1:

 
 /copy CGIDEV2/qrpglesrc,hspecs
 /copy CGIDEV2/qrpglesrc,hspecsbnd
 *
 /copy CGIDEV2/qrpglesrc,prototypeb
 /copy CGIDEV2/qrpglesrc,usec
 /copy CGIDEV2/qrpglesrc,variables1
 *=====================================================================
 * PROLOG
 * -Receive input from the remote browser
 * -Use QtmhCvtDb API to parse the input
 *  into fields of an externally defined data structure
 *=====================================================================
 /copy CGIDEV2/qrpglesrc,prolog1
 *=====================================================================
 * MAIN PROCESS LINE
 *=====================================================================
 * Read external html member into memory
C                   callp     gethtml('DEMOHTML':'CGIDEV2':'HELLO1':
C                                     '<as400>')
 * Set html output variables
 *     -first name
C                   callp     updHTMLvar('FIRSTNAME':cgiinp01:
C                             InitHTMLVars)
 *     -last name
C                   callp     updHTMLvar('LASTNAME':cgiinp02)
 * Send just one html output section
C                   callp     wrtsection('all')
 * Send the output buffer
C                   callp     wrtsection('*fini')
 * Back to caller
C                   return
       

At last, here comes the response sent from the program to the remote browser:


HELLO!
Your first name is Horst and your last name is Gruber.


Conclusions

  • CGI (Common Gateway Interface) is a crossplatform standard for the Internet.
    iSeries (AS/400) is fully compliant.
  • CGI bring the lightest load on your HTTP server
  • Mel Rothman's service program easies RPG CGI developments by
    • hiding API complexities
    • separating program logic from output presentation
  • HTML is a very simple, still very powerful script language for WEB presentation.
    You just need a pocket manual to be started.
  • ILE RPG implications are really moderate.
    If you know RPG III, you already know more than 95% of what's needed. The rest you'll learn while delivering production code.
  • There are no prerequisites on your iSeries (AS/400), provided it runs at least OS/400 V4R5M0.
  • Our site
    www.easy400.net is fully dedicated to Mel's service program.
    It provides demonstrations, sample code, tutorials and utilities that you may download at no charge for a fast startup in the WEB area.
This is why you, by choosing this development method, will be already developing WEB pages at full steam in your first week.