Sending two servlet responses per request

2019-02-11 07:19发布

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

1条回答
成全新的幸福
2楼-- · 2019-02-11 08:03

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.

查看更多
登录 后发表回答