2

I write, because I can not solve the following problem. I have a servlet that processes some information. In response I put both text and binary content. How do I get two response, then two html page, starting from the same request? is a thing possible? The first response should continue to do what he does now, while the second would appear to make a popup window to save an image. There are easier ways to achieve the same result? Many thanks in advance

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sangi
  • 511
  • 3
  • 13
  • 25

1 Answers1

4

As answered in your previous question, You can send only one HTTP response per HTTP request. This is not a servlet restriction, this is a HTTP restriction. The server is not supposed to send data to the client unaskingly. That would have made the Internet extremely annoying and unusable. As if you're thrown dead with a continuous stream of spam.

To be able to return two responses, the client has to fire two requests itself. If you want to do this automagically on a "single click", then you can (ab)use some shot of JavaScript for this. E.g.

<a href="page.jsp" onclick="window.open('downloadservlet/file.ext')">click</a>

This will fire two requests, one to page.jsp using normal HTML in current window and another one to downloadservlet/file.ext in new window using JavaScript. This window will however disappear if the response is of Content-Disposition: attachment as answered in your previous question.

You only need to take into account that this won't work when the client has JavaScript disabled.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This answer is not entirely correct! Most HTTP interactions are purely request-response, however the response code [102 Processing](https://tools.ietf.org/html/rfc2518#section-10.1) calls for multiple responses on the same stream. There are use-cases for this response, however most framewords don't seem to take these into account. – penguineer Dec 19 '18 at 22:56
  • @penguineer: Ha! Moreover, things have changed since HTTP/2. – BalusC Dec 20 '18 at 13:59