Get Thee to the Web, Part 3
September 22, 2010 Paul Tuohy
In this final article, we will compare and contrast the scripting solutions available with CGIDEV2, PHP, and Java. Here are a few questions to keep in mind when considering which scripting language to use:
CGIDEV2 is a toolkit (service program) of functions that provide assistance with handling browser input and output, using externally describing HTML, data handling and validation, error handling and reporting, debugging, and other miscellaneous functions.
A common mistake people make with CGIDEV2 is thinking that it is CGI programming. It isn’t: one of the main functions of CGIDEV2 is to remove the complexity of dealing with the CGI APIs.
CGIDEV2 is written in RPG and all source is included, so if you don’t like the way it works–change it. A couple of sample applications are also provided.
For an RPG programmer, CGIDEV2 is easy to learn since it is written in RPG. You just have to learn how to use the CGIDEV2 subprocedures, and five of them are all you need to get up and running (which should only take a couple of hours).
Again, since you are using RPG, you have easy access to the database and easy access to program/procedure calling.
Since CGIDEV2 is written in RPG, you will have to have access to an i in order to experiment with it. Also, since CGIDEV2 is specific to the IBM i, there are not a lot of educational resources available–the best resource is at www.easy400.net and the Easy400 group at Yahoo groups. But that being said, there is very little to be learned (a handful of procedures), and the rest is just good old RPG.
You also have to come up with your own solution for statelessness (using database or user spaces), but this just means finding a technique that works well for your application.
CGIDEV2 is not as heavily integrated with HTML as some of the other options, which means that the generation of some pages can, at times, be cumbersome. Also, CGIDEV2 does not have the same level of tooling as other options, e.g., IDEs, editors, etc.
To the best of my knowledge, the only framework that is available for CGIDEV2 is from Renaissance . It is open source and really excellent.
Let’s have a look at a simple “Hello World” example. CGIDEV2 requires that your HTML is coded separately from your RPG. Code 1 shows the HTML for our example, which is a document stored in the IFS named “/myCGIDevDir/helloWorld.tmpl”. “/$top” identifies a section (record format) named “top”, and “/%today%/” identifies a variable (field) named “today”.
Code 1--HTML for CGIDEV2 Hello World:
Code 2 shows the corresponding RPG program, which writes the page. The main points to note are:
Code 2--RPG for CGIDEV2 Hello World:
CGIDEV2 is available for download at www.easy400.net.
The Zend distribution of PHP on i now comes supplied on the system. Alternatively, you can download it at www.zend.com.
PHP is an interpreted (no compilation) script language that was designed with a strong focus on Web programming. It includes many strong language features for handling input from browser, has exceptional array handling (oh for some of it in RPG), and has lots and lots of BIFs.
One of the best features of PHP is that you don’t need an i in order to try it out. You can use one of the many free distributions, like XAMPP, on your PC, even accessing i data without any special configuration on i.
Given that PHP is not specific to i, there are lots of frameworks and tools available. Some of these are open source, others are chargeable.
There are also lots of examples available on the Web and myriad educational and reference resources, PHP.net probably being the best of these.
It is easy to access the database from PHP–it has a set of db2_ functions–but access to program/procedure calls require that you either wrap the programs/procedures in SQL stored procedures or make use of Zend’s PHP Toolkit for i5/OS. Although both work well, the latter does have some drawbacks in that not all data types are allowed as parameters, and only 10-digit integers may be used as return values.
Code 3 shows a PHP example of a “Hello World” script–helloWorld.php. PHP code is usually embedded in HTML, encased in <?php ? > directives, or it can construct HTML. In Figure 3, “Date()” is a PHP built-in function.
Code 3--PHP Hello World:
What can one say about Java? It is the hot, “new ” language on the block. To use Java in a Web environment you make use of JavaServer Pages (JSP) or JavaServer Faces (JSF). JSP/JSF are interpreted scripting languages that mix well with HTML. They work in the same way as PHP–the code is integrated with the HTML or can generate the HTML.
As with PHP, JSP/JSF are not specific to i, and there are lots of frameworks and tools available. Some of these are open source, others are chargeable. There are also the corresponding myriad educational and reference resources available.
For the traditional RPG programmer, one of the downsides of a Java approach is that you have to learn an OO language, and lots of us find that difficult to do. But you will find it easier if you have more than a passing familiarity with free form RPG and ILE. It may help you discover your hidden OO talents.
One of the facets that might influence a company (as opposed to an individual) toward a Java solution is that many institutes are now teaching Java as first preference: so there is a large workforce readily available.
JSP/JSF requires an Application Server, which is a little trickier to configure as opposed to the Apache Server. As with PHP, you don’t need an i in order to try it out, you can try it on your PC instead.
Database access is not as straightforward as with PHP. You will need to use a framework for your database access. And the same difficulties apply to program/procedure calls as with PHP.
Code 4 shows a JSP example of a “Hello World” script–helloWorld.jsp. Java code is usually embedded in HTML encased in <% %> directives), or it can construct HTML.
Code 4--JSP Hello World:
Scalability relates to how many people will be accessing your application at the same time, and how well your application can handle the workload without detrimental response times.
Usually, you would use Java for high usage, PHP for medium, and CGIDEV2 for below medium. The issue here is how to identify high, medium, and low, and I’m afraid there is no easy answer to that. It all depends on what the application is doing.
Hardware also has a large say in the proceedings. An application running on a Power 7 machine runs faster than the same application running on Power 5.
So, unless you have specific application knowledge that would lead you in one direction or another, I would let ease of maintenance and development be your guide.
To get to the Web, there is no single “right way.” It depends on where are you starting from, what your programming skills are, and what size application you are developing.
Remember to employ a tiered approach to your design, which means you treat the interface separately. This makes it is easy to change your mind and easy to implement future interfaces (e.g., iPhone, etc.).
As with all good technical solutions, it does not have to be one or the other. You could end up with a mixture of CGIDEV2, PHP, and Java. Maybe start with CGIDEV2 (since you are familiar with RPG), and progress to PHP/Java.
There is only one mistake you can make–doing nothing.
Paul Tuohy is CEO of ComCon, an iSeries consulting company, and is one of the co-founders of System i Developer, which hosts the RPG & DB2 Summit conferences. He is an award-winning speaker who also speaks regularly at COMMON conferences, and is the author of “Re-engineering RPG Legacy Applications,” “The Programmers Guide to iSeries Navigator,” and the self-study course called “iSeries Navigator for Programmers.” Send your questions or comments for Paul to Ted Holt via the IT Jungle Contact page.