This
interface defines the remote methods available for * the Customer
object. */
public interface CustomerInterface extends java.rmi.Remote {
/** *
Retrieves the customer data for the given customer
ID * number. If the customer is not found a null value will be *
returned. * * @param id Customer ID number * @return
CustomerData object containing all of the customer * data or null if not
found */ CustomerData getCustomerData(String id) throws
java.rmi.RemoteException, java.sql.SQLException; }
Default
constructor. * * @param con JDBC Connection to use for the
query */ public Customer(java.sql.Connection con) throws
java.rmi.RemoteException,
java.sql.SQLException { super(); m_con = con;
// Create a prepared statement for us to use String sql = "SELECT
Custno, Name, Balance from Customer " + "WHERE Custno = ?";
m_ps = con.prepareStatement(sql); }
/** *
Retrieves the customer data for the given customer ID * number.
If the customer is not found a null value will be *
returned. * * @param id Customer ID number * @return
CustomerData object containing all of the customer * data or null if not
found */ public CustomerData getCustomerData(String id) throws
java.rmi.RemoteException, java.sql.SQLException { CustomerData data
= null;
System.out.println("Customer query for " + id);
//
Set the customer ID m_ps.setInt(1, Integer.parseInt(id));
//
Execute the query java.sql.ResultSet rs = m_ps.executeQuery();
// Get the results. If there are no results available, // return
null to the client if (rs.next()) {
// A row exists. Create a new CustomerData object and // fill it
in data = new CustomerData(); data.id =
rs.getString(1); data.name = rs.getString(2); data.balance =
rs.getBigDecimal(3, 2); }
// Close the ResultSet rs.close();
return
data; }
/** *
Main entry point for the remote object. This
method * will bootstrap the object and register it with the * RMI
registry. */ public static void main(String
args[]) { // Install the default RMI security
manager System.setSecurityManager(new
java.rmi.RMISecurityManager());
try {
// Register the JDBC
driver Class.forName(m_driver).newInstance();
This is a simple servlet that will return
customer * information. Note that this servlet implements the *
SingleThreadModel interface which will force the Web browser * to
synchronize all requests */
public class CustomerInfo extends HttpServlet implements
SingleThreadModel { /** *
Performs the HTTP POST
operation * * @param req The request from the client * @param
resp The response from the servlet */
public void doPost(HttpServletRequest req, HttpServletResponse
resp) throws ServletException, java.io.IOException {
// Create a PrintWriter to write the response java.io.PrintWriter
out = new java.io.PrintWriter(resp.getOutputStream());
// Set the content type of the
response resp.setContentType("text/html");
// Print the HTML
header out.println(""); out.println("
//
Get the Customer id String id = null; String values[] =
req.getParameterValues("CustomerID"); if (values != null) { id =
values[0]; }
// The target host. Change to 'rmi://yourhost/' when the // server
runs on a remote machine String host = "";
try {
// Attempt to bind to the remote host and get the // instance of
the Customer object CustomerInterface cust =
(CustomerInterface) java.rmi.Naming.lookup(host + "Customer");
// We have an instance of the remote object. Get the // customer
information CustomerData data = cust.getCustomerData(id);
// Format the HTML out.println("The current balance for " +
data.name + " is $" + data.balance); } catch (Exception
ex) {
// Turn exceptions into a servlet
exception ex.printStackTrace(); throw new
ServletException(ex.getMessage()); }
// Wrap
up out.println(""); out.flush(); out.close(); }
/** *
Initialize the servlet. This is called once when
the * servlet is loaded. It is guaranteed to complete before any *
requests are made to the servlet * * @param cfg Servlet
configuration information */
public void init(ServletConfig cfg) throws
ServletException { super.init(cfg); }
/** *
Destroy the servlet. This is called once when the
servlet * is unloaded. */
public void
destroy() { super.destroy(); } } 图16.6
CustomerInfo.java代码清单