import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class Conn4 extends MIDlet implements CommandListener
{
  private Display display;
  private Form form;
  private Form form2;
  private TextField userID;
  private TextField password;
  private StringItem text;
  private Command cmConnect;
  private Command cmExit;

  public Conn4()
  {
     display = Display.getDisplay(this);
     cmConnect = new Command("Connect", Command.SCREEN, 1);
     cmExit = new Command("Exit", Command.EXIT, 1);
     userID = new TextField("User id:", "", 10, TextField.ANY);
     password = new TextField("Password:", "", 10, TextField.PASSWORD);
     form = new Form("Login");
     form.addCommand(cmExit);
     form.addCommand(cmConnect);
     form.append(userID);
     form.append(password);
     form.setCommandListener(this);
  }
  public void startApp()
  {
     display.setCurrent(form);
  }
  public void pauseApp()
  { }
  public void destroyApp(boolean unconditional)
  { }
  public void commandAction(Command c, Displayable s)
  {
     if (c == cmConnect)
     {
       Thread t = new Thread(){
          public void run(){
            try{
               connect();
            } catch (Exception e){
               System.err.println("Msg: " + e.toString());
            }
         }
       };
       t.start();
     }
      else if (c == cmExit)
     {
        destroyApp(false);
        notifyDestroyed();
     }
  }
  private void connect() throws IOException
  {
     HttpConnection conn = null;
     InputStream input = null;
     String url = "http://localhost:8080/servlet/example2?userID=" +
                   userID.getString() + "&password=" + password.getString();
     try
     {
        conn = (HttpConnection) Connector.open(url);
        input = conn.openInputStream();
        process(conn, input);
     }
     finally
     {
      if (input != null)
         input.close();
      if (conn != null)
         conn.close();
     }
  }

  private void process(HttpConnection conn, InputStream input) throws IOException
  {
     text = new StringItem("Login:", "");
     if (conn.getResponseCode() == HttpConnection.HTTP_OK)
     {
        int length = (int) conn.getLength();
        String str;
        if (length != -1)
        {
           byte data[] = new byte[length];
           input.read(data);
           str = new String(data);
        }
        else // no length returned
        {
           ByteArrayOutputStream bStream = new ByteArrayOutputStream();
           int ch;
           while ((ch = input.read()) != -1)
                    bStream.write(ch);
           str = new String(bStream.toByteArray());
           bStream.close();
        }
           text.setText(str);
     } else
        text.setText(new String(conn.getResponseMessage()));
        // show main window
        form2 = new Form("Main window");
        form2.addCommand(cmExit);
        form2.append(text);
        form2.setCommandListener(this);
        display.setCurrent(form2);
  }
}

