每个请求发送两个servlet响应 [英] Sending two servlet responses per request

查看:94
本文介绍了每个请求发送两个servlet响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写道,因为我无法解决以下问题。
我有一个处理一些信息的servlet。
作为回应,我放了文本和二进制内容。
如何从同一个请求开始获得两个响应,然后是两个html页面?
是可能的吗?
第一个响应应该继续执行他现在所做的操作,而第二个响应似乎会创建一个弹出窗口来保存图像。
有更简单的方法可以达到相同的效果吗?
非常感谢提前

解决方案

上一个问题,每个HTTP请求只能发送一个HTTP响应。这不是servlet限制,这是 HTTP 限制。服务器不应该不经意地向客户端发送数据。这将使互联网非常烦人和无法使用。好像你被连续不断的垃圾邮件扔掉了。



为了能够返回两个回复,客户端必须发出两个请求本身。如果你想在单击中自动执行此操作,那么你可以(ab)使用一些JavaScript镜头。例如

 < a href =page.jsponclick =window.open('downloadservlet / file.ext') >点击< / A> 

这将触发两个请求,一个发送到 page.jsp 在当前窗口中使用普通HTML,使用JavaScript在新窗口中使用另一个到 downloadservlet / file.ext 。然而,如果回复是内容 - 处置:附件,则此窗口将消失,如上一个问题中所述。



您只需要考虑到当客户端使用JavaScript 已禁用时这不起作用。


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

解决方案

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.

这篇关于每个请求发送两个servlet响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆