e-RPG Supercharged: Free and Easy Web Enablement

ovalscissorsInternet and Web Development

Jul 30, 2012 (5 years and 3 months ago)

389 views

e
-
RPG Supercharged: Free and Easy

Web Enablement with CGIDEV2


Author: Bradley V. Stone


Every month, it seems I receive hundreds of emails from companies and programmers that are at
a crossroads in their Internet development plan. After deciding that movi
ng some or all of their
business to the Web would benefit the company, they now are facing a decision as to which
technology to use. Many times, this means choosing between WebSphere, Net.Data or e
-
RPG
(RPG CGI programming), or a third
-
party package.

In co
nsulting with these companies, I find that these shops are usually in the same situation. They
have two or three salesmen or consultants convincing them WebSphere is the only way to go.
But, they’ve heard of good results using other technologies such as RP
G. And they know RPG
and are comfortable with it. Not to mention their programmers are very versed in RPG, and it has
kept their businesses moving for years and years.

Another factor to take into consideration is that IBM does such a good job promoting the

WebSphere product that I find most iSeries application developers are under the impression that
to do any Web programming, WebSphere is needed. This couldn’t be further from the truth,
especially when using technologies that already exist on your iSeries.

The first thing I do when looking at a Web development plan is to evaluate the position of the
company, its resources, and its needs and goals. This means asking about their shop, what type
of business they are doing currently, what type of applications t
hey have, which of these
applications they need or just want on the Web, what type of hardware resources they have
available, and most importantly, what are the primary programming skill sets of the development
teams.

If this information leads me to decidi
ng that using RPG for Web development is the best starting
point (which it is in 99% of the cases), then I like to start them off by having them do a crash
course on subjects such as HTML, JavaScript and Cascading Style Sheets (CSSs). These are
some of the

root topics people need to understand when starting down the Web path.

The biggest benefit of these technologies is no matter which technology is chosen, the knowledge
of these basic areas will apply to all of them. If you hear about a Web product that cl
aims you’ll
never have to touch these things, I would grow wary. At the very least, you will want to do some
tweaking with your application, and that means being familiar with these technologies.

The next step is to understand what tools are available on t
he iSeries to produce interactive Web
applications. The tools are in the form of system
-
supplied APIs that allow you to generate
dynamic content and present to the user’s Web browser making the request. Other APIs allow
you to read information from Web for
ms so that you can accept input from users, making
applications more user
-
friendly.

Next, I like to explain to the users that while these APIs are useful, using a little bit of ILE
technology to wrap the APIs with easy to use subprocedure interfaces allows

them to be much
more productive. Using API wrappers encapsulates the functionality of a sometimes
-
cumbersome
API into an easy to use function, which makes developing and updating applications much easier.

Finally, I introduce them to a little
-
known gem in

the IBM world known as CGIDEV2. CGIDEV2 is
a package that was originally created by Mel Rothman in 1996 while he was working with the
AS/400 Partners In Development (PID) group. The first version, known as CGIDEV (stands for
CGI Development Tools), was cr
eated simply because Mel saw the need to create easy to use
function wrappers around the CGI programming APIs that were included in the AS/400 operating
system. These wrappers add much more than just basic functionality. The CGIDEV2 package
contains a uniq
ue development environment that takes CGI programming on the iSeries to the
next level.

In 1997 Mel left PID and moved to the eServer Custom Technology Center (eCTC). He then
worked on upgrading and fine
-
tuning the CGIDEV package to what it is today. Suppo
rted
currently by Giovanni Perotti of IBM Italy, CGIDEV2 is one of those rare gems that is created
when people with ideas, know
-
how and enthusiasm pool their efforts to create something based
on need, instead of greed.

Because so many others and I have fou
nd the CGIDEV2 package useful, I thought it would be a
good idea for me to share the basic advantages of this package over normal CGI programming,
or even other technologies. The best way to do explain the advantages is to simply show you the
basic concept
s behind this package, as well as a few examples.

Before you can begin to use CGIDEV2, you need to download the package. Point your browser to
the Easy400 home page, and home page for CGIDEV2, at the following URL:
http://www
-
922.ibm.com


Here you can read information, see demos, and download the CGIDEV2 package. If you’re
wondering what type of software is included in the download, it is fairly simple to explain.
CGIDEV2 is really nothing more than a coll
ection of RPG ILE service programs that let you easily
create dynamic Web content. The service programs contain subprocedures that wrap the
sometimes complex APIs that are required to write and read information to and from a Web
browser or other Web device
.

The biggest benefit of CGIDEV2 is the use of templates. If you have done any CGI programming
in the past with RPG, you probably found that in some cases putting all or most of the HTML
inside of the RPG program is cumbersome. But, using CGIDEV2, you can
create templates that
eliminate the need to put any HTML in your programs. You read that correctly. CGI programs
created using CGIDEV2 contain absolutely no HTML embedded within the RPG source because
of the use of templates.

Instead of trying to explain h
ow this template works, let me show you an example. Figure 1
contains a sample template that is used with CGIDEV2.




/$all

Content
-
type: text/html

<html>

<head>

<title>Business Card for /%lastname%/, /%firstname%/</title>

</head>

<body>

<table cellspacin
g="0" cellpadding="2" border="1">


<tr bgcolor="#FFCCCC">


<td>


<div align="center">Customer First Name</div>


</td>


<td>


<div align="center">/%firstname%/</div>


</td>


</tr>


<tr bgcolor="#FFCCCC">


<td>


<div align="c
enter">Customer Last Name</div>


</td>


<td>


<div align="center">/%lastname%/</div>


</td>


</tr>


<tr bgcolor="#FFCCCC">


<td>


<div align="center">Address1</div>


</td>


<td>


<div align="center">/%address1%/</div>



</td>


</tr>


<tr bgcolor="#FFCCCC">


<td>


<div align="center">Address2</div>


</td>


<td>


<div align="center">/%address2%/</div>


</td>


</tr>


<tr bgcolor="#FFCCCC">


<td>


<div align="center">City, State, Zip</div>



</td>


<td>


<div align="center">/%city%/, /%state%/ /%zip%/</div>


</td>


</tr>


<tr bgcolor="#FFCCCC">


<td>


<div align="center">Phone Number</div>


</td>


<td>


<div align="center">/%phone%/</div>


</td>


</tr>


<tr bgcolor="#FFCCCC">


<td>


<div align="center">Fax</div>


</td>


<td>


<div align="center">/%fax%/</div>


</td>


</tr>

</table>

</body>

</html>





Figure 1 contains sample HTML template used with CGIDEV2



The template shown in

figure 1 may appear to be plain HTML, but if you look closely you will
notice that there are special identifiers known as replacement variables embedded in the HTML.
If you haven’t yet found them, then are the fields that are surrounded by the /% and %/
c
haracters.

You will also notice at the top of the example what is known as a section name. HTML Templates
are divided into "sections" by using the section delimiters. For example, in Figure 1 this template
uses only one section that is named all and delimi
ted by using the /$all identifier located at the
beginning of the data.

You may have already figured out by looking at this example template that CGIDEV2 actually
takes the opposite approach that normal CGI programming does. Normally when you use CGI,
long

HTML strings are built inside the program, and then written out to the browser. So a change
to the HTML requires a change to the program.

On the other hand, with CGIDEV2 you can create HTML templates and then use the CGI
program to replace variables insid
e of that template. This method means that all of the HTML is
stored externally from your CGI program. Because of this, changing the HTML for the dynamically
generated page means simply changing the template.

Which method is better? Well, for most cases yo
u will find the techniques used by CGIDEV2 are
much more convenient. In rare cases, using the normal CGI programming method may be better,
but again, that’s entirely up to the development team to decide.

As far as how the programming interface is used with

CGIDEV2, it is also simple. Figure 2
contains an example of how the basic steps of a program using CGIDEV2 are carried out.


H DFTACTGRP(*NO) BNDDIR('BVSCGIDEV2/CGIDEV2BND')


****************************************************************


* Prototypes


*


****************************************************************


/COPY QCOPYSRC,PROTOTYPEB


****************************************************************

D Path

S

1024

*********
*******************************************************

C

callp

ClrHtmlBuffer

C

eval

Path = '/bvstools/erpg/cgidev2' +

C

'/examples/namecard.html'

C

callp


GetHtmlIfs(Path)

C

callp

updHTMLvar('firstname':'Ellis')

C

callp

updHTMLvar('lastname':'Rory')

C

callp

updHTMLvar('address1':'1456 Northridge Way')

C

callp

updHTMLvar(
'address2':'Apt #2')

C

callp

updHTMLvar('city':'Pismo Beach')

C

callp

updHTMLvar('state':'CA')

C

callp

updHTMLvar('zip':'93449')

C

callp

updHTMLvar('phone':'271
-
555
-
124
7')

C

callp

updHTMLvar('fax':'N/A')

C

callp

wrtsection('all')

C

callp

wrtsection('*fini')

C

setOn

LR



Figure 2 contains the source

for the NAMECARD example CGIDEV2 program


As you can see in the source for the CGI program in Figure 2, if we were to compare it to a
comparable CGI program that doesn’t use CGIDEV2, there is really not much to it.

Program NAMECARD starts out by defining
an H
-
Spec containing compiler directives. Because
we are using CGIDEV2, which is basically a large set of ILE subprocedures contained within an
ILE service program, we tell the compiler to use a binding directory named CGIDEV2BND which
points to the servic
e program used in the CGIDEV2 package.

Next we define a /COPY directive that copies ILE prototype definitions into our program. The
CGIDEV2 package contains a few available source member used for copying prototypes and
variables into our programs that are
used by the package. In this case we only need to specify
the PROTOTYPEB member in the QCOPYSRC source physical file because we are only use a
few of the basic subprocedures.

We start the main portion of the NAMECARD CGI program by calling a subprocedure n
amed
ClrHtmlBuffer, which clears a buffer, or temporary storage, that the CGIDEV2 applications use.
This buffer is used during program execution to load and manipulate the contents of our template,
and finally write the information out to the browser.

The
next subprocedure we call is named GetHtmlIfs. This subprocedure uses a predefined path
to a stream file (defined in the Path variable) in the IFS that contains our template. In this case it
is the template that is shown in Figure 1.

Next there are multipl
e calls to a subprocedure named updHTMLvar. This subprocedure is used
to replace the variables (those fields surrounded by /% and %/ in our template) with actual data.
In this case, you will see that we are using literal text values to keep things simple.
In reality
before you would begin calling the updHTMLvar subprocedure you would most likely retrieve data
from a file and use the data from that file to use as replacement data.

After we have updated our replacement variables, we call the wrtsection subpro
cedure. This
subprocedure is used to write a section of our template to another temporary buffer used to hold
information that is "ready to write to the browser". Remember that in our template example we
defined the entire document as one section named "al
l." So, in this step we are writing the
contents of the entire template to our "ready" or finished buffer.

Finally, we call the wrtsection subprocedure once more, this time using the special value of "*fini"
for the section name. This last call takes the d
ata from the finished buffer and write the data to the
browser.

The results of CGI program NAMECARD can be seen in Figure 3.





























Figure 3 contains
the results of the NAMECARD CGI program



As you can see, producing results using th
e CGIDEV2 package can be a lot simpler than coding
your HTML directly into your RPG programs. It also makes updating the Web page as simple as
changing the template. For example, if you wanted to move the data in the table around or
change any of the text
labels you would simply edit the information in the template. You don’t
need to recompile of the actual CGI program.

It also means that most WYSIWYG HTML editors can be used to create the templates with little
or no modification when complete. I prefer usi
ng editors that don’t try and think they are "smarter
than you" by adding HTML tags that it thinks you forgot. One of my favorites is Macromedia’s
Dreamweaver. One thing to keep in mind is that you probably will need to add the section
name(s) and the HTML

headers (with the 2 new line characters!) to the document when you are
done.

Probably the biggest benefit of using this method is that you can split the development of your
Web application up accordingly within your IT team. If you have a star RPG program
mer and
another person who is excels at designing Web pages, each person can do what he or she is
best at. In the end, you can merge their products into one very clean and informational Web
application!

Please understand that this article only glazes the s
urface of the capabilities of the CGIDEV2
package. In this example, I’m simply showing you how easy it is to write information to a browser.
While this process is normally the most important part of Web application development, you
undoubtly will want to l
et the user interact with the application as well. So you will need to read
information in from HTML forms at one time or another.

The good news is that the CGIDEV2 package also contains many different ways to read
information in from a Web page. Creating
very interactive Web applications can be just as easy or
easier than with traditional CGI.

I have found that the CGIDEV2 package in most cases speeds up and simplifies the development
of Web applications. In certain cases, your applications may be so compl
ex that using CGIDEV2
may hinder the actual development, but remember that you can use any methods you want! You
can mix and match CGIDEV2 with traditional CGI with no consequence. So if you already have a
set of ILE subprocedures for CGI programming, you
can still use them along with those
subprocedures included in the CGIDEV2 package and get the best of both worlds!

If you are looking for more information on the CGIDEV2 package, the homepage for CGIDEV2
(
http://www
-
922.ibm.com/

) is a great start. There you can download the package as well as
installation instructions. The page also has many examples including source code you can use to
get started. Most of these examples are included with the CGIDEV2 pac
kage. So once you have
it installed, you can follow along with everything already on your system.

I also have written a tutorial and training manual specifically for those who are interested in really
getting deep into how CGIDEV2 works and how to use it t
o it’s fullest potential. This manual is
called e
-
RPG Powertools, Stone on CGIDEV2. This fully hands
-
on training manual and tutorial is
available through Rochester Initiative at
Lab400.com

Whether you’re a
n experienced CGI programmer or just a beginner, I believe the CGIDEV2
package will be a huge added benefit to your arsenal of programming resources!


Bradley V. Stone is the author of e
-
RPG Powertools


Stone on CGIDEV2 training course, and
the best sell
ing book e
-
RGP: Building AS/400 Web Applications with RPG as well as the follow
-
up book, e
-
RPG(v2): e
-
Volving RPG Applications for a Connected World. Brad can be reached at
Stone@Lab400.com