S1A - Web Programming with eRPG
Transcription
S1A - Web Programming with eRPG
Essential eRPG Bradley V. Stone www.bvstools.com Essential eRPG • The Basics HTML, JavaScript, Stylesheets, Cookies, SSI, The IFS • The Function How does eRPG work? • The Core Working with Input, Output and More • The Misunderstandings Hype, Lies, and the real power of eRPG • The Examples Examples of sites using eRPG • The Resources Resources for eRPG Application Development and Administration © 2007 Cozzi Productions, Inc. The Basics Any web application team requires knowledge of the following: • Server Configuration • HTML • JavaScript • Stylesheets (CSS) • Cookies • Server Side Includes (SSI) • The IFS © 2007 Cozzi Productions, Inc. The Basics Server Configuration Contains commands that: • Specify the port • Restrict and grant access to certain locations on the machine • “Hide” physical location of documents • Specify location of CGI applications • Control other aspects of the server (directory indexes, server side includes, etc.) © 2007 Cozzi Productions, Inc. The Basics Server Configuration Cont… Listen xx.xx.xx.xx:80 DocumentRoot /www DirectoryIndex index.html CGIConvMode %%EBCDIC/EBCDIC%% ScriptAliasMatch ^/cgi-bin/(.*) /qsys.lib/as400cgi.lib/$1.pgm <Directory /> Options None AllowOverride None order deny,allow deny from all </Directory> <Directory /www> AllowOverride None order allow,deny allow from all <FilesMatch "\.html(\..+)?$"> Options +Includes SetOutputFilter Includes </FilesMatch> </Directory> <Directory /qsys.lib/as400cgi.lib> allow from all order allow,deny Options +ExecCGI +Includes SetOutputFilter Includes </Directory> © 2007 Cozzi Productions, Inc. The Basics Sever Configuration Cont… Web Request • www.mysite.com • www.mysite.com/faq.html • www.mysite.com/docs/erpg .html • www.mysite.com/cgibin/listorder • /images/logo.jpg © 2007 Cozzi Productions, Inc. Actual file served • /www/index.html • /www/faq.html • /www/docs/erpg.html • Program LISTORDER in library AS400CGI • ? The Basics Server Configuration Cont… <img src=“/images/logo.jpg” border=“0”> Actual location: /www/images/logo.jpg © 2007 Cozzi Productions, Inc. The Basics HTML Why is HTML important? © 2007 Cozzi Productions, Inc. The Basics HTML • Hypertext Markup Language • The “Building Blocks” of web pages • Used for static and dynamic content • Without it, web clients would be “lost” © 2007 Cozzi Productions, Inc. The Basics HTML Cont… Main HTML Components • Text • Hyperlinks • Images • Tables • Input fields and objects © 2007 Cozzi Productions, Inc. The Basics HTML Cont… http://www.bvstools.com/download.html (local) © 2007 Cozzi Productions, Inc. The Basics JavaScript • Makes pages more Interactive • Gives control over web page objects • Client-side data validation © 2007 Cozzi Productions, Inc. The Basics JavaScript Cont… <SCRIPT> function Validate(form) { if (form.dept.value=="") { alert("Please enter a department."); return false; } return true; } </SCRIPT> © 2007 Cozzi Productions, Inc. The Basics JavaScript Cont… http://www.bvstools.com/erpg/erpgv2/examples/formvalidation1.html (local) © 2007 Cozzi Productions, Inc. The Basics Stylesheets (CSS) • Used to control the look and feel of web page • Allow you to “externalize” the web page formatting • Allow for easy, quick and localized web page style changes © 2007 Cozzi Productions, Inc. The Basics Stylesheets Cont… • • • • Stylesheets apply “styles” to web objects Stylesheets “cascade” Stylesheets can be internal or external Using Stylesheets allows you to change attributes for all web objects in one place body {background-color: #FFFFFF; font-size: 70%; font-family: arial} td {font-size: 80%; background-color: #FFFFFF; text-align: left; vertical-align: top} td.right {text-align: right} © 2007 Cozzi Productions, Inc. The Basics Stylesheets Cont… © 2007 Cozzi Productions, Inc. The Basics Stylesheets Cont… © 2007 Cozzi Productions, Inc. The Basics Cookies What are Cookies? © 2007 Cozzi Productions, Inc. The Basics Cookies • Cookies allow you to store information to identify a visitor, order, or other information • Cookies allow “easy” persistent web programming • Cookies themselves are not “dangerous” • Cookies should not contain sensitive data • Some “bugs” in browsers allow(ed) access to cookies they shouldn’t have © 2007 Cozzi Productions, Inc. The Basics Cookies Cont… Cookies are placed into the web page headers in the form: Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure • Cookies are stored locally on the client’s machine • Cookies values are retrieved using the HTTP_COOKIE environment variable © 2007 Cozzi Productions, Inc. The Basics Cookies Cont… http://www.bvstools.com/erpg/erpgv2/examples/testcookie.html © 2007 Cozzi Productions, Inc. The Basics Server Side Includes (SSI) • Server Side Includes (SSI) allow you to build your web pages in smaller pieces, like puzzle • SSI allows you to change reused pieces on every page in one step • SSI content can be either static or dynamic © 2007 Cozzi Productions, Inc. The Basics SSI Cont… Web Page Source: SSIData.html Source: <html> <body> The following will be inserted using an SSI directive. <hr> <!--#include virtual="/SSIData.html" -> <hr> The preceding was inserted using an SSI directive. </body> </html> <!--begin SSIData.html --> This data was inserted using an SSI directive.<br> Isn't that cool?<br> <!--end SSIData.html --> © 2007 Cozzi Productions, Inc. The Basics SSI Cont… © 2007 Cozzi Productions, Inc. The Basics SSI Cont… “View Source” results: <html> <body> The following will be inserted using an SSI directive. <hr> <!--begin SSIData.html --> This data was inserted using an SSI directive.<br> Isn't that cool?<br> <!--end SSIData.html --> <hr> The preceding was inserted using an SSI directive. </body> </html> © 2007 Cozzi Productions, Inc. • SSI Statements insert content at run time • Content inserted can be static or dynamic • HTTP server must be configured to “recognize” SSI statements The Basics SSI Cont… Web Page Source: <html> <body> The following will be inserted using an SSI directive. <hr> <!--#exec cgi="/erpg/ssi1" -> <hr> The preceding was inserted using an SSI directive. </body> </html> © 2007 Cozzi Productions, Inc. The Basics SSI Cont… © 2007 Cozzi Productions, Inc. Source for SSI1 eRPG Program The Basics SSI Cont… <Directory /www> AllowOverride None order allow,deny allow from all <FilesMatch "\.html(\..+)?$"> Options +Includes SetOutputFilter Includes </FilesMatch> </Directory> <Directory /qsys.lib/cgilib.lib> allow from all order allow,deny Options +ExecCGI +Includes SetOutputFilter Includes </Directory> © 2007 Cozzi Productions, Inc. The Basics The IFS • Different from the Library/File file system we are used to • Similar to Windows or Unix file systems • Used to store static web pages, JavaScript, Stylesheets, etc. • Used to store Apache configuration files • Access through green screen, FTP, Windows Explorer © 2007 Cozzi Productions, Inc. The Basics The IFS Cont… The IFS Contains: • QSYS.LIB file system • QDLS file system • QOpenSys file system • “Root” File System • Others… IFS Commands • Work With Object Links (WRKLNK) • Display File (DSPF) • Edit File (EDTF) • Copy to Stream File (CPYTOSTMF) • Copy to Import File (CPYTOIMPF) • Others… © 2007 Cozzi Productions, Inc. The Basics The IFS Cont… Basic IFS Structure for web applications. Port 80 (Production) • /www [html] – /Images – /Stylesheets – /Javascript – /Templates – /SSI © 2007 Cozzi Productions, Inc. Port 8080 (Devo/Test) • /wwwtest [html] – /Images – /Stylesheets – /Javascript – /Templates – /SSI The Function • • • • • • What is “eRPG”? How does eRPG Work? What Languages are Used? What about the Web Server? Environments eRPG Benefits © 2007 Cozzi Productions, Inc. The Function What is “eRPG”? Simple, an easier way to say: • RPG/CGI • CGI/RPG • CGI Programming with RPG © 2007 Cozzi Productions, Inc. The Function How does eRPG work? • Client makes a request • Server decides what it needs to return • Static pages are returned “as is” • Dynamic pages are built using a eRPG program Examples: http://www.myserver.com/index.html (static) http://www.myserver.com/cgi-bin/sales.pgm (dynamic) © 2007 Cozzi Productions, Inc. The Function What Languages are Used? • • • • • • RPG COBOL Net.Data Perl Active Server Pages (ASP) Java (Servlets, Java Server Pages, etc) © 2007 Cozzi Productions, Inc. The Function What Languages are Used? Basic Requirements for each language: • RPG/COBOL/Net.Data – “Powered by Apache” HTTP Server • Perl – 3rd party or native Perl Interpreter and “Powered by Apache” HTTP Server • Active Server Pages (ASP) – Microsoft Internet Information Services (IIS) Server • Java – WebSphere or other Java Web Service (such as Tomcat) © 2007 Cozzi Productions, Inc. The Function What about the Web Server? • In the beginning, there was the “Classic HTTP Sever” • Then a choice, “Classic” or the new “Powered By Apache” HTTP Server • V5R3 and higher, no longer a choice, only the “Powered by Apache” HTTP Server is available • WebSphere is NOT required © 2007 Cozzi Productions, Inc. The Function Environments • It is useful to set up at least two environments, possibly three – Production – Testing – Development © 2007 Cozzi Productions, Inc. The Function Environments Cont… Each environment should contain: • A web server using a separate IP address/port • A web configuration • A directory to hold files, templates, images, JavaScript, etc • A programming library © 2007 Cozzi Productions, Inc. The Function eRPG Benefits • • • • • • • Very small learning curve for RPG shops Available learning media Available toolkits (CGIDEV2, eRPG SDK) Existing RPG skills used No extra software is needed Excellent performance Fewer system resources used overall © 2007 Cozzi Productions, Inc. The Core • • • • • • • • User Profiles Library List APIs Output Input Record Selection Debugging Toolkits © 2007 Cozzi Productions, Inc. The Core User Profiles • Your eRPG programs run in jobs just like any other application • Jobs normally run under user profiles QTMHHTTP and QTMHHTP1 • Knowing this helps with problem solving such as disabled profiles, authority issues and finding job logs © 2007 Cozzi Productions, Inc. The Core Library List • Library Lists are handled a bit differently with web applications • The library list cannot be manipulated using job descriptions (at least previous to V5R3) • The library the web application is running in is “automatically” in the library list • Any other libraries must be manually added or removed © 2007 Cozzi Productions, Inc. The Core APIs • • • • • Read input Write output Retrieve environment variables Convert web data to a usable format Located in service program QZHBCGI in library QHTTPSVR © 2007 Cozzi Productions, Inc. The Core Output • Basic function of eRPG programs is output • Normally in the form of HTML • This is why understanding HTML is so important • Can also be text, XML, other types of data © 2007 Cozzi Productions, Inc. The Core Output Cont… • QtmhWrStout API is used • Create a string, pass it to this API • Data is written to “Standard Output” – “Data” is normally HTTP Headers + HTML • Standard Output in this case is a web browser • Example (local) • Example Source © 2007 Cozzi Productions, Inc. The Core Input • Interaction requires Input from the user • Text fields, text areas, selection lists, bullets, check boxes • Using the proper input type is critical to design • Data validation © 2007 Cozzi Productions, Inc. The Core Input Cont… Environment Variables GET or POST Data • • • • • • Dependant on form “Method” • Each type uses a different API to read data • Data is always in the same form: IP address User ID (when available) Browser Type POST Data Length Data from a form using the “GET” method • Cookie data • Many more! var1=value1&var2=value2& …. © 2007 Cozzi Productions, Inc. The Core Input Cont… Input is read using one of the following APIs: • QtmhGetEnv – For reading “Query Strings” (GET) • QtmhRdStin – For reading “Standard Input” (POST) • QzhbCGIParse – Returns data by field name (either GET or POST) © 2007 Cozzi Productions, Inc. The Core Input Cont… • QtmhCvtDB API is used to convert input to a “usable” format • A physical file is used by the QtmhCvtDB API to “pair” fields to determine size and data type Data: NAME=Brad&AGE=36 Physical File: NAME 30(A) AGE 5,0(P) Results: NAME = “Brad” AGE = 36 © 2007 Cozzi Productions, Inc. The Core Input Cont… • QzhbCGIParse API simplifies reading data • Build the command string • Call the API • Data is always returned as character • Can also retrieve the nth instance of a variable © 2007 Cozzi Productions, Inc. Command String: -value NAME Returns: Brad Command String: -value AGE Returns: 34 Command String: -init Returns: QUERY_STRING=NAME=Brad&Age=3 4 The Core Input Cont… • GET Example (Source) • POST Example (Source) • QzhbCGIParse Example (Source) © 2007 Cozzi Productions, Inc. The Core Input Cont… • Data validation is very important • At least server side • Both client and server side is ideal • How errors are displayed is also crucial © 2007 Cozzi Productions, Inc. The Core Input Cont… • When possible, don’t display a page with errors only, asking to use the “Back” Button • Instead, display errors on the screen, close to the fields in error. (source) • Server Side Error Example © 2007 Cozzi Productions, Inc. The Core Record Selection • To display data, you must retrieve data from your database files • “Native” methods (ie. READx, CHAIN) • Open Query File (OPNQRYF) • SQL (Embedded or Dynamic) © 2007 Cozzi Productions, Inc. The Core Record Selection Cont… Native Methods (ie READx, CHAIN, SETxx) • eRPG Programs function exactly like green screen/report programs • Accessing data is no different • Main issue is library list © 2007 Cozzi Productions, Inc. The Core Record Selection Cont… OPNQRYF (Open Query File) • Normally done inside RPG Programs • Statement can’t be built interactively • Outdated method, but still may be more comfortable to some • Main issues are library lists, overrides, manual file opens © 2007 Cozzi Productions, Inc. The Core Record Selection Cont… Embedded or Dynamic SQL • SQL is more common to other platforms • Programmers not familiar with RPG may be able to help with SQL • Dynamic SQL is more “friendly” for certain applications • Main issue is learning curve © 2007 Cozzi Productions, Inc. The Core Record Selection Cont… • Creating functional SQL statements requires breaking up the SQL statement into logical clauses – The “SELECT” Clause – The “WHERE” Clause – The “ORDER BY” Clause © 2007 Cozzi Productions, Inc. The Core Record Selection Cont… • The “SELECT” clause is used to specify which fields to retrieve from the database File, as well as which database file to process Eval Select = ‘Select ITITEM, ITDESC, ITQTY from ITEMPF’ Eval Select = ‘Select * from ITEMPF’ © 2007 Cozzi Productions, Inc. The Core Record Selection Cont… • The “WHERE” clause is used to specify which records to select from the database file Eval Where = ‘WHERE ITITEM = ‘’’ + %trim(MyItem) + ‘’’ AND ITQTY > 10’ © 2007 Cozzi Productions, Inc. The Core Record Selection Cont… • The “ORDER BY” clause is used to specify the order in which the data is returned Eval OrderBy = ‘ORDER BY ITQTY, ITITEM’ © 2007 Cozzi Productions, Inc. The Core Record Selection Cont… • Combine all three pieces to create your completed SQL statement! Eval DynSQL = %trim(Select) + ‘ ‘ + %trim(Where) + ‘ ‘ + %trim(OrderBy) Select ITITEM, ITDESC, ITQTY from ITEMPF WHERE ITITEM = ‘MYITEM’ AND ITQTY > 10 ORDER BY ITQTY, ITITEM © 2007 Cozzi Productions, Inc. The Core Record Selection Cont… • Dynamic SQL Example • Source © 2007 Cozzi Productions, Inc. The Core Debugging • Problem solving involves “playing computer” • Sometimes debugging is the only way to track down a problem • eRPG Debugging is similar to Batch RPG debugging © 2007 Cozzi Productions, Inc. The Core Debugging Cont… eRPG Debugging Steps • Find the correct job • Start a service job • Start debug on the application • Add breakpoints • Run application from browser and debug © 2007 Cozzi Productions, Inc. The Core Debugging Cont… Finding the correction job – Method 1 (V5R4 and up) 1. End the HTTP Server 2. Restart the HTTP Server with parameters “minat 1 maxat 1” 3. There should only be one CGI job now that you can use for debugging (job won’t start until you run a CGI program) © 2007 Cozzi Productions, Inc. The Core Debugging Cont… Finding the Correct Job – Method 2 (V5R3 and down) 1. View the HTTP Server Jobs using WRKACTJOB 2. Restart Statistics with F10 3. Run CGI Application from web client 4. Refresh HTTP Server Jobs Screen and look for CGI job that the CPU % changed on © 2007 Cozzi Productions, Inc. The Core Debugging Cont… © 2007 Cozzi Productions, Inc. The Core Debugging Cont… © 2007 Cozzi Productions, Inc. The Core Debugging • Start Debug on the Application: STRDBG PGM(AS400CGI/LISTORDER) UPDPROD(*YES) • Add Breakpoints • Run application from browser • Program will break in the location you set, debug as your normally do green screen applications © 2007 Cozzi Productions, Inc. The Core Debugging Cont… Debugging Tips • • • • Debug in a Test or Development Environment Find the correct job to start a service job on Make sure breakpoints are in the proper places Clean, well-commented code makes debugging easier! © 2007 Cozzi Productions, Inc. The Core Toolkits • • • • Use existing skill set Very small learning curve Easy on resources Split up business and presentation layers • No more hard-coding HTML! • Not just for web pages! © 2007 Cozzi Productions, Inc. The Core Toolkits Cont… • Templates are external from program • Use ILE functions to “replace” data and build web page • CGIDEV2 – www.easy400.net • eRPG SDK – www.erpgsdk.com © 2007 Cozzi Productions, Inc. The Core Toolkits Cont… • Example (local) • Template • RPG Source © 2007 Cozzi Productions, Inc. The Misunderstandings • • • • • • • • • It’s “Legacy” Technology! It’s Slow! It’s Resource Intensive! It’s Hard to Learn! There Aren’t Any Resources, Toolkits or IDEs Available! Our Web Programmers Don’t Know RPG! WebSphere Makes “Prettier” Web Pages! IBM Doesn’t Promote It! Quotes from eRPG Users © 2007 Cozzi Productions, Inc. The Misunderstandings “It’s Legacy Technology!” “A legacy system is an antiquated computer system or application program which continues to be used because the user (typically an organization) does not want to replace or redesign it.” - Wikipedia • Scare term used to sell the flavor of the month • Will those “flavors” ever become legacy (ie, will they be around long enough?) • Always ask yourself, “What do those who call RPG legacy have to gain by you not using it, and what do you have to lose?” © 2007 Cozzi Productions, Inc. The Misunderstandings “It’s Slow!” • Believe it or not, some people (including IBM reps and popular figures in the iSeries community) have told people other technologies such as Java and WebSphere are faster and more efficient than RPG for web applications! • RPG Web applications should not be any slower than green screen applications. Native DB access is a big plus when it comes to performance. • Trust your experience with a technology that’s been around long enough to be called “legacy”. © 2007 Cozzi Productions, Inc. The Misunderstandings “It’s Resource Intensive!” • Yes, the same people calling RPG slow also claim RPG to be more resource intensive than applications created with tools like WebSphere! • Anyone who has spent any time using WebSphere knows that this couldn’t be further from the truth! • Most small to mid sized machines are crippled just by starting the WebSphere Application Server. © 2007 Cozzi Productions, Inc. The Misunderstandings “It’s Hard to Learn!” • The hardest part of using any web solution is the basics such as HTML, JavaScript, Stylesheets, Server Side Includes, and Cookies. • These topics are required for ANY web solution. • With the use of a toolkit such as CGIDEV2 or the eRPG SDK, the learning curve is even lower! • Compare that to the months or years needed to learn how to use WebSphere and Java effectively! © 2007 Cozzi Productions, Inc. The Misunderstandings “There Aren’t any Resources Available!” • Actually, there are plenty of resources available! Check out www.bvstools.com/erpg/ for a list of books and training manuals on the subject! • Sign up on the Web400-L mailing list at www.midrange.com for discussions on the subject! • Toolkits such as CGIDEV2 and the eRPG SDK are readily available! The only IDEs you really need are SEU (to edit RPG) and a good text editor or HTML editor! Especially with the use of a toolkit! © 2007 Cozzi Productions, Inc. The Misunderstandings “Our Web Programmers Don’t Know RPG!” • Your web programmers can help your RPG programmers with the HTML, JavaScript, and other web topics! • Split up your web application design into presentation and business groups! Let your web programmers design the web pages, and your RPG programmers handle the business programming! • With the use of a toolkit, splitting the work is even easier! © 2007 Cozzi Productions, Inc. The Misunderstandings “WebSphere Makes ‘Prettier’ Web Pages!” • How “pretty” a web page looks relies entirely on the web page design. • Web pages are made up of the same “stuff” no matter what web tool is used! • There is nothing “Magical” about any web tool that will make your web pages look better! It’s all dependant on how the programmers design them! • As always, please practice function before form! © 2007 Cozzi Productions, Inc. The Misunderstandings “IBM Doesn’t Promote It!” • Why should they, it’s virtually a free technology! • IBM pushes WebSphere because they make money not only on the product, but once you realize the system and programming resources needed, you’ll be looking for bigger hardware, consulting and training! • Sometimes what makes the most sense is the obvious choice, even if it differs from the opinions of IBM and System i “pundits”. © 2007 Cozzi Productions, Inc. The Misunderstandings Quotes from eRPG Users ““I have found that using [WebSphere] the learning curve is great, the costs are great, the recourses both on the PC and the AS/400 is great and it is extremely slow compared to using RPG and the CGIDEV2 tools.” – Jason Baker, K&M Tire, Inc. © 2007 Cozzi Productions, Inc. The Misunderstandings Quotes Cont… “eRPG has a particular advantage for companies with experienced RPG people who already have experience with the local data and business rules.” – Martin Cytryn, Midrange Systems © 2007 Cozzi Productions, Inc. The Misunderstandings Quotes Cont… “WebSphere was initially offered for free as part of the OS. It was promoted by IBM as a way to easily transform Client Access screens into web usable pages. It was more costly trying to implement this, "free" software than if I had purchased a program. It did not come close to working as advertised and every time IBM revised the program it would wipe out the code we wrote to work around WebSphere's problems. To add insult to injury IBM then tiered WebSphere into various feature levels for purchase. We luckily found eRPG. It works as stated and my RPG programmers can actually see and understand all the code. WebSphere is a Trojan horse for consultants and IBM. Get CGIDEV2 instead. ” – Edward Gallucci, Jeunique International © 2007 Cozzi Productions, Inc. The Examples • • • • BVS/Tools (www.bvstools.com) Jeunique (Distributor’s Page 1052427) Jeunique (Online Orders) Spartan Insurance (Insurance Quote – Zip code 78701/Austin) © 2007 Cozzi Productions, Inc. The Resources • www.bvstools.com/erpg/ • www.easy400.net • www.rpgiv.com © 2007 Cozzi Productions, Inc.