Servlet 何时读取或写入实际数据

发布于 2024-11-30 17:16:58 字数 322 浏览 0 评论 0原文

我正在测试客户端服务器应用程序,以了解 Servlet 何时实际接收/写入数据到客户端。

测试设置有一个客户端 Java 程序,它读取文件并将文件 POST 到 Servlet。

仅当客户端完成将整个文件 (4 MB) 数据写入套接字后,服务器才开始读取该数据。类似地,当 Servlet 使用 PrintWriter 发送响应时,只有当 Servlet 中的 doPost(...) 方法返回时,响应才会到达客户端。经过测试,Tomcat 内存根本没有增加...

Servlet 连接/会话何时建立?为什么当客户端向套接字写入前几个字节数据时,Servlet 无法立即读取数据?

I was testing a Client Server Application to know when does a Servlet actually receives / writes a data to a Client.

The test setup has a client Java program which reads a file and POSTs the file to a Servlet.

Only after the client finishes writing the whole file (4 MB) data to socket the server starts reading that data. Similarly, when a Servlet sends a response using PrintWriter, the response reaches the client only when the doPost(...) method in the Servlet returns. Thorought the testing Tomcat memory didn't increase atall...

When does the Servlet Connection / Session gets established? Why does the Servlet coudn't read the data immediately when the client writes first few bytes of data to socket?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角迷惘 2024-12-07 17:16:59

这就是它的工作原理。在从客户端接收到整个请求之前,容器不会调用 servlet。以任何其他方式这样做都将意味着许多不可预见的后果。对于初学者来说,您必须在所有 servlet 中进行手动完成检查,这只会添加不必要的样板代码。

如果您希望能够从开始接收 HTTP 请求的那一刻起对其做出反应,您将必须编写自己的套接字程序,绑定和侦听端口并自己处理输入。

输出是另一回事。输出流缓冲所有 servlet 输出,但您可以随时在 ServletResponse 上调用lushBuffer(),以强制它发送所有当前缓冲的内容。不过,对此有一个警告,一旦您开始向客户端发送数据,响应就会被视为“已提交”。这是因为 HTTP 协议要求某些信息按一定的顺序出现。例如,必须首先发送标头,这意味着一旦您“提交”响应,服务器应用程序就无法再添加标头和 cookie。 Servlet 执行完毕后,容器始终会调用lushBuffer()(或ServletOutputStream 上的flush())。

That's just the way it works. The container will not invoke the servlet until the entire request is received from the client. Doing it any other way would imply any number of unforeseeable consequences. For starters, you would have to put manual completion checks in all your servlets, which would just add unnecessary boiler-plate code.

If you want to be able to react to HTTP requests from the moment they start being received, you will have to write your own socket program, binding and listening on a port and handling the input yourself.

Output is another matter. The output stream buffers all servlet output, but you can invoke flushBuffer() on the ServletResponse at any time to force it to send all currently buffered content. A warning about this though, once you start sending data to the client, the response is considered "committed". This is because of the HTTP protocol that requires certain pieces of information to come in a certain order. Headers, for instance, must be sent first, meaning that once you "commit" the response, headers and cookies can't be added by the server application anymore. The container will always call flushBuffer() (or flush() on the ServletOutputStream) after the servlet has finished executing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文