Guild Companies, Inc.  
 
Midrange Programmer - How-To Advice & Free Code
OS/400 Edition
Volume 1, Number 3 - February 14, 2002

Build Dynamic Web Apps With iSeries Tomcat

by Richard Shaler

I described how to obtain, install, and configure Tomcat for your PC, and presented a sample JavaServer Page (JSP) application to run on your PC and retrieve information from your iSeries in "JavaServer Pages 101." With nothing more than an average PC and the free Tomcat servlet engine, from Apache Software Foundation, you can now start developing Java-based Web applications. (Though you'll probably want to use your iSeries system instead of a PC when it comes time to put your application on the Web.)

Profound
This article will teach you how to create and run a dynamic JSP application from your iSeries. A sample application is included that requests input from a user and passes the information to a JSP for processing. Running the application on an iSeries makes sense because the application retrieves the data from the same system on which the application runs. It offers better performance and more centralized management of application resources.

Prerequisites

To run a JSP application on an iSeries, you'll need to have Tomcat installed and configured on your iSeries system. The good news is, Tomcat is available for the iSeries (V4R5 or later) at no charge through the HTTP Server for iSeries (powered by Apache) Group PTF. Basically, you'll need to have an HTTP Server (powered by Apache ) instance configured and have Tomcat configured for that instance. (Learn more about configuring Tomcat on the iSeries in David Morris' article "Installing and Configuring Tomcat on iSeries.")

You can also find information about how to configure HTTP Server and Tomcat from the iSeries Information Center. Select your operating system level and then expand the topic e-business and web serving. Select Web serving, where you'll find the ASF Tomcat topic and detailed information on configuring an HTTP Server instance and Tomcat. For the sample application I present here, I used an HTTP Server instance named jkltest, which I configured from the instructions provided by the iSeries Information Center.

Your iSeries system will also need iSeries Developer Kit for Java licensed program (*BASE) and an optional feature Java Development Kit 1.2 or higher.

Be sure your iSeries system has the latest version of the HTTP Server Group PTF (SF99156 for V5R1; SF99035 for V4R5). You can find out what version is on your system with one of the following commands:

  • V5R1 systems--DSPDTAARA QHTTPSVR/SF99156
  • V4R5 systems--DSPDTAARA QHTTPSVR/SF99035
  • A display should appear, with a date to the right of the PTF number/version. Go to the HTTP Server PTFs page to determine the Last updated date. If the last updated date is later than what is on your system, you should order and install the latest PTF.

    If the appropriate data area is not on your system, it means the HTTP Group PTF has never been installed on your system.

    The Sample Application

    The sample application uses a JSP to process information from an HTML input form. The application displays dependent files (logical files or views) for any physical file or table (wildcard file names can be submitted) on your iSeries system. I refer to this application as depfiles. I use the depfiles application name in a number of the application resources.

    This is the HTML input form used by the sample application:

    As you can see, the input form requests system name, user profile, password, and file name or wildcard file name. The values you see in the input form request a list of dependent files for any physical file that begins with the characters OR, for an iSeries system named S103D64G.

    This is resulting output for the values submitted:

    It's really a pretty simple application, but it illustrates some important techniques used to create dynamic browser output from your iSeries. The source code for the application is made up of two files: an HTML file (depfilesinput.html) used for input and a JSP file (depfilesoutput.jsp) used for output. (The source code for these files is available for download.)

    HTTP Server (powered by Apache) and Tomcat
    Configurations and Resources

    Configuration and application resources for the HTTP Server (powered by Apache) instances and Tomcat can be found in the www directory of the ROOT file system of the iSeries Integrated File System, as illustrated here:

    There will be a directory under www for every instance of HTTP Server (powered by Apache). As you can see in the previous figure, there are two: apachedft and jkltest. Now look under the jlktest directory and notice the webapps directory. It is under the wepapps directory that the Tomcat application resources are stored. Each Tomcat application is contained in a subdirectory of webapps. The sample application in this article, for example, is stored in the directory named depfiles. You can name this directory anything you want, but it's a good idea to give it a name appropriate for the application contained within it. You can see the two files that make up the sample application in the depfiles directory: depfilesinput.html and depfilesoutput.jsp.

    Configuring Tomcat for the Sample Application

    For simplicity, I configured a Tomcat as an in-process, instead of as an out-of-process, server.

    I navigated to my Tomcat configuration by loading the administration application (http://:2001), signing on, clicking on the Configuration and Administration link (in the left navigator column), selecting the jkltest HTTP server by clicking on its radio button, clicking the Configure button, and, finally, getting to the JKLTEST global settings display:

    If you are configuring your Tomcat server for the first time, click on the ASF Tomcat Setup task, under the Dynamic Content column, in the middle of the display; if you want to view or change an existing Tomcat server configuration, click on ASF Tomcat Settings.

    Besides the basic Tomcat configuration settings, such as enabling servlets and enabling the in-process servlet engine, there are two things you need to do to add an application to a Tomcat server:

    1. Add URL
    2. Add an application context entry

    In the following figure, you can see the URL entry /depfiles/*, in the URLs list box. I added this simply by entering the value to the end of the list.

    Next, you can see the context entry for /depfiles at the end of the Application contexts list:

    This context entry maps the URL path to the application base directory, which is relative to /www/. For the sample depfiles application, you can see the URL path is /depfiles and the application base directory is webapps/depfiles. The full physical path to the sample application is /www/jkltest/webapps/depfiles.

    To add a context entry for the depfiles sample application, use the Add button, under the Application contexts section. A new entry with an empty text field under URL path and Application base directory columns will be added to the bottom of the list. Specify /depfiles for the URL path and /webapps/depfiles for the application base directory. Then, click on the Configure button of the new entry. This will open the ASF Tomcat Application Configuration display in another browser window. You don't need to change anything on this display for the sample application. Just click on the OK button and the default directories for the application will be created. For the sample application, a directory named depfiles will be be created under the /www/jkltest/webapps directory (the full path will be /www/jkltest/webapps/depfiles). You'll also notice a few other subdirectories created under the depfiles directory, such as WEB-INF, classes, and lib. (Those default directories won't be used here, but look for more information about these directories in a future article.) Before you leave the ASF Tomcat settings page, click the Apply button (located at the bottom of the page).

    Once Tomcat is configured for your application the HTTP Server (powered by Apache) instance under which Tomcat is configured must be stopped and started (or restarted) to make the changes take affect.

    Export/Publish a JSP Application to Tomcat

    No matter what system (PC or iSeries) you use to develop or download your JSP application, you'll need to copy the application's source files and other resources--Java Beans, class packages, configuration files--to the appropriate directory of the Tomcat server. This process is sometimes referred to as exporting or publishing. (In the case of J2EE the process is called deployment of the Java application.)

    For the sample application, copy the two source files (depfilesinput.html and depfilesoutput.jsp) available from the download to the /www/jkltest/webapps/depfiles directory.

    How to Call a JSP from an Input Form

    Now, let's look at the JSP application. The key to calling a JSP from an input form (in this case, depfilesinput.html) is the HTML form tag action attribute. I set the action attribute value to depfilesoutput.jsp to cause the depfilesoutput JSP to be called when the form is submitted. The form statement looks like this:

    <form action="depfilesoutput.jsp" method="post">
    

    I use post instead of get for one reason: I don't want the password field to appear in the URL parameter list that is automatically generated when the get method is used. The post method passes HTML form fields through the HTTP header, which is hidden from the average user. In a production application, you would actually want to take additional measures, such as using an SSL-enabled HTTP server.

    There are other good reasons for using post instead of get, such as not allowing savvy browser users to bypass client-side validation processing, or if you wanted to pass more than 256 characters of data or pass more than just character data.

    Retrieving the Input Form Fields

    Once the user enters data on the input form and submits it, the JSP (depfilesoutput.jsp) is called. However, before the JSP can perform any processing, it needs to retrieve the information from the form. A JSP can obtain the value of HTML form fields through the getParameter method of the implicit request object. For example, to retrieve the value of an HTML input field called firstname and assign it to a variable called fName, your JSP could use the following scriptlet:

    <% String fName = request.getParameter("firstname"); %>
    

    In the depfilesoutput.jsp source file, you can see four getParameter methods being used near the beginning of the source code, right after the "// Get input fields" comment. The JSP uses these methods to retrieve the system name, user profile, password, and file name values from the HTML input form and to assign them to variables used by the JSP.

    To better understand the implicit request object, it helps to know that, under the hood, your JSP is converted into a servlet, which by default uses two generic servlet objects: HTTPServletRequest and HTTPServletResponse. The HTTPServletRequest object actually represents the implicit request object of a JSP. An implicit response object is also available to JSPs, and that object is actually represented by the HTTPServletResponse object. Although the response object isn't used by the sample application, it's typically used for sending responses back to the browser client.

    Using the Appropriate JDBC Driver

    My previous article's JSP application used the IBM Toolbox for Java JDBC driver (type 4), because the application ran from a remote PC to access data on the iSeries system. A type 4 driver is appropriate for accessing a remote relational database because it converts the JDBC calls to the network protocol used by the relational database (e.g., DB2).

    The depfiles sample application, however, does not need remote access to DB2, because it retrieves data from the same system on which the JSP is running. In this case, the native iSeries JDBC driver (type 2) is more appropriate and can be used to provide better performance. This driver is automatically made available to your Web application, but if you're curious about it's location, it can be found in the db2_classes.jar package, located in the iSeries Integrated File System:

    /QIBM/ProdData/Java400/ext/db2_classes.jar
    

    Running the Sample Application

    Before you run the depfiles application, you need to ensure the HTTP server for which you configured Tomcat is running. To start the jkltest server instance, configured for the sample application from an OS/400 command line, use the following command:

    STRTCPSVR SERVER(*HTTP) HTTPSVR(JKLTEST) 
    

    Now open a browser window and use the following URL to activate the application:

    http://<your_server>:<HTTPpbA_port>/depfiles/depfilesinput.html
    

    If your HTTP Server (powered by Apache) uses the default port, which is 80, then :<HTTPpbA_port> is not required.

    This application is relatively simple but is a great reference for creating other typical applications that accept user input, process it, and then create output. Of course, this application is Web-capable.

    Coming Attractions

    In my next article, you'll learn how to add powerful server-side validation (no JavaScript required) by enhancing the sample application presented here. The enhancement will enable the JSP to trap errors and gracefully report them back to the user. The enhancement will teach you how to forward processing from one JSP to another and more about parameter management between different JSPs.

    A Quick Tomcat Tip

    Here's a tip that could be helpful to know as you get further into JSP application development. If you've programmed in Java, you probably know your Java program doesn't always find the classes it needs. Often you find yourself updating your system's CLASSPATH environment variable to gain access to classes (whether they're stand-alone or part of a package).

    With Tomcat, you have the option of either creating an entry for a JAR file in the Java classpath entries for your Tomcat server or simply copy the class or package in special Tomcat directories: WEB-INF/classes and WEB-INF/lib. (The WEB-INF/classes and WEB-INF/lib directories are automatically created when you add an application context entry to your Tomcat application and configure it.)

    To add supporting stand-alone classes to your Tomcat application use this path:

    /webapps/<your_application_directory>/WEB-INF/classes
    

    To add JAR files to your Tomcat application use this path:

    /webapps/<your_application_directory>/WEB-INF/lib
    

    If, for example, your JSP application needed the IBM Toolbox for Java JAR file (jt400.jar), you could create a Java classpath entry for your Tomcat server that pointed to the physical location of the package, or you could copy jt400.jar to the WEB-INF/lib directory. In both cases, Tomcat will automatically make the jt400.jar package available to your JSP application, eliminating the need to worry about the CLASSPATH variable.

    Sponsored By
    PROFOUND LOGIC SOFTWARE

    Looking to increase programmer productivity? Visit http://www.RPGAlive.com/now.

    RPG-Alive adds an array of graphical features to IBM's native Source Edit Utility, and is one of only a few products to receive a 10 out of 10 rating in an independent evaluation by 400Times.

    RPG-Alive allows RPG programmers to quickly gain an understanding of existing RPG programs, and facilitates a quicker way for programmers to change or write new source code.

    Here is what RPG-Alive users say:

    Name: Glenn Elliott
    Company: GDX Automotive
    "Your software is awesome. If someone were to sit down and figure out a great software program to accent code generation on a 400, this would be it. Everything from source code indentation to promptable key words. What a time saver! Thank you for saving me some time!"

    Name: Stuart Rowe
    Company: Help/Systems, Inc.
    "Installation went flawlessly, it started working without asking any questions, no lengthy setup, keep it up you're doing a great job. I have my manager poised over the checkbook with pen in hand as I type this."

    Name: Krish Thirumalai
    Company: Manhattan Associates
    "Excellent product. We have a lot of people in our organization that use and love this product. The built-in help for opcodes and BIF's is really cool! Our programmers no longer need to search for their RPG manuals to look up these opcodes."

    Name: Jim Kucharik
    Company: Victaulic Company of America
    "Your product has won the hearts of our programmers, not an easy thing to do!"

    To try RPG-Alive on your system, visit http://www.RPGAlive.com/now or call (937) 439-7925.

    THIS ISSUE
    SPONSORED BY:
    Help/Systems
    SoftLanding Systems
    BCD Int'l
    Jacada Ltd.
    Profound Logic Software
    WorksRight Software
    BACK ISSUES
    TABLE OF CONTENTS
    CL-Like Error Handling in RPG
    Installing and Configuring Tomcat on iSeries
    Learning Basic HTML Tags
    Java Concepts for iSeries Programmers
    Build Dynamic Web Apps With iSeries Tomcat
    Five More Cool Things You Can Do With OpsNav
      Newsletters | Subscribe | Advertise | About Us | Contact | Search | Home  
      Last Updated: 2/13/02
    Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.