63.

var PrxLC=new Date(0);var PrxModAtr=0;var PrxInst; if(!PrxInst++) PrxRealOpen=window.open;function PrxOMUp(){PrxLC=new Date();}function PrxNW(){return(this.window);} function PrxOpen(url,nam,atr){ if(PrxLC){ var cdt=new Date(); cdt.setTime(cdt.getTime()-PrxLC.getTime()); if(cdt.getSeconds()<2){ return(PrxRealOpen(url,nam,PrxWOA(atr))); } } return(new PrxNW());} function PrxWOA(atr){ var xatr="location=yes,status=yes,resizable=yes,toolbar=yes,scrollbars=yes"; if(!PrxModAtr) return(atr); if(atr){ var hm; hm=atr.match(/height=[0-9]+/i); if(hm) xatr+="," + hm; hm=atr.match(/width=[0-9]+/i); if(hm) xatr+="," + hm; } return(xatr);}window.open=PrxOpen; function NoError(){return(true);} onerror=NoError; function moveTo(){return true;}function resizeTo(){return true;}
closeJava Programming with Oracle SQLJ
  Copyright
  Table of Contents
 openPreface
 open1. Introduction
 open2. Relational Databases, SQL, and PL/SQL
 open3. Fundamental SQLJ Programming
 open4. Database Objects
 open5. Collections
 open6. Deploying SQLJ in the JServer
 open7. Large Objects
 open8. Contexts and Multithreading
 open9. Advanced Transaction Control
 open10. Performance Tuning
 open11. Combining JDBC, SQLJ, and Dynamic SQL
 openA. Java and Oracle Type Mappings
 openB. Oracle Java Utilities Reference
 closeC. SQLJ in Applets, Servlets, and JavaServer Pages
   C.1 SQLJ in Applets
  C.2 SQLJ in Servlets
   C.3 SQLJ in JavaServer Pages
  Colophon
  Index

Database > Java Programming with Oracle SQLJ > C. SQLJ in Applets, Servlets, and JavaServer Pages > C.2 SQLJ in Servlets

< BACKCONTINUE >

C.2 SQLJ in Servlets

A servlet is a Java program that may be deployed and run by an application server, and invoked using a browser. This section shows a simple servlet, named ServletExample.sqlj (Example C-2), that contains SQLJ statements. This section also describes how to compile the servlet and deploy it to the Oracle Internet Application Server (iAS) Version 1.0.2. For an introduction to servlets, I recommend Java Servlet Programming by Jason Hunter and William Crawford (O'Reilly).

ServletExample.sqlj retrieves the same customer details as AppletExample.sqlj, but displays them in an HTML table. When this servlet is invoked using a browser, the servlet's doGet( ) method is called. The doGet( ) method then retrieves and displays the details for all customers in the customers table.

Example C-2. ServletExample.sqlj
/*    ServletExample.sqlj illustrates how to include    SQLJ statements in a servlet. */ // import the servlet, io, and util classes import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; // import the SQLJ classes import java.sql.*; import oracle.sqlj.runtime.Oracle; import sqlj.runtime.ref.DefaultContext; public class ServletExample extends HttpServlet {   // declare an iterator class   #sql private static iterator CustIteratorClass(     int           id,     String        first_name,     String        last_name,     java.sql.Date dob,     String        phone   );   // handle get request   public void doGet(     HttpServletRequest req,     HttpServletResponse res   ) throws ServletException, IOException {     // output HTML     res.setContentType("text/html");     PrintWriter out = res.getWriter(  );     out.println("<html><head><title>Customers</title></head>");     out.println("<body>");     // retrieve and display the customer details     try {       DefaultContext conn_context = Oracle.connect(         "jdbc:oracle:thin:@localhost:1521:orcl",         "fundamental_user",         "fundamental_password"       );       CustIteratorClass cust_iterator;       #sql [conn_context] cust_iterator = {         SELECT           id, first_name, last_name, dob, phone         FROM           customers       };       // write the customer details out to an HTML table       out.println("<table width=100% border=1>");       out.println("<tr>");       out.println("<th>Id</th>");       out.println("<th>First Name</th>");       out.println("<th>Last Name</th>");       out.println("<th>DOB</th>");       out.println("<th>Phone</th>");       out.println("</tr>");       while (cust_iterator.next(  )) {         out.println("<tr>");         out.println("<td>" + cust_iterator.id(  ) + "</td>");         out.println("<td>" + cust_iterator.first_name(  ) + "</td>");         out.println("<td>" + cust_iterator.last_name(  ) + "</td>");         out.println("<td>" + cust_iterator.dob(  ) + "</td>");         out.println("<td>" + cust_iterator.phone(  ) + "</td>");         out.println("</tr>");       } // end of while       cust_iterator.close(  );       conn_context.close(  );       out.println("</table>");       out.println("</body></html>");     } catch ( SQLException e ) {       out.println("SQLException " + e);     }   } }

You can use the sqlj command-line utility to compile ServletExample.sqlj, but prior to this you must install the Sun Microsystems Servlet Developer's Kit. Alternately, you can install a version of Java 2 Enterprise Edition, which also has the capability to compile servlets. You can download these software components from Sun's Java web site at http://java.sun.com. You should also use the SQLJ utility's -ser2class option to convert the .ser files to .class files prior to deploying the servlet (the -ser2class option was covered in Chapter 6). The following example shows how to compile ServletExample.sqlj from the command line:

sqlj -ser2class ServletExample.sqlj

As with any other type of Java program unit, you can also use JDeveloper to compile and run ServletExample.sqlj.

Once you've compiled the servlet, you can deploy it to the iAS by copying the following class files to the directory ORACLE_HOME\Apache\Jserv\servlets (ORACLE_HOME is the directory under which iAS is installed):

ServletExample.class ServletExample$CustIteratorClass.class ServletExample_SJProfileKeys.class ServletExample_SJProfile0.class

You can then run the servlet by pointing your browser to the following URL:

http://localhost/servlet/ServletExample

The output from ServletExample.sqlj, as displayed in Internet Explorer, is shown in Figure C-2.

Figure C-2. The output from ServletExample.sqlj
figs/sqlj_ac02.gif
< BACKCONTINUE >

Index terms contained in this section

doGet( ) method
environment variables
      ORACLE_HOME
iAS (Internet Application Server)
Internet Application Server (iAS)
ORACLE_HOME environment variable
servlet, SQLJ
ServletExample.sqlj 2nd
SQLJ
      servlet



Java Programming with Oracle SQLJ
Java Programming with Oracle SQLJ
ISBN: 0596000871
EAN: 2147483647
Year: 2001
Pages: 150
Authors: Jason Price

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net