如何序列化HttpServletRequest / HttpServletResponse? [英] How Serialize HttpServletRequest/HttpServletResponse?

查看:780
本文介绍了如何序列化HttpServletRequest / HttpServletResponse?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个客户端(A和B)和Servlet。我想,当一个客户端向SERVLET发送请求时,SERVLET将请求重定向到客户端B,客户端B将响应发送回客户端A.客户端不是服务器!它们是普通的套接字客户端,因此不可能是经典的servlet重定向!!

I have two clients (A and B) and Servlet. I want, when A client send a request to the SERVLET, SERVLET redirected the request to the client B and client B send response back to the client A. CLIENT ARE NOT SERVLETS!!! They are ordinary socket clients, consequently IS NOT possible classic servlet redirect!!

你有什么建议可以排除故障吗???

Do you have any suggestions for troubleshooting???

非常感谢!!!!

推荐答案

首先,你不能使用Java序列化HttpServletRequest或HttpServletResponse序列化。符合这些API的对象通常包括对servlet实现堆栈中stuff的引用,这些内容本质上是不可序列化的。

Firstly, you can't serialize a HttpServletRequest or HttpServletResponse using Java serialization. Objects that conform to these APIs typically include references to "stuff" in the servlet implementation stack that is inherently non-serializable.

其次,你不能重定向一个请求给另一个客户。从HTTP协议的角度来看它没有意义。

Second, you can't "redirect" a request to another client. It doesn't make sense from the perspective of the HTTP protocol.


  • 当客户端向a发送请求时会发生重定向服务器和服务器响应有一个3xx状态代码,表示在其他地方尝试该请求。它是重定向到另一个服务器,而不是另一个客户端。

  • A redirect happens when a client sends a request to a server and the server response has a 3xx status code that says "try that request somewhere else". It is a redirection to a different server, not a different client.

甚至忽略重定向的含义细节。您无法向HTTP客户端角色中的某些内容发送HTTP请求。它不会期待它(听它),也不会知道如何处理它。 (实际上它违反了HTTP协议。)

Even ignoring the details of what redirection means. You can't send an HTTP request to something that is in the HTTP client role. It won't be expecting it (listening for it), and wouldn't know what to do with it. (And indeed it would be a violation of the HTTP protocol.)

第三,普通套接字客户端可以不要谈论HTTP服务(使用Servlet或其他任何东西实现)。客户端必须至少实现HTTP协议的一个子集,以使HTTP服务能够理解它自己。有可能实现手工,但IMO这是一个坏主意......当有高质量的实现时,您可以免费使用。

Thirdly, an "ordinary socket client" can't talk to an HTTP service (implemented using Servlets, or anything else). The client has to implement at least a subset of HTTP protocol in order to make itself understood by the HTTP service. It is possible to implement that "by hand", but IMO that's a bad idea ... when there are high quality implementations you can use for free.

简而言之,您似乎想要做的事情是不可能/荒谬的。 (如果我理解你的问题是正确的......这是值得商榷的。)

In short, what you seem to be trying to do is impossible / nonsensical. (If I understand your Question correctly ... which is debatable.)

如果你解释了你实际上在尝试什么在这里,我们或许可以建议合理的替代方法。

If you explained what you were actually trying to do here, we might be able to suggest sensible alternative approaches.


我正在尝试跨服务器连接两个Java客户端应用程序。客户端将能够直接与其他客户进行通信。

I'm trying to connect two java client applications across server. The client will be able to communicate directly with other client.

字面上你不能使用HTTP来做到这一点。但是你可以构建一个HTTP服务器/ servlet,它将消息从一个客户端传输到另一个客户端;例如

Literally you can't do that using HTTP. But you could build an HTTP server/servlet that transfers messages from one client to another; e.g.


  1. 客户端A向服务器发送包含A消息的PUT请求。

  2. 服务器存储消息并回复客户端A.

  3. 客户端B发送GET请求,询问是否有任何消息?服务器。

  4. 服务器查找消息并使用来自A的消息进行响应。

  1. Client A sends a PUT request containing a message for A to server.
  2. Server stores message and replies to client A.
  3. Client B sends a GET request asking "any messages?" to server.
  4. Server looks up messages and responds with the message from A.

但是请注意,无法使用普通套接字客户端执行此操作。客户端必须是HTTP客户端。

But note that you can't do that with plain socket clients. The clients have to be HTTP clients.

如果您准备放弃服务器是HTTP服务器/ servlet的要求,您可以打开简单套接字客户端与服务器的双工连接,并让服务器在客户端之间传递消息。这需要为消息传递实现自定义协议。

If you were prepared to ditch the requirement that the server was an HTTP server / servlet, you could have "simple socket" clients open duplex connections to the server, and have the server pass "messages" between the clients. This entails implementing a custom "protocol" for messaging.

第三种方法是使用现有的RPC或对象代理技术;例如RMI,CORBA,ICE,等等

A third alternative is to use an existing RPC or object broker technology; e.g. RMI, CORBA, ICE, etcetera

这篇关于如何序列化HttpServletRequest / HttpServletResponse?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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