2

I'm an experienced Java programmer but a newbie web developer. I'm trying to put together a simple web service using the HttpServer class that ships with JDK 1.6. From the examples I've viewed, some typical code from an HttpHandler's handle method would look something like this:

Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
exchange.sendResponseHeaders(200, 0);

OutputStream responseBody = exchange.getResponseBody();
responseBody.write(createMyResponseAsBytes());
responseBody.close();

My question: What happens if I send a response header to indicate success (i.e. response code 200) and perhaps begin to stream back data and then encounter an exception, which would necessitate sending an "internal server error" response code along with some error content? In other words, what action should I take given that I've already sent a partial "success" response back to the client at the point where I encounter the exception?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Adamski
  • 54,009
  • 15
  • 113
  • 152

1 Answers1

3

200 is not sent until you either flush the stream or close it. But once it is sent, there is nothing you can do about it.
Usually it may happen only when you have a really large amount of data and you use chunking.

Tarlog
  • 10,024
  • 2
  • 43
  • 67
  • Thanks. TBH I will be sending a large amount of data using a chunking approach so I guess this is something I'll have to deal with, perhaps using a header and footer around my dataset to allow the client to detect an error condition. – Adamski Oct 06 '11 at 16:45