Java Q&A 
by Cliff Berg
   
Example 1:

public Customer getCustomer(String firstname, String lastname);
public Customer[] getCustomers();
public void addCustomer(String firstname, String lastname);
public void remCustomer(String firstname, String lastname) ;


Example 2

<html>
Customer Status...<br>
<servlet name=CustomerStatus code=customer.CustomerStatusImpl>
<param name=firstname value=abe>
<param name=lastname value=lincoln>
</servlet>
<br>
</html>


Listing One
/* CustomerManagerImpl.java
 * Copyright 1997 by Digital Focus. May be used for non-commercial purposes.
 * No warrantee or guaranteed is given or implied. */
 
package customer;
import java.io.*;
import javax.servlet.*;
/** Servlet which has dual-personalities: http and RMI.
 * Allows you to write services that can be accessed from any browser, and 
 * across a firewall without tunneling. */
public class CustomerManagerImpl 
   extends java.rmi.server.UnicastRemoteObject
    implements CustomerManager, Servlet
{
    private ServletConfig servletConfig;
    private java.util.Vector customers;
    static
    {
        // Set security manager for RMI invocation, to protect 
        //    against malicious stubs
        if (System.getSecurityManager() == null)
        {
            System.setSecurityManager(new java.rmi.RMISecurityManager());
            System.out.println("Servlet server has no security manager; 
                                                using RMI security manager");
        }
    }
    public CustomerManagerImpl() throws java.rmi.RemoteException
    {
        super();
    }
    // CustomerStatus methods. The remote entry point implementations are 
    // synchronized, because they may be called my multiple server threads, 
    // either as an RMI service, or as a servlet service.
    public synchronized Customer getCustomer(String firstname, 
                String lastname) throws java.rmi.RemoteException, Exception
    {
        return findCustomer(firstname, lastname);
    }
    public synchronized Customer[] getCustomers() 
                                  throws java.rmi.RemoteException, Exception
    {
        Customer[] ca = new Customer[customers.size()];
        for (int i = 0; i < customers.size(); i++)
        {
            Customer c = (Customer)(customers.elementAt(i));
            ca[i] = c;
        }
        return ca;
    }
    public synchronized void addCustomer(String firstname, String lastname, 
                           int age) throws java.rmi.RemoteException, Exception
    {
        Customer c = new Customer(firstname, lastname, age);
        customers.addElement(c);
        save();
    }
    public synchronized void remCustomer(String firstname, 
                   String lastname) throws java.rmi.RemoteException, Exception
    {
        Customer c = findCustomer(firstname, lastname);
       if (c == null) throw new Exception("Not found");
        customers.removeElement(c);
        save();
    }
    protected Customer findCustomer(String firstname, String lastname)
    {
        for (int i = 0; i < customers.size(); i++)
        {
            Customer c = (Customer)(customers.elementAt(i));
            if (c.getFirstname().equals(firstname) && 
                                            c.getLastname().equals(lastname))
            {
                return c;
            }
        }
        return null;
    }
    protected synchronized void load() throws Exception
    {
        // synchronized to protect access to customers object
        System.out.println("Loading CustomerFile...");
        File file = new File("CustomerFile");
        FileInputStream fis = null;
        try { fis = new FileInputStream(file); } 
                                   catch (FileNotFoundException ex) {}
        if (fis == null)
        {
            customers = new java.util.Vector();
        }
        else
        {
            ObjectInputStream ois = 
                        new ObjectInputStream(new FileInputStream(file));
            customers = (java.util.Vector)(ois.readObject());
            ois.close();
        }
        System.out.println("...done loading.");
    }
    protected synchronized void save()
    {
        // synchronized to protect access to customers object
        System.out.println("Saving CustomerFile...");
        File file = new File("CustomerFile");
        try
        {
            ObjectOutputStream oos = 
                         new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(customers);
            oos.close();
        }
        catch (IOException ex)
        {
            System.out.println("Error writing customer file:");
            ex.printStackTrace();
       }
        System.out.println("...done saving.");
    }
    // Servlet methods...
    public void destroy()
    {
        // Save file
        save();
    }
    public ServletConfig getServletConfig()
    {
        return servletConfig;
    }
    public String getServletInfo()
    {
        return "CustomerStatus Servlet";
    }
    public void init(ServletConfig c) throws ServletException
    {
        servletConfig = c;
        init();
    }
    public void init() throws ServletException
    {
        try
        {
            java.rmi.registry.Registry registry = 
                            java.rmi.registry.LocateRegistry.getRegistry();
            registry.rebind("CustomerManager", this);
            System.out.println("Server bound to CustomerManager");
        }
        catch (Exception ex)
        {
            System.out.println("Could not bind:");
            ex.printStackTrace();
        }
        // Load file
        try
        {
            load();
        }
        catch (Exception ex)
        {
            System.out.println("Error reading customer file:");
            ex.printStackTrace();
            throw new ServletException("Terminating due to prior error");
        }
    }
    public static void main(String[] args)
    {
        try
        {
            CustomerManagerImpl m = new CustomerManagerImpl();
            m.init();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    public void service(ServletRequest request, ServletResponse response) 
        throws ServletException, IOException
    {
        System.out.println("Service...");
        // Identify customer
        String firstname = request.getParameter("firstname");
        String lastname = request.getParameter("lastname");
        if ((lastname == null) || (firstname == null))
        {
            response.getOutputStream().println("Both last name and 
                                              first name must be specified");
            return;
        }
        // Retrieve information on the customer
        System.out.println("firstname=" + firstname);
        System.out.println("lastname=" + lastname);
        Customer customer = null;
        try
        {
            customer = getCustomer(firstname, lastname);
        }
        catch (Exception ex)
        {
            throw new ServletException("Error looking up customer.");
        }
        
        // Return information on the customer
        if (customer == null)
        {
          response.getOutputStream().println("Customer could not be found.");
          return;
        }
        response.getOutputStream().println(customer.toString());
    }
}



