Listing 1

OutputStream outStream = con.getOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(outStream);

dataOutStream.writeUTF(toSend);

InputStream inStream = con.getInputStream();
DataInputStream dataInStream = new DataInputStream(inStream);

//get the response from the server
fromServer = dataInStream.readUTF();
System.out.println("Reply from Servlet: " + fromServer);

Listing 2

InputStream in = req.getInputStream();
OutputStream out = res.getOutputStream();
DataInputStream dataIn = new DataInputStream(in);
DataOutputStream dataOut = new DataOutputStream(out);

//read in the value passed in
String passedIn = dataIn.readUTF();

//use this value to send back
String toReturn = passedIn;

res.setContentType("application/octet-stream");

//write it out to the response
dataOut.writeUTF(toReturn);
dataOut.flush();

Listing 3

private void outputEcho() {
  ...
  String fromUser = "foo";

  HttpConnection con = openConnection();

  DataOutputStream dataOutStream = openConnectionOutputStream(con);
  dataOutStream.writeUTF(fromUser);

  DataInputStream dataInStream = openConnectionInputStream(con);

  //get the response from the server
  String fromServer;
  fromServer = dataInStream.readUTF();

  //close all the connection pieces and free resources
  closeConnection(con, dataOutStream, dataInStream);
  //display the results to the user
  Form outputForm = new Form("Echo Output");
  outputForm.append(fromServer);

  outputForm.addCommand(EXIT);
  outputForm.setCommandListener(this);

  display.setCurrent(outputForm);
  ...
}

Listing 4

int responseCode = connection.getResponseCode();

if (responseCode == HttpConnection.HTTP_OK ||
    responseCode == HttpConnection.HTTP_CREATED) {
  if (responseCode == HttpConnection.HTTP_CREATED) {
    // If the server forwarded to another URL, use that URL
    // next time.
    serviceURL = connection.getHeaderField("Location");
  }

  DataInputStream inputStream = connection.openDataInputStream();

  return inputStream;

} else {
  System.out.println("openConnectionInputStream failed!!!");
}

Listing 5

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="content-type"
          content="text/html; charset=ISO-8859-1">
    <title>Altisimo Computing</title>
  </head>
  <body>
    <h1>Altisimo Computing</h1>
    <p>Altisimo is your source for J2ME applications, 
      consulting, and training.<br>
      Call us at 888-868-4306.<br></p>
 
    <p>Downloads:<br></p>
    <ul>
      <li><a href="hello.jad">Hello World</a>
        (license: <a href="LICENSE-GPL-MULTIPART">GPL</a>)</li>
      <li><a href="echo.jad">Echo</a>
        (license: <a href="LICENSE-GPL-MULTIPART">GPL</a>,
        <a href="LICENSE-Sun">Sun</a>)</li>
    </ul>
  </body>
</html>