Chris Pollett > Students >
Rekha

    ( Print View )

    [Bio]

    [CS297Proposal]

    [Del1]

    [Del2]

    [Del3]

    [Del4]

    [CS297 Report-PDF]

    [CS298 Proposal]

    [CS298 Presentation-PDF]

    [Game Description-PDF]

    [Final Game-ZIP]

    [CS298 Report-PDF]

                          

























Deliverable_Two

Description: The purpose of this deliverable is to establish connection between two wireless devices and transfer information wirelessly. This deliverable consists of three classes-TransferRecord.java, RegisterServlet.java, and ListingServlet.java,TransferRecord.java is the application to be loaded onto the wireless device.

Example:This is the Welcome screen for the application. A menu is presented to the user. Using this menu, the user can add, delete, list, transfer, receive records.

Welcome Screen of the application

Example:Add a record screen

GUI screen

Example:This is the screen that gets displayed when the user selects the transfer option. Using this screen, the user can select the record to be transferred to other device.

Search screen

Example:This is the screen that shows the result of the search

Search Result screen

Example:This is the screen shows the list of destinations available

Search Result screen

Example:This is the receive screen. The Receive button on this screen will start the server that receives the record.

Receive screen

Example:This is the success screen for the record transfer and also record receive.

Success screen

The welcome screen of the application has a menu with the options- Register, Add Record, Transfer Record ,Receive Record, Delete Record, List Records, Quit. When Register option is selected, Register Screen comes up and then when Register button is selected,the device's IP address gets registered at the central server. When Add Record option is selected, the user is prompted with the input screen where he can enter the Name and Phone Number and add the record into the local record store.The Receive Record option is to be selected by the user who wishes to receive a record from the other user. When this option is selected, the server is started and it waits until it receives any record.The Transfer Record option is to be selected by the user of the device who wishes to transfer a record. When this option is selected, the Record Search Screen is displayed with which the user can select the record that he wishes to transfer by specifying the Name value of the record. Then he has to select List Destinations option for displaying all the destinations registered at the central server. From the list of the destinations, he can select a destination for the record transfer. Then it connects with the receiving device and transfers the record. Then the Success Screen is displayed at the receiver's end. The Delete Record option lets the user delete a record. The List Records option lets the user list all the records in the local record store. The RegisterServlet.java connects to the Oracle database and puts the client's IP address into it. The ListingServlet.java connects to the Oracle database and queries it and returns a list of the IP addresses that are registered with the server. The connection between the two wireless devices in this deliverable is achieved using DatagramConnection.

package TransferRecord;

/**

   COPYRIGHT (C) 2003 Rekha Vaddepalli.

   Class to do the functionality of transferring the record
   from one wireless device to the other.

   Solves Deliverable 2

   @author Rekha Vaddepalli

   @version 1.00 2003/11/15

*/

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.util.Enumeration;
import java.util.Vector;
import java.io.*;

/**

 */
public class TransferRecord extends MIDlet
implements CommandListener
{

  private static String registerUrl =
   "http://anouki:8088/examples/servlet/RegisterServlet";
  private static String listingUrl =
   "http://anouki:8088/examples/servlet/ListingServlet";

  private Display display; // Declaring variables for Display class.
  Command search; // Declaring variables for Buttons in the UI.
  Command quit; // Declaring variables for Buttons in the UI.
  Command register; // Declaring variables for Buttons in the UI.
  Command addnow;
  Command delete;
  Command transfer; // Declaring variables for Buttons in the UI.
  Command mainmenu; // Declaring variables for Buttons in the UI.
  Command destSel;
  Command listRec;
  Command okCommand = new Command("OK", Command.OK, 1);
  String trRec = "";
  Command listDest;
  Command receiveRec;

  List menu; // Declaring a Menu(List)
  Form ui_form;
  StringItem si;
  TextField name; // TextField For storing the name...
  TextField phone;
  TextField dest;
  TextField hname;

  RecordStore recordStore = null;
  // Declaring a RecordStroe(Database....)

  private StreamConnection streamConnection = null;

  // use OutputStream to send requests
  private OutputStream outputStream = null;
  private DataOutputStream dataOutputStream = null;

  // use InputStream to receive responses from Web server
  private InputStream inputStream = null;
  private DataInputStream dataInputStream = null;


/**
   Method Name: TransferRecord
   Description: Constructor for the class
   Purpose: Initialize the Command buttons required for the UI
*/

  public TransferRecord()
  {
   display = Display.getDisplay(this); // Initializing the Display..
   quit = new Command("Quit", Command.SCREEN, 3);
   // initializing the Button..
   search = new Command("Search", Command.SCREEN, 2);
   // initializing the Button..
   register = new Command("Register", Command.SCREEN, 2);
   // initializing the Button..
   addnow = new Command("Add", Command.SCREEN, 2);
   // initializing the Button..
   delete = new Command("Delete", Command.SCREEN, 2);
   // initializing the Button..
   mainmenu = new Command("Main", Command.SCREEN, 2);
   // initializing the Button..
   destSel = new Command("Select Dest", Command.SCREEN, 2);
   transfer = new Command("Transfer Record", Command.SCREEN, 2);
   listDest = new Command("List Destinations", Command.SCREEN, 2);
   listRec = new Command("List Records", Command.SCREEN, 2);
   receiveRec = new Command("Receive", Command.SCREEN, 2);

   // Initializing the Record Store
   try
   {
    recordStore = RecordStore.openRecordStore("addresses1", true);
   }
   catch (RecordStoreException rse)
   {
    rse.printStackTrace();
   }
   }

/**

   Do the startApp(). This method initializes the application

   It starts the application by displaying the menu

*/


   public void startApp()
   {
    menu = new List("Transfer Records...", List.IMPLICIT);
    menu.append("1. Register", null);
    menu.append("2. Add Record", null);
    menu.append("3. Transfer Record", null);
    menu.append("4. Receive Record", null);
    menu.append("5. Delete Record", null);
    menu.append("6. List Records", null);
    menu.append("7. Quit", null);
     menu.setCommandListener(this);
    display.setCurrent(menu);
   }
/**

   This method displays the screen to do the registration
   of the IP address with the central server.

 */

   void registerScreen()
   {
    ui_form = new Form("Register...");
    hname = new TextField("Host Name:", "", 50, 0);
    ui_form.append(hname);
    ui_form.addCommand(register);
    ui_form.addCommand(mainmenu);
    ui_form.addCommand(quit);
    ui_form.setCommandListener(this); // Invoking Action Listener..
    display.setCurrent(ui_form);
   }

/**

   This method displays the screen to start the transfer of the record

*/

   void transferScreen()
   {
    searchScreen();
   }

/**

   This method displays the screen to start receiving the record

*/

   void receiveScreen()
   {
    ui_form = new Form("Receive Record...");
       ui_form.addCommand(receiveRec);
    ui_form.addCommand(mainmenu);
    ui_form.addCommand(quit);
       ui_form.setCommandListener(this); // Invoking Action Listener..
    display.setCurrent(ui_form);
   }

/**

   This method displays the screen to start the search for the record

*/

   void searchScreen()
   {
    ui_form = new Form("Search An Address");
    name = new TextField("Name to Search..", "", 50, 0);
    ui_form.append(name);
    ui_form.addCommand(search); // Adding Command Button to the ui_form.
    ui_form.addCommand(quit); // Adding Command Button to the ui_form.
    ui_form.setCommandListener(this); // Invoking Action Listener..
    display.setCurrent(ui_form);
   }

/**

   This method displays the screen to delete the record

*/


   void deleteScreen()
   {
    ui_form = new Form("Delete An Address");
    name = new TextField("Name to Delete..", "", 50, 0);
    ui_form.append(name);
    ui_form.addCommand(delete); // Adding Command Button to the ui_form.
    ui_form.addCommand(quit); // Adding Command Button to the ui_form.
    ui_form.setCommandListener(this); // Invoking Action Listener..
    display.setCurrent(ui_form);
   }

/**

   This method displays the screen to select the destinations for the record

*/

   void destinationSelectScreen()
   {
    ui_form = new Form("Select the Destination..");
    ui_form.addCommand(listDest);
    //get the list and set the text
    ui_form.addCommand(quit);
    ui_form.setCommandListener(this); // Invoking Action Listener..
    display.setCurrent(ui_form);
   }

   // GUI for the Addition Screen Screen.......
   void addScreen()
   {
    ui_form = new Form("Add an Entry..");
    name = new TextField("Name ..", "", 50, 0);
    ui_form.append(name);
    phone = new TextField("Phone No.. ", "", 50, 0);
    ui_form.append(phone);
    ui_form.addCommand(addnow); // Adding Command Button to the ui_form.
    ui_form.addCommand(quit); // Adding Command Button to the ui_form.
    ui_form.setCommandListener(this); // Invoking Action Listener..
    display.setCurrent(ui_form);
   }

/**

   This method pauses the application

*/


   public void pauseApp()
   {
    menu = null;
   }


/**

   This method says what to do when the application is destroyed

*/

   public void destroyApp(boolean unconditional)
   {
    menu = null;
       notifyDestroyed(); //   Destroy...
   }

/**

   This method tells what to do when each command is clicked
   @param Command
   @param Displayable

*/

   public void commandAction(Command c,Displayable d)
    // Event handling for the Button..
   {
   //When quit command is selected
   if (c == quit)
   {
     try
     {
      close();
     }
     catch (RecordStoreException rse)
     {
       rse.printStackTrace();
     }
      destroyApp(true);
   }
   //when search command is selected
   else if (c == search)
      // When Search button is pressed (search_add is called)..
   {
     String temp_search = name.getString();
     System.out.println(temp_search);
     search_add(temp_search);
   }
   //when mainmenu command is selected
   else if (c == mainmenu) // To return to Main Menu...
   {
      startApp();
   }
   //when register command is selected
   else if (c == register)
   {
    registerAddress();
   }
   //when add command is selected
    else if (c == addnow)
   {
     String temp_name = name.getString();
     String temp_phone = phone.getString();
        address_add(temp_name, temp_phone);
   }
   //when Destination Select command is selected
   else if (c == destSel)
   {
     destinationSelectScreen();
   }
   //when delete command is selected
   else if (c == delete)
   // When delete button is pressed (delete_add) is called
   {
    String temp_delete = name.getString();
    delete_add(temp_delete);
   }
   else if (c == transfer)
   //when transfer command is selected
   {
    String hostname;
    hostname = dest.getString();
    transferRecord(hostname);
   }
   else if (c == receiveRec)
   //when Receive command is selected
   {
    startServer();
   }
   else if (c == listDest)
   //when List Destinaions command is selected
   {
    getDestinations();
   }
   else
   {
   List down = (List) display.getCurrent();
   switch (down.getSelectedIndex())
   {
    case 0 :
         registerScreen();
         break;
    case 1 :
         addScreen();
         break;
    case 2 :
         transferScreen();
         break;
    case 3 :
         receiveScreen();
         break;
    case 4 :
         deleteScreen();
         break;
    case 5 :
         listRecords();
         break;
    case 6 :
         destroyApp(true);
         break;
    }

   }
  }

/**

   This method does the functionality of the listening
   server when the receive command is selected. This
   starts listening at the specified port until the record is received.

*/
  void startServer()
  {
    DatagramConnection receiver;
    try
    {
     receiver = (DatagramConnection)Connector.open("datagram://:32767");
     byte[] buffer = new byte[256];
     Datagram dgram = receiver.newDatagram(buffer,buffer.length);
     String recvRec;
         while (true)
         {
         System.out.println("accepted connection");
         dgram.setLength(buffer.length);
         receiver.receive(dgram);
         int length = dgram.getLength();
         recvRec = dgram.readUTF();
           if(recvRec!="")
            break;
         }
        System.out.println("Received the record\n");
     System.out.println(recvRec);
     String recvname = recvRec.substring( 0,recvRec.indexOf("?"));
     String recvphno = recvRec.substring( recvRec.indexOf("?")+1);
     address_add(recvname,recvphno);
     ui_form = new Form("Receive Results");
     ui_form.append("Received the record\n\n");
     ui_form.append("Name: "+recvname+"\n");
     ui_form.append("Phone No: "+recvphno);
     ui_form.addCommand(okCommand);
     ui_form.addCommand(mainmenu);
     ui_form.addCommand(quit);
      }
      catch(IOException ioe)
      {
      ioe.printStackTrace();
   }
   }

/**

   This method lists the records in the RecordStore

*/
   void listRecords()
   {
     String temp = " ";
     String phone_number;
     String person_name;
       try
       {
      RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
      ui_form = new Form("Listing Records..");
      while (re.hasNextElement())
      {
       String name1 = new String(re.nextRecord());
       person_name = name1.substring(2, name1.indexOf("?"));
       phone_number = name1.substring(name1.indexOf("?") + 1);
       temp = temp+ "\nName.."+ person_name+ "\nPhone.."+ phone_number;
      }
      ui_form.append(temp);
      ui_form.addCommand(quit); // Adding Command Button to the ui_form.
      ui_form.addCommand(mainmenu);
      // Adding Command Button to the ui_form.
      ui_form.setCommandListener(this); // Invoking Action Listener..
      display.setCurrent(ui_form);
      }
      catch (RecordStoreNotOpenException rsnoe)
      {
       rsnoe.printStackTrace();
      }
      catch (InvalidRecordIDException irid)
      {
       irid.printStackTrace();
      }
      catch (RecordStoreException rse)
      {
       rse.printStackTrace();
      }

   }

/**

   This method deletes the record given the Name value of the record

*/
   void delete_add(String address) // Function for deletion....
   {
      String temp = " ";
      String phone_number;
      String person_name;
      int i = 1;
      int del_id = 0;
      try
      {
      RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
      ui_form = new Form("Delete Result..");

      while (re.hasNextElement())
      {
      String name1 = new String(re.nextRecord());
      try
      {
      person_name = name1.substring(2, name1.indexOf("?"));
      }
      catch (Exception ef)
      {
      person_name = "check";
      }
      if (person_name.equals(address))
      {
      del_id = i;
      }
      i++;
      }
      if (del_id != 0)
      {
      recordStore.deleteRecord(del_id);
      temp = "One Record Deleted Successfully...";
      }
      else
      {
      temp = "The Record is not Listed...";
      }
      }
      catch (Exception e)
      {
      }
      ui_form.append(temp);

      ui_form.addCommand(quit); // Adding Command Button to the ui_form.
      ui_form.addCommand(mainmenu); // Adding Command Button to the ui_form.

      ui_form.setCommandListener(this); // Invoking Action Listener..

      display.setCurrent(ui_form);

   }
/**

   This method gets the destinations registered with the server

*/

   void getDestinations()
   {
      ListForm l = new ListForm("list");
      display.setCurrent(l);
      l.start();
   }

/**

   This method registers the IP address with the central server

*/
   void registerAddress()
   {
      int hostname = 0;
      try
      {
      hostname = com.ibm.oti.connection.socket.
         Socket.getHostByNameImpl(hname.getString());
      System.out.println(com.ibm.oti.connection.
      socket.Socket.addressToString(hostname));
      }
      catch (IOException ioe)
      {
      ioe.printStackTrace();
      }

      System.out.println(hostname);
      StatusForm f = new StatusForm(hname.getString(),
      com.ibm.oti.connection.socket.Socket.addressToString
                                          (hostname));
      display.setCurrent(f);
      f.start();

   }

/**

   This method transfers the record given the remote host name

*/
   void transferRecord(String remotehost)
   {
      try
      {
      DatagramConnection sender = (DatagramConnection)Connector.open(
      "datagram://"+remotehost+":32767");
      Datagram dgram = sender.newDatagram(64);
      System.out.println("Before sending"+trRec);
      dgram.writeUTF(trRec);
      sender.send(dgram);
      }
      catch (IOException e)
      {
      System.err.println("Exception caught:" + e);
      }
   }

/**

   This method searches for the name in the record store

*/
   void search_add(String address) // Function for searching..
   .
   {
      String temp = "";
      String phone_number="";
      String person_name="";
      int size = address.length();
      try
      {
      RecordEnumeration re =  recordStore.enumerateRecords(null, null, false);
      ui_form = new Form("Search Result.");
      while (re.hasNextElement())
      {
      String name1 = new String(re.nextRecord());
      try
      {
      person_name = name1.substring(2, name1.indexOf("?"));
      }
      catch (Exception ef)
      {
      person_name = "check";
      }
      String check_name = person_name.substring(0, size);
      if (check_name.equals(address))
      {
      try
      {
      phone_number = name1.substring(name1.indexOf("?") + 1);
      trRec = name1;
      }
      catch (Exception e)
      {
      phone_number = "";
      }
      temp =   temp + "\nName.."+ person_name  "\nPhone.."  + phone_number;
      }
      }
      if (temp.equals(" "))
      {
      temp = "The required address not found...";
      }
      trRec = person_name+"?"+phone_number;
      System.out.println("trRec is"+trRec);
      ui_form.append(temp);
      ui_form.addCommand(destSel); //new
      ui_form.addCommand(quit); // Adding Command Button to the ui_form.
      ui_form.addCommand(mainmenu);
      // Adding Command Button to the ui_form.
      ui_form.setCommandListener(this); // Invoking Action Listener..
      display.setCurrent(ui_form);
      }
      catch (RecordStoreNotOpenException rsnoe)
      {
      rsnoe.printStackTrace();
      }
      catch (InvalidRecordIDException irid)
      {
      irid.printStackTrace();
      }
      catch (RecordStoreException rse)
      {
      rse.printStackTrace();
      }

   }
/**

   This method adds the record to the local Record Store

*/
   void address_add(String address,String phone) // Function for address addtion...
   {

      String data = address + "?" + phone;
      // ? (to distinguish between name and phone number

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      DataOutputStream outputStream = new DataOutputStream(baos);
      try
      {
      outputStream.writeUTF(data);
      byte[] b = baos.toByteArray();
      recordStore.addRecord(b, 0, b.length);
      ui_form = new Form("Success.....");
      ui_form.append("One Record Entered Successfully...");
      ui_form.addCommand(quit); // Adding Command Button to the ui_form.
      ui_form.addCommand(mainmenu);
      // Adding Command Button to the ui_form.
      ui_form.setCommandListener(this); // Invoking Action Listener..
      display.setCurrent(ui_form);
      }
      catch (IOException ioe)
      {
      ioe.printStackTrace();
      }
      catch (RecordStoreException rse)
      {
      rse.printStackTrace();
      }

   }
/**

   This method closes the Record Store

*/
   public void close()
      throws RecordStoreNotOpenException, RecordStoreException
   {
      recordStore.closeRecordStore();
   }

/**

   This class lists the destinations registered with the central server.
   This is started as a separate thread to keep the UI responsive

*/

   class ListForm extends Form implements Runnable,
   HttpConnectionHelper.Callback
   {
      StringBuffer text;
      StringItem t;
      StringItem message;

      ListForm(String text)
      {
      super("List");
      }

      // Updates the display.

      void display(String text)
      {
      if (message == null)
      {
      message = new StringItem(null, text);
      append(message);
      }
      else
      {
      message.setText(text);
      }
      }

      // Done.

      void done(String msg)
      {
      display(msg != null ? msg : "Done.");
      addCommand(okCommand);
      setCommandListener(TransferRecord.this);
      }

      // Callback for making the HTTP connection.

      public void prepareRequest(String originalURL, HttpConnection conn)
         throws IOException
      {
      conn.setRequestMethod(HttpConnection.POST);
      conn.setRequestProperty(
      "User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
      conn.setRequestProperty("Content-Language", "en-US");
      conn.setRequestProperty("Accept", "application/octet-stream");
      }

      // Do the connection on a separate thread to
      // keep the UI responsive...

      public void run()
      {
      HttpConnection conn = null;
      display("Obtaining HttpConnection object...");
      try
      {
      conn = HttpConnectionHelper.connect(listingUrl, this);
      int rc = conn.getResponseCode();
      if (rc == HttpConnection.HTTP_OK)
      {

      try
      {
      DataInputStream din = null;
      InputStream in = null;
      in = conn.openInputStream();
      System.out.println("reached one");
      din = new DataInputStream(in);
      System.out.println("reached two");
      text = new StringBuffer();
      int n = din.readInt();
      while (n-- > 0)
      {
      String h1 = din.readUTF();
      text.append(h1);
      text.append('\n');
      }
      din.close();
      System.out.println("reached three");
      }
      catch (IOException e)
      {
      System.out.println("Problem 123");
      }

      ui_form = new Form("List...");
      System.out.println(text.toString());
      t = new StringItem("List ...", text.toString());
      ui_form.append(t);
      ui_form.append("Return Code" + rc);
      dest = new TextField("Destination ..", "", 50, 0);
      ui_form.append(dest);
      ui_form.addCommand(transfer);
      ui_form.addCommand(mainmenu);
      // Adding Command Button to the ui_form.
      ui_form.addCommand(quit);
      // Adding Command Button to the ui_form.
      ui_form.setCommandListener(TransferRecord.this);
      // Invoking Action Listener..

      display.setCurrent(ui_form);
      }
      else
      {
      done("Unexpected return code: " + rc);
      }
      }
      catch (IOException e)
      {
      done("Exception " + e + " trying to connect.");
      }
      }

      // Starts the upload in the background...

      void start()
      {
      display("Starting...");
      Thread t = new Thread(this);
      try
      {
      t.start();
      }
      catch (Exception e)
      {
      done("Exception " + e + " trying to start thread.");
      }
      }
   }

/**

   This class registers the IP address with the central server. This is started as
   a separate thread to keep the UI responsive

*/
   class StatusForm extends Form implements Runnable, HttpConnectionHelper.Callback
   {
      byte[] data1;
      StringItem message;
      StatusForm(String hostname, String ip_address)
      {
      super("Status");
      try
      {
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      DataOutputStream dout = new DataOutputStream(bout);
      dout.writeUTF("'"+hostname+"'"+","+"'"+ip_address+"'"+",''");
      data1 = bout.toByteArray();
      dout.close();
      }
      catch (IOException e)
      {
      System.out.println("IOException in StatusForm" + e.toString());
      }
      }

      // Updates the display.

      void display(String text)
      {
      if (message == null)
      {
      message = new StringItem(null, text);
      append(message);
      }
      else
      {
      message.setText(text);
      }
      }

      // Done.

      void done(String msg)
      {
      display(msg != null ? msg : "Done.");
      addCommand(okCommand);
      setCommandListener(TransferRecord.this);
      }

      // Callback for making the HTTP connection.

      public void prepareRequest(String originalURL, HttpConnection conn)
         throws IOException
      {
      conn.setRequestMethod(HttpConnection.POST);
      conn.setRequestProperty("User-Agent",
            "Profile/MIDP-1.0 Configuration/CLDC-1.0");
      conn.setRequestProperty("Content-Language", "en-US");
      conn.setRequestProperty("Accept", "application/octet-stream");
      conn.setRequestProperty("Connection", "close");
      conn.setRequestProperty("Content-Length",Integer.toString(data1.length));

      OutputStream os = conn.openOutputStream();
      os.write(data1);
      os.close();
      }

      // Do the connection on a separate thread to
      // keep the UI responsive...

      public void run()
      {
      HttpConnection conn = null;

      display("Obtaining HttpConnection object...");

      try
      {
      conn = HttpConnectionHelper.connect(registerUrl, this);
      display("Connecting to the server...");
      int rc = conn.getResponseCode();
      if (rc == HttpConnection.HTTP_OK)
      {
      ui_form = new Form("Success.....");
      ui_form.append("IP Address got registered...");
      ui_form.append("Return Code" + rc);
      ui_form.addCommand(mainmenu);
      // Adding Command Button to the ui_form.
      ui_form.addCommand(quit);
      // Adding Command Button to the ui_form.
      ui_form.setCommandListener(TransferRecord.this);
      // Invoking Action Listener..
      display.setCurrent(ui_form);
      }
      else
      {
      done("Unexpected return code: " + rc);
      }
      }
      catch (IOException e)
      {
      done("Exception " + e + " trying to connect.");
      }
      }

      // Starts the upload in the background...

      void start()
      {
      display("Starting...");

      Thread t = new Thread(this);
      try
      {
      t.start();
      }
      catch (Exception e)
      {
      done("Exception " + e + " trying to start thread.");
      }
      }
   }

}
/**

   This class helps for any redirections.

*/

class HttpConnectionHelper
{

   public interface Callback
   {
   void prepareRequest(String originalURL, HttpConnection conn)
   throws IOException;
   }

   public static HttpConnection connect(String url) throws IOException
   {
   return connect(url, null);
   }

   public static HttpConnection connect(String url, Callback callback)
      throws IOException
   {

   String originalURL = url;
   HttpConnection conn = null;
   while (url != null)
   {
   conn = (HttpConnection) Connector.open(url);

   if (callback != null)
   {
   callback.prepareRequest(originalURL, conn);
   }

   int rc = conn.getResponseCode();
   switch (rc)
   {
   case HttpConnection.HTTP_MOVED_PERM :
   case HttpConnection.HTTP_MOVED_TEMP :
   case HttpConnection.HTTP_SEE_OTHER :
   case HttpConnection.HTTP_TEMP_REDIRECT :
   url = conn.getHeaderField("Location");
   if (url != null && url.startsWith("/*")) {
   StringBuffer b = new StringBuffer();
   b.append("http://");
   b.append(conn.getHost());
   b.append(':');
   b.append(conn.getPort());
   b.append(url);
   url = b.toString();
   }
   conn.close();
   break;
   default :
   url = null;
   break;
   }
   }
   return conn;
   }
}

/**
 This class does the server side functionality of listing the destinations
 registered with the central server It connects to the Oracle Database and
 queries the database to get the list of the destinations registered
 with the central server.
 */

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.StringBuffer;
import java.io.IOException;
import java.sql.*;
import oracle.jdbc.driver.*;

public class ListingServlet extends HttpServlet
{

   /**
    Does doPost()
    @param HttpServletRequest
    @param HttpServletResponse
    */
   public void doPost(HttpServletRequest request,HttpServletResponse response)
      throws IOException, ServletException
   {

   Connection connection = null;
   Statement statement = null;
   StringBuffer sql = new StringBuffer();

      try
   {
   String driverName = "oracle.jdbc.driver.OracleDriver";
   Class.forName(driverName);
   }
   catch (ClassNotFoundException ej)
   {
   System.out.println("Problem ");
   ej.printStackTrace();
   }

   try
   {
   DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
   }
      catch (Exception e)
   {
   System.out.println("Problem Problem");
   e.printStackTrace();
   }

   String username = "scott",
   String password = "tiger",
   dbURL = "jdbc:oracle:thin:@anouki:1521:ORA9IDB";
   ResultSet rs = null;

   try
   {
   connection = DriverManager.getConnection(dbURL, username, password);
   statement = connection.createStatement();
   sql.append("select * from device_list ");
   System.out.println(sql.toString());
   rs = statement.executeQuery(sql.toString());
   }
   catch (Exception ex)
   {
   System.out.println("Problem problem problem");
   System.err.println("SQLException: " + ex.getMessage());
   ex.printStackTrace();
   }

   Vector v = new Vector();
   try
   {
   while (rs.next())
   {
   String hostname = rs.getString(1);
   String ip_add = rs.getString(2);
   v.addElement(hostname+":"+ip_add);
   }
   ByteArrayOutputStream bout = new ByteArrayOutputStream();
   DataOutputStream dout = new DataOutputStream(bout);
   int size = v.size();
   dout.writeInt(size);
   //out.print(size);
   System.out.println("Vector Size: " + size);
   for (int j = 0; j < size; ++j) {
   dout.writeUTF(v.elementAt(j).toString());
   //out.print((String)v.elementAt(j));
   System.out.println("v.elementAt[" + j + "]" + v.elementAt(j));
   }
   System.out.println("Reached here");
   byte[] data = bout.toByteArray();
   response.setContentType("application/octet-stream");
   response.setContentLength(data.length);
   response.setStatus(response.SC_OK);
   OutputStream out = response.getOutputStream();
   out.write(data);
   System.out.println("Reached here 2");
   out.close();
   rs.close();
   statement.close();
   connection.close();
   }
      catch (SQLException se)
      {
   se.printStackTrace();
   }

   }

   /**
    does doGet()
       @param HttpServletRequest
    @param HttpServletResponse
    */
   public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
      {
    doPost(request, response);
   }
}

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.StringBuffer;
import java.io.IOException;
import java.sql.*;
import oracle.jdbc.driver.*;

/**
  @author Sumi
  This class does the server functionality when the user needs to
  register his address with the server.
 */
public class RegisterServlet extends HttpServlet
{

   /**
    does doPost()
    @param HttpServletRequest
    @param HttpServletResponse

    */
   public void doPost(HttpServletRequest request,HttpServletResponse response)
      throws IOException, ServletException
   {

      // Get the input stream and read the data...
      ServletInputStream in = request.getInputStream(); //new
      DataInputStream din = new DataInputStream(in); //new

      String text = din.readUTF(); //new
      System.out.println(text);
      din.close(); //new

      Connection connection = null;
      Statement statement = null;
      response.setContentType("text/html");

      StringBuffer sql = new StringBuffer();
      try
      {
      String driverName = "oracle.jdbc.driver.OracleDriver";
      Class.forName(driverName);
      }
      catch (ClassNotFoundException ej)
      {
      System.out.println("Problem ");
      ej.printStackTrace();
      }
      try
      {
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
      }
      catch (Exception e)
      {
      System.out.println("Problem Problem");
      e.printStackTrace();
      }

      String username = "scott",password = "tiger",
      dbURL = "jdbc:oracle:thin:@anouki:1521:ORA9IDB";

      try
      {
      connection = DriverManager.getConnection(dbURL, username, password);
      statement = connection.createStatement();
      sql.append("insert into device_list values ( ");

      sql.append(text);
      sql.append(")");
      System.out.println(sql.toString());
      statement.executeUpdate(sql.toString());
      statement.close();
      connection.close();
      }
      catch (Exception ex)
      {
      System.out.println("Problem problem problem");
      System.err.println("SQLException: " + ex.getMessage());
      ex.printStackTrace();
      }

      response.setStatus(response.SC_OK);
      ServletOutputStream out = response.getOutputStream();

      out.close();

   }
}