Sending two servlet responses per request

前端 未结 1 1717
青春惊慌失措
青春惊慌失措 2021-01-03 14:49

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 re

相关标签:
1条回答
  • 2021-01-03 15:35

    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.

    0 讨论(0)
提交回复
热议问题