import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class Conn5 extends MIDlet implements CommandListener
{
  private Display display;
  private Form form;
  private Form form2;
  private TextField userID;
  private TextField password;
  private StringItem teksti;
  private Command cmConnect;
  private Command cmExit;

  public Conn5()
  {
     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;
     OutputStream output = null;
     InputStream input = null;
     String url = "http://localhost:8080/servlet/example3";
     try
     {
        conn = (HttpConnection) Connector.open(url);
        conn.setRequestMethod(HttpConnection.POST);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1");
        output = conn.openOutputStream();

        byte data[] = ("userID=" + userID.getString()).getBytes();
        output.write(data);
        data = ("&password=" + password.getString()).getBytes();
        output.write(data);
        output.close();

        input = conn.openInputStream();
        kasittely(conn, input);
     }
     finally
     {
      if (input != null)
         input.close();
      if (conn != null)
         conn.close();
     }
  }

  private void kasittely(HttpConnection conn, InputStream input) throws IOException
  {
     teksti = 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();
        }
           teksti.setText(str);
     } else
        teksti.setText(new String(conn.getResponseMessage()));
        // shows main window
        form2 = new Form("Main window");
        form2.addCommand(cmExit);
        form2.append(teksti);
        form2.setCommandListener(this);
        display.setCurrent(form2);
  }
}


