Note that there are some explanatory texts on larger screens.

plurals
  1. POHttpServer vs. Servlet - which approach to take to show an updating BufferedImage
    text
    copied!<p>I have a Java application, and would like to serve out a BufferedImage (that is updated a few times a second) to a web client.</p> <p>I've started by looking at HttpServer (part of JDK 6). I've implemented an HttpHandler, and on the handle(HttpExchange exchange) call I grab the latest BufferedImage and send it out using the exchange reference.</p> <p>I can use my web browser to view the image sent via exchange, and as I refresh the browser I can observe the image updating (since my application is updating the BufferedImage).</p> <p>For the next step, I would like to continuously update the image in the browser (without needing to manually refresh the web page). It seems that HttpServer with an associated HttpHandler is not really set up to do this (just a one time request/response). I've started to read about Servlets and am looking into using Jetty.</p> <p>I'd like to know if I'm on the right track with looking into Servlets, or is there a way to "drive" BufferedImage updates to a web client that has connected to my HttpServer?</p> <p>Thanks in advance.</p> <p>Code snippet blow for my handle method:</p> <pre><code>public void handle(HttpExchange exch) throws IOException { BufferedImage image = fImageProvider.getLatestImage(); ByteArrayOutputStream output = new ByteArrayOutputStream(SIZE); ImageIO.write(image, IMAGE_CODEC, output); byte[] byteArray = output.toByteArray(); exch.sendResponseHeaders(HttpURLConnection.HTTP_OK, byteArray.length); exch.getResponseBody().write(byteArray); exch.close } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload