0

Does anyone have an example of how to send both binary (image) and text-based data in a single servlet response? An example is returning an image and image-map all generated on the server. I was also not able to find a mixed-mode mime type to use to be able to perform this operation.

Thoughts?

stavarotti
  • 582
  • 5
  • 19

4 Answers4

2

You can use Data URI to embed binary objects into generated HTML. E.g.

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">

See also: https://serverfault.com/questions/241218/why-do-http-servers-not-send-a-single-file-back-when-possible#241224

Community
  • 1
  • 1
Mircea Vutcovici
  • 1,894
  • 19
  • 27
  • You can have a greater speed-up if your page and all embedded objects are not changing often and if you are using compression and cache control. The browser will check only one item if it has been updated instead of many of them. – Mircea Vutcovici Jan 04 '12 at 22:58
  • Yes, you just explained why I said "horrible and awesome" and not just horrible :) – digitaljoel Jan 04 '12 at 23:01
2

Browser support for multipart responses is still pretty dicey (read here). But if you are planning to parse the response on the client side yourself there are some pretty good examples out there. The mime-type you are looking for is multipart/mixed.

Community
  • 1
  • 1
Perception
  • 79,279
  • 19
  • 185
  • 195
0

This is not how HTTP and HTML work. A first request is made to load HTML code. This HTML code contains <img src="..."/> tags, which point to the URL of the image. A second request is then made by the browser to load the image. You can't download the HTML and the image in a single request.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Many WAP browsers supports multi-part responses, but I don't think "regular" browsers do.

Also see Browser support of multipart responses

Community
  • 1
  • 1
Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53