Friday, September 01, 2006


App to Servlet Communication

App to Servlet Communication



DataInputStream dis;
try {
String urlString = "http://www.ibm.com";
URL url = new URL(urlString);
dis = new DataInputStream(url.openConnection().getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(dis));
String total = "";
String line = "";
While ((line = in.readLine()) != null) {
total += line;
}
in.close();
} catch (Exception e) {
System.out.println(e);
}





// Listing 2
//
// Applet client-side code to send a student object
// to a servlet in a serialized fashion.
//
// A POST method is sent to the servlet.
//

URL studentDBservlet = new URL( webServerStr );
URLConnection servletConnection = studentDBservlet.openConnection();

// inform the connection that we will send output and accept input
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);

// Don't use a cached version of URL connection.
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);

// Specify the content type that we will send binary data
servletConnection.setRequestProperty ("Content-Type", "application/octet-stream");

// send the student object to the servlet using serialization
outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());

// serialize the object
outputToServlet.writeObject(theStudent);

outputToServlet.flush();
outputToServlet.close();

No comments: