Servlet 上的 Maven 测试接收 IllegalStatException:无法重置缓冲区 - 响应已提交
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.