使用 ObjectOutputStream 返回一个对象
我发现一段java代码声称使用ObjectOutputStream
OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(process);
oos.flush();
oos.close();
响应返回对象是一个HttpServletResponse对象。我想知道这段代码是如何工作的,以及如何测试它?
I found a segment of java code that is claimed to return object using ObjectOutputStream
OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(process);
oos.flush();
oos.close();
response is a HttpServletResponse object. I would like to know how this segment of code works, and how to test it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是一个简单的示例,展示了如何通过 HTTP 读取序列化对象。
Below is a simple example that shows how to read a serialized object via HTTP.
它使用Java 的默认二进制序列化协议(我个人不太愿意使用该协议)将对象序列化为servlet 的输出流(例如,将在HTTP 响应中提供的数据)。有关详细信息,请参阅对象序列化规范。
至于如何测试它 - 这取决于您想要使用的测试级别。您可以使用伪造的 HTTP servlet 库,获取响应,然后尝试使用
ObjectInputStream
再次读取它,或者您可以运行实际的 servlet 容器,发出 HTTP 请求,然后尝试反序列化回复。It serializes the object to the servlet's output stream (e.g. the data that would be served in the HTTP response) using Java's default binary serialization protocol (which I'm personally somewhat reluctant to use). For details, see the object serialization spec.
As for how to test it - it depends on the level of testing you want to use. You could use a fake HTTP servlet library, get the response and then try to read it again with an
ObjectInputStream
, or you could run up the actual servlet container, make an HTTP request and then try to deserialize the response.