Servlet 上的 Maven 测试接收 IllegalStatException:无法重置缓冲区 - 响应已提交

发布于 2024-09-24 07:25:28 字数 1048 浏览 1 评论 0原文

在我的 Maven 构建的测试阶段,我在一个 @test 方法中保存了以下代码:

        request.clearAttributes();
        response.reset();
        String story_uuid = qit.getQI().getStory_uuid();
        assertNotNull(story);
        request.setParameter("story_uuid", story_uuid);
        request.setParameter("activity", "get");
        queue.doPost(request, response);
        assertEquals(response.getErrorMessage(), HttpServletResponse.SC_OK, response.getStatus());


        request.clearAttributes();
        response.reset();    //**THIS RESET HERE**//
        request.setParameter("story", story_uuid);
        request.setParameter("activity", "revert");
        queue.doPost(request, response);
        assertEquals(response.getErrorMessage(), HttpServletResponse.SC_OK, response.getStatus());

response.reset() 行和 //**THIS RESET HERE**//< /code> 导致以下错误:

java.lang.IllegalStateException: Cannot reset buffer - response is already committed

我不应该使用一种方法来多次调用我的 servlet 吗?

During the test phase of my maven build I ave the following code in one @test method:

        request.clearAttributes();
        response.reset();
        String story_uuid = qit.getQI().getStory_uuid();
        assertNotNull(story);
        request.setParameter("story_uuid", story_uuid);
        request.setParameter("activity", "get");
        queue.doPost(request, response);
        assertEquals(response.getErrorMessage(), HttpServletResponse.SC_OK, response.getStatus());


        request.clearAttributes();
        response.reset();    //**THIS RESET HERE**//
        request.setParameter("story", story_uuid);
        request.setParameter("activity", "revert");
        queue.doPost(request, response);
        assertEquals(response.getErrorMessage(), HttpServletResponse.SC_OK, response.getStatus());

The line response.reset() with the //**THIS RESET HERE**// causes the following error:

java.lang.IllegalStateException: Cannot reset buffer - response is already committed

Should I not be using one method to have multiple calls to my servlet?

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

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

发布评论

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

评论(1

百善笑为先 2024-10-01 07:25:28

reset() 仅在响应尚未提交时才起作用。它基本上清除了输出缓冲区。您不能对单个请求发送多个响应。这完全违反了HTTP规范。对于客户端发送的每个请求,服务器只能发回一个完整的响应。当响应被提交后,你就无法返回了。如果您想发回新的响应,那么您需要让客户端触发一个全新的请求,以便您可以发回新的响应。

The reset() only works when the response is not committed yet. It basically clears the output buffer. You cannot send multiple responses on a single request. This totally violates the HTTP specification. For each request a client sends, the server can send only one fullworthy response back. When the response is been committed, you're at a point of no return. If you want to send a new response back, then you need to let the client fire a brand new request so that you can send a new response back.

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