HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Building a Connected Midlet"
Vol. 8, Issue 12, p. 49

	



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>

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.