Posted By:
Peter_VanCaeseele
Posted On:
Sunday, April 28, 2002 07:44 PM
Hi, I'm trying to write a servlet that, once activated, will continuously send objects back to a thread in the client. I'm hoping someone can point me in the right direction. This code works fine if you take out the while(true){...} loops. After doing a bit more research, I discovered the OutputObjectStream should be .reset() in order to reset the object map. I tried doing this before the .flush() with no success. The servlet continues to spew out objects, but the client side doesn't even see the first one, hanging on the in = DataInputStream(connection.getInputStream()) line. Servlet code: public class testStream extends HttpServlet { public void doGet(HttpServletRequest request,
More>>
Hi,
I'm trying to write a servlet that, once activated, will continuously send objects back to a thread in the client. I'm hoping someone can point me in the right direction. This code works fine if you take out the while(true){...} loops.
After doing a bit more research, I discovered the OutputObjectStream should be .reset() in order to reset the object map. I tried doing this before the .flush() with no success. The servlet continues to spew out objects, but the client side doesn't even see the first one, hanging on the in = DataInputStream(connection.getInputStream()) line.
Servlet code:
public class testStream extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
int[] somedata = new int [1];
somedata [0] = 1000;
String contentType =
"application/x-java-serialized-object";
response.setContentType(contentType);
ObjectOutputStream out =
new ObjectOutputStream(response.getOutputStream());
while (true) {
try {
out.writeObject(somedata );
System.out.println("Sent " + somedata [0]);
somedata [0]++;
} catch(Exception ie) {}
out.flush();
Thread.yield();
}
out.close(); // unreachable if while loop in place
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Applet side code:
*
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class testStream implements Runnable {
private boolean isDone = false;
private URL dataURL;
public testStream(String urlSuffix, URL currentPage) {
try {
// Only the URL suffix need be supplied, since
// the rest of the URL is derived from the current page.
String protocol = currentPage.getProtocol();
String host = currentPage.getHost();
int port = currentPage.getPort();
dataURL = new URL(protocol, host, port, urlSuffix);
Thread imageRetriever = new Thread(this);
imageRetriever.start();
} catch(MalformedURLException mfe) {
System.err.println("Bad URL");
isDone = true;
}
}
public void run() {
try {
retrieveImage();
} catch(IOException ioe) {
}
isDone = true; // will never get hit
}
public boolean isDone() { // meaningless now with infinite loop
return(isDone);
}
private void retrieveImage() throws IOException {
URLConnection connection = dataURL.openConnection();
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDefaultUseCaches (false);
connection.setRequestProperty("Content-Type", "application/x-java-serialized-object");
ObjectInputStream in = null;
try {
in = new ObjectInputStream(connection.getInputStream());
} catch (Exception pe) { System.err.println(pe); }
while (true) {
// loop forever requesting data
try {
// The return type of readObject is Object, so
// I need a typecast to the actual type.
int[] someData = (int[]) in.readObject();
System.err.println(someData[0]);
} catch(ClassNotFoundException cnfe) {
System.err.println("received NULL");
}
Thread.yield();
}
}
}
<<Less