tnt400.com - AS/400 Tips And Techniques

Sponsored by news400.com

This page is a discussion on the mentioned topic.
Most of the answers are in their original posted form, including any technical/spelling/grammatical errors.
No guarantees are expressed or implied. :-)
Comments, corrections, concerns about this tip?

Got another AS/400 question? Ask it here


What's New?
See what's new at Tips-N-Tech.

AS/400 Tips-N-Tech
AS/400 tips, techniques, and FAQ. Updated frequently.
CODEPage/400
All the code samples you can eat! RPG, CL, DDS, etc. etc.
AS/400 FAQs
The official news400.com FAQs.






All AS/400 Tip Categories / AS/400 Programming / Example of ILE RPG CGI Program


Question:

I'm looking for a CGI program example. I've heard that you can program it in RPG/ILE, but I have never seen it. Do you have any books or sample code?


Answer(s):



Take a look at
http://publib.boulder.ibm.com:80/cgi-bin/bookmgr/BOOKS/QB3AEQ02/CCONTENTS

A very simple one that only outputs data looks as follows;
DOutBuff          S           2048A
DOutBuffLn        S              9B 0 Inz(2048)
DAPIError         DS
D APIBytes                1      4B 0
D CPFId                   9     15
DAPIStdOut        C                   'QtmhWrStout'
DHTML             S            100    Dim(6) PerRcd(1) CtData
DNewLine          C                   X'15'
C                   MoveL     *Blanks       OutBuff
C     1             Do        6             n                 5 0
C                   Cat       HTML(n):0     OutBuff
C                   Cat       NewLine:0     OutBuff
C                   EndDo
C                   ExSr      StdOut
C                   SetOn                                        Lr
C                   Return
C     StdOut        BegSr
C                   Eval      APIBytes = %Size(APIError)
C                   Eval      OutBuffLn = %Len(%Trim(OutBuff))
C                   CallB     APIStdOut
C                   Parm                    OutBuff
C                   Parm                    OutBuffLn
C                   Parm                    APIError
C                   EndSr
**CTDATA HTML
Content-type: text/html

<HTML>
<HEAD><TITLE>Hello world CGI sample</TITLE></HEAD>
Hello world.
</BODY></HTML>


BTW, if you build the output string like this, it's recommended to use varying length fields, and just before the API copy them to the fixed length output field. The difference in performance is huge when some string handling is done.




I continually cringe at CGI examples that use arrays in the RPG Program. Try this helloworld program out for size.
 ****************************************************************
 * 1. CRTRPGMOD MODULE(lib/HELLOW) +                            *
 *     SRCFILE(lib/QRPGLESRC) TGTRLS(V3R7M0)                    *
 *                                                              *
 * 2. CRTPGM PGM(lib/HELLOW) BNDSRVPGM(QTCP/QTMHCGI)            *
 *                                                              *
 ****************************************************************
D WPError         DS
D  EBytesP                1      4B 0 INZ(40)
D  EBytesA                5      8B 0
D  EMsgID                 9     15
D  EReserverd            16     16
D  EData                 17     56
 *
D HTTPHeader      C                   CONST('Content-type: text/html')
D NewLine         C                   CONST(X'15')
 *
D WrtDta          S           1024
D WrtDtaLen       S              9B 0
 ****************************************************************
C                   EXSR      $Main
 *
C                   eval      *INLR = *On
 ****************************************************************
 * Main Subroutine
 ****************************************************************
C     $Main         BEGSR
 *
C                   eval      WrtDta = '<html><head>' +
C                                      '<title>Hello World</title>' +
C                                      '</head><body>' +
C                                      'Hello World!' +
C                                      '</body></html>' +
C                                      NewLine
C                   EXSR      $WrStout
 *
C                   ENDSR
 ****************************************************************
 * Write to Standard Output
 ****************************************************************
C     $WrStout      BEGSR
 *
C     ' '           CHECKR    WrtDta:1024   WrtDtaLen
 *
C                   CALLB     'QtmhWrStout'
C                   PARM                    WrtDta
C                   PARM                    WrtDtaLen
C                   PARM                    WPError
 *
C                   ENDSR
 ****************************************************************
 * Initialization Subroutine
 ****************************************************************
C     *INZSR        BEGSR
 *
C                   eval      WrtDta = %trim(HTTPHeader) +
C                                      NewLine + NewLine
C                   EXSR      $WrStout
 *
C                   ENDSR





This should do it...
DOutBuff          S           2048A
DOutBuffLn        S              9B 0 Inz(2048)
DAPIError         DS
D APIBytes                1      4B 0
D CPFId                   9     15
DAPIStdOut        C                   'QtmhWrStout'
DHTML             S            100    Dim(8) PerRcd(1) CtData
DNewLine          C                   X'15'
C                   Eval      OutBuff = ''
C     1             Do        8             n                 5 0
C                   Cat       HTML(n):0     OutBuff
C                   Cat       NewLine:0     OutBuff
C                   EndDo
C                   ExSr      StdOut
C                   SetOn                                        Lr
C                   Return
C     StdOut        BegSr
C                   Eval      APIBytes = %Size(APIError)
C                   Eval      OutBuffLn = %Len(%Trim(OutBuff))
C                   CallB     APIStdOut
C                   Parm                    OutBuff
C                   Parm                    OutBuffLn
C                   Parm                    APIError
C                   EndSr
**CTDATA HTML
Content-type: text/html
!!!this is a blank line!!!
<HTML>
<HEAD><TITLE>Hello world CGI sample</TITLE></HEAD>
<BODY>
Hello world.
</BODY>
</HTML>

Don't forget to compile it with service program QTCP/QTMHCGI





Other tips in this category:

Click here to see all categories.

Socket Programming And Timeout Issues
Deleting Duplicate Records From A Table
What are data queues and how to use
Retrieving SMTP Name
RPGLE example for Dynamic Screen Manager API
Calling APIs from CL - with examples!
Compare two strings in RPG character by character
How to search all pgms in QCLSRC for a keyword
Retrieve Database File Description (QDBRTVFD) API
How to redirect the output to STDERR from RPG-IV
Using Multiformat Logical To Join 2 Identical File
D-Spec *LIKE DEFN
RPG: Converting Character to Decimal
Example of ILE RPG CGI Program
Handling ILE RPG Numeric Overflow
Help with Subfile Programming in RPG III
Zoned parameter in ILE RPG
What's the best way to do modulus in CL?
RPG Nesting Source Print Utility
More on changing the SIGNON screen
A silly ILE RPG question
Calling Validation List API From ILE RPG
Soft Coding Module Names
RPG Multidimensional Arrays in Action
Build a Page-Equals-Size Lookup Window
Procedures within an ILE RPG program
Break msg from RPG
Source Debugger for batch jobs
RPGLE debugging
Timing out display sessions in DDS
Building Dynamic Stored Procedures
Put Message in System Log
Dynamic RPGSQL
Randomize function for the AS/400
What's the fastest way to do a simple RPG lookup?
First time Data queue application in RPG
Print file overflow in ILE RPG
Mapping Fields To Arrays in ILE RPG
Named inidicators
STRQMQRY: comparision operator '=' isn't correct?!
DSPDTAARA to an outfile - possible?
PCL ESC codes in RPG
Sharing DB files between two AS/400s
Help: AS/400 subfiles
How do you change the signon screen?
Determining Even/Odd Values in queries
Get day of the week in RPG
Convert UPPERCASE to lowercase
RPG IV help using APIs
Help with data area API
Detecing IFS Files from RPG
SQL in a CL program
Calling AS/400 APIs from ILE RPG
CVTDAT command to convert an *MDY to a *LONGJUL
CPYSPLF Automation
Subfile Window background problem
Using IFS APIs w/ ILE RPG
Packed or unpacked fields?
QRYDFN to source and back
What does the "optimize" parm do?
Determining the calling program
Sockets in RPG?
Sorting a user space
OVRDBF and SECURE() keyword in an ILE environment
RPG record locking
RPG Differences: V2R3 to V3R2 upgrade
Getting the relative record number (RRN)
What is the longest parameter usable in RPG?
A C function that returns a string to an RPG pgm
Problems with ZADD *ZEROS
Can you highlight code in SEU?
Reusing deleted records - OK?
Is there an easy way to change edit codes?
Help - Windowed Subfiles
Logical Files and DDS
AS/400 'machine language' - MI Programming
Library lists and performance
How to use ERRSFL
Updating in CL
Record lock wait time
Message subfile problem
Physical File Joins
CL: Copying User Profiles
Commands and PARM
ILE RPG, RPG IV, vs. RPG/400
RPG and subfiles
Multiple subfiles
Field masking for passwords
SFLMODE keyword in an ILE program
Subfile size
Color coding records in a subfile
MSGLINE in windows
Controlling cursor movement on a display file
API returns error! Why?
Put an RPG Program on the Web
Variable length records in RPG
Differences Between RPG400, RPG IV, and ILE RPG


You are at a news400.com site.
Contact Us | Report Bugs | Submit Comments/Suggestions | Read Site Use Agreement | Read Privacy Policy
Copyright © 2000 Duke Communications International.
This site is best viewed with the latest versions of Netscape or Internet Explorer, 800 x 600 resolution (or higher), and at least 256 colors.
Duke Communications   NEWS/400 | 29th Street Press | Business Finance | DominoPro | Selling AS/400 Solutions | SQL Server Magazine | Windows NT Magazine