java 如何克隆一个分离的 Servlet 容器提供的 HttpServletRequest 和 HttpServletResponse?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5273579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clone a detached HttpServletRequest and HttpServletResponse provided by the Servlet Container?
提问by L.J.W
I want to implement the following logic: when I receive HttpServletRequeset and HttpServletResponse in main servlet's doService method (in the main web Container thread),I start A,B,C three threads (thread managed by my own program) to process other servlet in parallel mode,and then join each response from these servlet in main thread,and if one of my own thread (assume A thread) work slow,the main thread will finish,so main response will return to user.and the A thread must continue work properly,I will request the response of the A thread using AJAX in browser side later.
我想实现如下逻辑:当我在主servlet的doService方法中(在主web容器线程中)接收到HttpServletRequeset和HttpServletResponse时,我启动A,B,C三个线程(由我自己的程序管理的线程)来处理其他servlet并行模式,然后在主线程中加入来自这些servlet的每个响应,如果我自己的一个线程(假设A线程)工作缓慢,主线程将完成,因此主响应将返回给用户。A线程必须继续正常工作,稍后我将在浏览器端使用AJAX请求A线程的响应。
So,I want to clone the HttpServlettRequest and HttpServletResponse provided by the Servlet Container,and the cloned request and response must be detached(When container's HttpServletTrequest and HttpServletResponse finished,the cloned request and reponse still work properly).
所以,我想克隆Servlet容器提供的HttpServlettRequest和HttpServletResponse,克隆的请求和响应必须分离(当容器的HttpServletTrequest和HttpServletResponse完成后,克隆的请求和响应仍然正常工作)。
The behave of the cloned request and response must be same as the Container's from my code's view.It can be followed and included.
从我的代码的角度来看,克隆的请求和响应的行为必须与容器的行为相同。它可以被遵循和包含。
Any idea?
任何的想法?
Thanks very much!
非常感谢!
L.J.W
丽江
回答by Juan C
Cloning HTTP request and response is possible via HttpServletResponseWrapper class http://docs.oracle.com/javaee/1.3/api/javax/servlet/http/HttpServletResponseWrapper.html. You can find an example of usage on Sun documentation https://web.archive.org/web/20120626033905/http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets8.html.
可以通过 HttpServletResponseWrapper 类http://docs.oracle.com/javaee/1.3/api/javax/servlet/http/HttpServletResponseWrapper.html克隆 HTTP 请求和响应。您可以在 Sun 文档https://web.archive.org/web/20120626033905/http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets8.html上找到使用示例。
Notice this was a workaround from (at that time) Sun to address this problem as it was never planned that you could modify request and response information before committed.
请注意,这是(当时)Sun 提供的解决此问题的变通方法,因为从未计划在提交之前可以修改请求和响应信息。
You can use the wrapper to make a copy of Http information and the pass it to a different thread.
您可以使用包装器制作 Http 信息的副本并将其传递给不同的线程。
回答by DaveH
Sounds like you need to create classes to act as a delegate to the HttpRequest and HttpResponse objects and then pass a reference to on to a Runnable object to process.
听起来您需要创建类来充当 HttpRequest 和 HttpResponse 对象的委托,然后将 on 的引用传递给要处理的 Runnable 对象。
There are certain operations that can only be done once to an HttpRequest object ( reading from the inputstream springs to mind ), the delegate class would have to cater for this.
有些操作只能对 HttpRequest 对象执行一次(从输入流中读取),委托类必须满足这一点。
Not sure what you're going to do if the A, B and C threads make conflicting changes to the HttpResponse object though.
如果 A、B 和 C 线程对 HttpResponse 对象进行了相互冲突的更改,则不确定您将要做什么。
I think I'd prefer to not pass the HttpResponse object through to the processing threads and leave the logic for populating the response in the controlling servlet class
我想我不想将 HttpResponse 对象传递给处理线程,而是将用于填充响应的逻辑留在控制 servlet 类中
回答by Andrew White
I think you are asking to much of the HttpServletRequest. Once a request has been completed you shouldn't count on the request object being of any use. I don't recommend threading inside a J2EE container in most cases anyway but that's a different issue.
我认为您对 HttpServletRequest 的要求很高。请求完成后,您不应指望请求对象有任何用处。在大多数情况下,我不建议在 J2EE 容器内进行线程处理,但这是一个不同的问题。
If you musthandle the request in parallel I recommend you extract the data you need from the request object and send that to your threads and make the worker threads mostly Servlet ignorant with the exception of the HttpSession where they could store their computed values for the Ajax retrieval.
如果您必须并行处理请求,我建议您从请求对象中提取所需的数据并将其发送到您的线程,并使工作线程大部分是 Servlet 无知的,除了 HttpSession 之外,它们可以在其中存储 Ajax 的计算值恢复。
回答by Mike Baranczak
The request and response classes aren't designed to be cloned or accessed from multiple threads. If you try to do so, you're bound to run into problems. I suggest that you re-think your requirements.
请求和响应类不是为了从多个线程克隆或访问而设计的。如果您尝试这样做,您一定会遇到问题。我建议你重新考虑你的要求。