Guild Companies, Inc.  
WorksRight Software Inc.
 
Midrange Programmer - How To, Advice & Free Code
OS/400 Edition
Volume 1, Number 1- January 17, 2002

JavaServer Pages 101

by Richard Shaler

[You can download the code for this article at www.itjungle.com/downloads/mpo011702-story06.zip.]

You may be closer to Web application development than you realize. Whether you're just getting started with Java or you've followed the path of leaning object-oriented programming and all of its abstract concepts--just to find that, after months of study, you still haven't written a Java application--JavaServer Pages might be exactly what you need. JSP is probably the simplest way to get started with Java Web application development. In this article, I'll explain the basics of JSP programming and include a working JSP sample program that retrieves information from an iSeries file to help you learn just how simple developing with JSP can be.

WorksRight

JavaServer Pages Defined

JSPs can be thought of as Web page templates. Dynamic content, including information from existing applications and relational databases such as those stored in iSeries applications, can be generated by embedding Java code (scripting) along with normal HTML statements. With JSP, Web application developers can quickly create and easily maintain dynamic pages. Don't think scripting implies something less than powerful, because you essentially have the full Java programming language available to you in JSPs.

Although JSPs are relatively simple, what's going on under the covers is a little more complex. The illustration in JSPFigure1.jpg conveys the basic working of the JSP architecture.

The steps involved in getting a JSP to produce output are as follows:

  • The browser requests a JSP that resides on the server.
  • The embedded Java source code of the JSP is parsed out of the .jsp file.
  • The parsed code is translated into Java source code and placed in a .java source file.
  • The .java source file is compiled into a special type of Java program known as a servlet and stored in a .class file.
  • The servlet executes, returning output to the browser.

Once the JSP is translated and compiled, the servlet normally remains on the system, ready for use again and again. Because servlets are multithreaded and memory-resident, they are a good match for Web applications, where the need to manage large numbers of users in a stateless environment is often required.

A Simple Sample

Before I get into more detail about JSP, I want to show you a simple JSP that displays the current date and time in a browser. The following JSP source code would be contained in a file with an extension of .jsp.

<html>
<head>
<title>Current Date and Time</title>
</head>
<body>

The current date and time is:
<%= new java.util.Date() %>

</body>
</html>

As you can see, most of the page is HTML, except for the one statement scriptlet
containing the Java code to display the current date and time.

Most JSP elements are opened with the <% tag and closed with the %> tag. An XML-like syntax that is more self-descriptive can be used as an alternative to the <% and %> tags,
although the <% and %> tags are more efficient for a developer to use when manually
creating JSPs.

JSP Elements

There are five scripting elements in JSP: scriptlets, expressions, declarations, directives, and actions.

JSP scriptlets are simply one or more Java language statements. This is the JSP syntax:

<% Java code %>

If you need to specify a group of Java statements, there's no need to surround each one with tags. Instead, you can use the open tag, specify as many Java statements as you need, and then use the close tag. This would be the syntax:

<%
Java statement 1;
Java statement 2;
...
%>

Similar to Java programming, every Java statement specified in a scriptlet must be followed by a semicolon.

JSP expressions are Java language expressions that return a string to the page. This is the JSP syntax:

<%= Java expression %>.

In the sample provided above, under the section "A Simple Sample," the sole JSP element is an expression that returns the date and time.

JSP declarations are used to define variables and methods. JSP declarations are global to the entire page. The JSP syntax is this:

<%! Java variable or method %>

JSP actions are predefined functions that can create and use objects. The syntax of the action element must be specified in an XML syntax. Actions look like this:

<jsp:action_name attribute1="value1" attribute2="value2" ... />

A common action element is the <jsp:useBean> tag, which allows your JSP
to use JavaBeans, an important part of Java's component-based model, where reusability
and reliability can be exploited.

JSP directives tell the JSP engine how the page should be compiled. The JSP syntax is this:

<%@ directive %>

A common JSP directive is the page directive, which can be used to import Java classes used by the JSP. For example, to import the java sql and util classes, you could use the following directive:

<%@ page import="java.sql.*, java.util.*" %>

One other very important directive is the taglib directive, which allows your JSP program to employ custom tag libraries. Custom tag libraries (supported beginning with JSP 1.1) are a very powerful feature of JSP, because they allow you to reduce complex code into simple JSP tags. For example, you could define a tag (let's say <mycustomtag:getset>) to retrieve a set of records and iterate through them.

Instead of the JSP programmer writing scriptlet code to retrieve the records and loop
through them, he could simply use a single tag with number of parameters, such as the
table name, the selection criteria, and number of records to return.

JSPs Need a Servlet Engine

Web servers deliver content over the Internet using HTML, and basically only HTML. If you want to serve content using JSP, you need something in addition to a Web server; you need what is known as a servlet engine. Generally, a servlet engine can be called a Web application server. Commercial-grade Web application servers, such as IBM's WebSphere Application Server, can support multiple servlet engines configured differently for different applications.

Fortunately, there are a number Web application servers available, some powerful, scalable, and expensive, and some not so powerful and scalable, but good enough for learning and even producing working applications. Here, I will use one of the most readily available and popular--Tomcat.

Tomcat

Although Tomcat may not be considered a full-blown Web application server, it is great to use for testing and prototyping JSP applications. Tomcat is free and can be installed on just about any computer, allowing you to easily get started with the JSP technology with as little as a PC. Because Tomcat is also available for the iSeries, what you learn on your PC can be applied to the iSeries, too.

Tomcat is a servlet engine originally created by a group of open-source developers known as the Apache Java Group; it was created to work with the Apache Software Foundation's very successful Apache HTTP server. Initially, the servlet engine was called Apache JServ.

During the time the Apache Java Group was working on Apache JServ, Sun had its own servlet engine, which was eventually packaged as the Java Servlet Development Kit (JSDK). Sun had the engine that most closely adhered to the JSP and Servlet specifications, but it didn't perform well; the Apache Java Group had the engine that performed best, with the Apache HTTP server.

To make a long story short, in 1999 Sun donated its JSDK engine to ASF, and a subgroup called the Jakarta Project was formed to merge the Sun reference implementation with the Apache JServ engine. Eventually the Jakarta Project took over all the work of the Apache Java Group, and Tomcat became one of a number of projects under the Jakarta umbrella.

Where to Tomcat

To allow anyone with a PC to get started with Tomcat and run the sample application presented later, I will explain how to install and use Tomcat for the Windows platform. The Tomcat version I use here to run my sample application is 3.3, which is a reference implementation of the JSP 1.1 and servlet 2.2 specifications. You can obtain version 3.3 or the latest version of Tomcat from Apache's Jakarta Project site. On this page you'll find a download section, where you'll want to select binaries, versus the source code (only for those who want to "hack" the code). You'll also find good documentation, including an application development manual.

Installing Tomcat

All you need to get Tomcat installed is to extract the .zip file (for my download, it was jakartatomcat3.3.zip) to a directory with a name similar to jakarta-tomcat-3.3, which will become your tomcat root directory. You'll need to create a TOMCAT_HOME environment variable on your system and set it's value to the tomcat root directory you created. On my system, the TOMCAT_HOME environment variable value is e:\jakarta- tomcat-3.3.

You’ll also need a Java Software Development Kit (you can download the one available from Sun's Web site or use the one you get with the IBM WebSphere Development Studio (WDS-5722) toolkit, available with V4R5 and V5R1 iSeries systems). Before you can start the Tomcat server, you’ll need to add an environment variable named JAVA_HOME and set it to the location where you installed the Java SDK.

How to Start and Stop

Once you've extracted Tomcat from the downloaded .zip file, you'll find the bin directory under the Tomcat \bin directory, beneath the Tomcat root directory. This directory contains a number of files, two of which can be used for starting and stopping the Tomcat engine: startup.bat and shutdown.bat. (For Linux users, there are two similar files, except their extension is .sh.) I created shortcuts to each of these batch files on my desktop so I can easily start and stop Tomcat. By default, Tomcat uses port 8080.

Application paths/directories

Tomcat ships with some useful JSP and Servlet sample applications. After you start Tomcat, you can access these samples with this URL: http://localhost:8080. From the sample applications page, you can execute and view the source for the applications. I learned a lot by studying the source code of these examples. The preconfigured applications reside under the /webapps directory, in the examples directory, which is the document root of the sample applications. In the examples, you'll find a directory called jsp, and in that directory you'll find directories for each of the sample applictions, such as cal, checkbox, and num. These directories contain the JSP source files.

You will create a top-level (document root) directory for your own Web application. This is where you place the HTML files and JSPs that comprise your application's user interface. This top-level application directory should be created under the /webapps directory. For example, for the sample application I'll describe later, I created an application directory called myapp. This is the complete path to the user interface documents for myapp:

<drive:>\<tomcat_root>\webapps\myapp\<user_interface_docs>

OK, so you know where to place your JSPs. But there's one more thing you should know about your Tomcat application directories. To facilitate the distribution of your application, you need to conform to the Web Application Archive (WAR) scheme. You should create and arrange directories under your document root as follows:

As previously mentioned, place your .html and .jsp files under the document root. For more complex applications, you can create subdirectories to better organize your files (see the examples directory mentioned above).

Create a WEB-INF directory. In this directory, you will eventually create a file called web.xml, which contains what is known as the Web Application Deployment Descriptor, for your application.

Create a WEB-INF /classes/ directory to contain Java class files (and associated resources) required for your application, such as JavaBeans.

Create a WEB-INF /lib/ directory to contain JAR files that contain Java class files (and associated resources) required for your application. For example, for the sample application I present next, I copied the jt400.jar file to this directory so I can access the iSeries JDBC driver.

See JSPFigure2.jpg for an example of how I set up the directories for my sample application.

There are ways to configure your application, using .xml files, to do things like setting up logical paths (context paths) to the physical locations of your application resource. See the application development manual included with Tomcat. To access the documentation, use the following path:

<drive:><tomcat_root>\doc\index.html

iSeries Data Retrieval with JSP

The remainder of this article presents the JSP source code for a Web application that retrieves all of the source physical file names contained in library QGPL on an iSeries system. Create a directory under /webapps and call it myapp (or you could use a different name). Under the myapp directory, place the source code file you create from the source code in JSPFigure3.jsp, calling it JSP101.jsp. Make sure you set the system variables--system, user, and password--to values appropriate to your own system. If you leave them all blank, you'll be prompted for the system address, user profile, and password. The complete path looks like this:

<tomcat_root>\webapps\myapp\JSP101.jsp

Because the sample application requires the iSeries JCDB driver class, the iSeries Toolbox for Java jt400.jar file should be added to the following directory:

<tomcat_root>\webapps\myapp\web-inf\lib

(There are other ways to gain access to additional class files, but this method conforms to what the Tomcat documentation recommends.) You can download the latest version of the jt400.jar file, along with all other iSeries-specific Java jar files, from IBM’s Web site.

To execute the sample application, start the Tomcat engine (startup.bat), open your browser, and submit the following URL:

http://localhost:8080/myapp/JSP101.jsp

After a few seconds, you should see a screen that looks similar to the one in JSPFigure4.jpg.

Examine the code in Figure 3. You should recognize the JSP elements and be able to match them to some of the JSP elements I described earlier. The JSP basically executes an SQL select to retrieve the information and then iterates through the result set with a while loop. Notice that HTML code is embedded in between the JSP code, offering powerful dynamic page creation.

That Wasn’t So Hard!

As you’ve seen, getting started with Java, and even developing a Java Web application, isn't really all that difficult, not when you use the power of JSPs.

Richard Shaler is a contributing editor to Guild Companies. He can be reached at rshaler@itjungle.com.

 

Sponsored By
WORKSRIGHT SOFTWARE, INC.

Indiana and Arkansas have new area codes! How are you going to update your customer files?

We have the answer. Our ZIP/CITY System for the AS/400 can automatically update your customer files for this area code split and future ones as well.

Visit our Web site www.worksright.com to learn more about ZIP/CITY. We offer a free, no-hassle, 30-day trial. Phone, fax, e-mail us, or order your free trial directly from our Web site.

THIS ISSUE
SPONSORED BY:
Help/Systems
SoftLanding Systems
BCD Software Int'l
Jacada Ltd.
RJS Software Systems
WorksRight Software, Inc.
BACK ISSUES
TABLE OF CONTENTS
Welcome to Midrange Programmer, OS/400 Edition
Determining a PC's IP Address
I Lost My License Key!
Calling a Program Using the iSeries Toolbox for Java
RPG Prototyping
JavaServer Pages 101
  Newsletters | Subscribe | Advertise | About Us | Contact | Search | Home  
  Last Updated: 1/17/02
Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.