Tuesday, April 18, 2017

Web Development with CGI programming

Hello Friends,

My previous post AS400 & HTML was about displaying some static HTML page from your AS400 IFS path by changing Apache httpd.conf file (using Alias statement). If you remember, I have stated that IFS directory is the path where we store our HTML. Yes that is true but we can also achieve the same using CGI programming. (In this case there is no need of separate HTML file required).

Note: CGIDEV2 is a separate tool which simplifies the CGI programming but unfortunately it is just free code available over web but IBM no longer supports it. But still it has very large variety of usage and we can use it at our own risk. I shall cover a separate series of blog for this. In this section we are going to concentrate on developing simple CGI program based on IBM provided API.

How the HTML is formed:

Well, obviously we are building the HTML code inside RPGLE only. So that bottom line is, we have to construct the HTML data inside RPGLE and finally we should be able to write it into the browser directory. In other words, we are going to execute the RPGLE program directly from the WEB and the program outputs a HTML data to the browser.

QZHBCGI service program:

QZHBCGI is the service program available under QHTTPSVR library. It contains number of procedures which are used under CGI programming at different state.

QtmhWrStout API:

In this tutorial we are going to look into QtmhWrStout API which is used to write the data into browser.

Ok let’s jump into coding. Assume we are going to write a RPGLE program WELCOME and if we call this program over web it should display “WELCOME to CGI Programming”

i.e.: http://youras400ip/cgi/welcome  should render HTML page.

Step 1: create RPGLE

Yep! This is it. Seems like simple one. But it is the starting point. 

If we split the code, we are storing the raw HTML data inside “Data” variable and directly writing it into browser using QtmhWrStout API.

Do note on two CRLF and Content-type. This is necessary since we are telling browser that we are writing HTML data. But this header will not be displayed in the browser.

How to compile:
  1. Compile the program as module (by giving 15)
  2. Then CRTPGM PGM(*LIBL/WELCOME) MODULE(*LIBL/WELCOME)    BNDSRVPGM((QHTTPSVR/QZHBCGI)) 
Step 2: Apache configuration

Previously we used Alias command and routed any request coming as /mydocs/ to get HTML file from specific IFS folder. Similarly we need to use ScriptAliasMatch configuration for CGI

Add this 4 lines into your httpd.conf file of Apachedft server.













What this essentially means that if any request coming with /cgi/ in the URL then call program from DEVYUS library. ([a-z0-9]+) is actually regular expression.

Note: it is not necessary that you have to give /cgi/ , you can give your own name as well. Just the fact that the URL should be http://youras400ip/cgi/program_name

Okies, once you have done this restart your apache server using 

STRTCPSVR SERVER(*HTTP) RESTART(*HTTP) HTTPSVR(APACHEDFT)

Now point to your browser and type http://youras400ip/cgi/welcome














Wow..!! Congrats you have just completed your first CGI program.

Again it will not seem like big as of now because it is just static page which is build based on string concatenation. So can we do some dynamic HTML based on CGI… well yes we can wait for it to come in my next post. Until then…

Have fun..!!! Happy Coding…!!!

2 comments:

  1. Oh.....my.....gosh!

    I'm shocked!!

    This is 2017!. The As400/iSeries/System I / whatever IBM decide to call it next has java for years. Decades even.

    And, java supports calling RPG using JNI and JNA.

    WHY.....just *why* are you doing this? Now. Today. Its not the way. Really.

    Instead, do this.
    1) Download Tomcat. Put it somewhere on the IFS
    2) Download Eclipse (or IBM's Eclipse based RPG tool).
    3) Create dynamic web app. Export as WAR, put in tomcat webapps directory on IFS
    4) Start Tomcat (RUNJVA)

    Really.

    ReplyDelete
    Replies
    1. Hi Tony, Appreciate your comment. I guess you get more opportunities to work with web development on IBM i. But you know, unfortunately we have many people being in the field for many years but still not aware of these basics (which I have explained here.

      Yes, I agree these are old way of doing it (may be in 2002 or 2004) but If we suddenly talk about Tomcat or other advance topics then they might not digest it.

      I am trying here to share my knowledge from scratch and with these baby steps soon I hope I can cover what you have suggested as well.

      Again thanks for sharing your thoughts and I hope you can also help me If I would need any help from you while exploring some advance topics.

      Cheers,

      Delete