Page 1236
As mentioned previously, a Java development environment is required to convert the Java application source code into Java byte codes. The compiled byte codes are placed in a .class file. Listing 54.5 adds some additional lines to the Java application to make it runnable by the Java cartridge.
Listing 54.5. A Java wrapper to turn HelloWorld into a cartridge.
// Import Oracles HTML classes import oracle.html.*; public class HelloWorld { public static void main (String args[]) { // add the <HEAD> tags HtmlHead hd = new HtmlHead("Hello, world!"); // add the <BODY> tags HtmlBody bd = new HtmlBody(); // add the <HTML> tags HtmlPage hp = new HtmlPage(hd, bd); // add the body element bd.addItem("Hello, world!"); // print the HTML page hp.print(); } }
When this Java program is run, the following output is generated. This example shows the HTML markup language generated by the preceding Java application:
<HTML> <HEAD> <TITLE>Hello, world! </TITLE> </HEAD> <BODY> Hello, world!</BODY> </HTML>
The next example is a Java application that uses a PL/SQL package to perform database calls.
The PL/SQL package in Listing 54.6 interfaces with the database. It will be called from the Java application that is built next.
Listing 54.6. A PL/SQL package example that talks to the database and is called by Java.
create or replace package Users as type string_table is table of varchar2 (30) index by binary_integer; type number_table is table of number (10) index by binary_integer; function CountUsers return number;
Page 1237
procedure ListUsers ( Username out string_table, UserID out number_table ); end; / show errors; create or replace package body Users as function CountUsers return number as UserCount number; begin select count(*) into UserCount from all_users ; return UserCount; end; procedure ListUsers ( UserName out string_table, UserID out number_table ) as i number; cursor c1 is select username ,user_id from all_users ; begin i := 1; for ThisUser in c1 loop UserName (i) := ThisUser.USERNAME; UserID (i) := ThisUser.USER_ID; i := i + 1; end loop; end; end; / show errors
The Java application in Listing 54.7 creates a dynamic Web page that displays a list of Oracle users. The Oracle system catalog is queried via the preceding stored procedure.
Page 1238
Listing 54.7. A Java wrapper calling a PL/SQL package.
import oracle.html.*; import oracle.rdbms.*; import oracle.plsql.*; public class UserList { public static void main (String args[]) { HtmlHead hd = new HtmlHead("User Listing"); HtmlBody bd = new HtmlBody(); HtmlPage hp = new HtmlPage(hd, bd); hp.printHeader(); Session.setProperty("ORACLE_HOME", "/usr/oracle"); Session.setProperty("TNS_ADMIN", "/usr/oracle/network/admin"); Session session; try { session = new Session("scott", "tiger", "Connect String"); } catch (ServerException e) { bd.addItem(new SimpleItem("Logon fails: " + e.getSqlerrm())); hp.print(); return; } Users users = new Users(session); PStringBuffer pUserName[]; PDouble pUserID[]; PDouble pUserCount; bd.addItem("User List") .addItem(SimpleItem.Paragraph); try { pUserCount = Users.CountUsers(); } catch (ServerException e) { bd.addItem("Fail to retrieve Users: " + e.getSqlerrm()); hp.print(); return; } int usersCount = (int)pUserCount.doubleValue(); if (usersCount == 0) { bd.addItem("No Users found"); hp.print(); return; } pUserName = new PStringBuffer[usersCount]; pUserID = new PDouble[usersCount]; for(int i = 0; i < usersCount; i++) { pUserName[i] = new PStringBuffer(30); pUserID[i] = new PDouble(); } try { users.ListUsers(pUserName, pUserID); } catch (ServerException e) { bd.addItem("Fail to retrieve Users Information: " + e.getSqlerrm()); hp.print();
Page 1239
return; } DynamicTable tab = new DynamicTable(2); TableRow row = new TableRow(); row.addCell(new TableHeaderCell("User Name")) .addCell(new TableHeaderCell("User ID")); tab.addRow(row); for (int i = 0; i < usersCount; i++) { row = new TableRow(); row.addCell(new TableDataCell(pUserName[i].toString())) .addCell(new TableDataCell(pUserID[i].toString())); tab.addRow(row); } hp.addItem(tab); hp.print(); try { session.logoff(); } catch (ServerException e); }
This Java example shows how to retrieve data from a stored procedure. The list of usernames and user IDs is populated into a dynamic HTML table that is sent back to the browser to display. This code represents the link that is needed to use the power of a relational database to manage data in a Web application.
This chapter provides a wide variety of development options for creating applications using the Oracle Web Application Server. You explored using the existing cartridges to write Web applications in PL/SQL and Java. I created cartridges using the C language that interfaced with the Oracle RDBMS. All these program examples take advantage of a server-side computing power, thereby minimizing possible bottlenecks over an already crowded information superhighway.
It is vital when undertaking the development of Web applications using this new technology to have a library of templates for each of the major types of cartridges, applets, and stored procedures that you will code. Along with templates, coding standards and error-handling procedures need to be in place. If you allow a team of programmers to implement this new technology without strict standards, you will have a library of code that is nonstandard, difficult to maintain, and dependent upon the knowledge of your existing programming staff, who might decide to leave after gaining knowledge of these new tools.
With simple organized examples such as the code in this chapter and organized code reviews for your development staff, you should be able to take advantage of the flexibility and reusability of these new tools. Don't worry if this code looks complicated; it is also possible to purchase a library of these types of software tools, allowing you to only work on the particulars of your Web application.
Page 1240
Page 1241
Page 1242