Search code examples
javaservletstomcat6

Maven Test on servlet receiving an IllegalStatException: Cannot reset buffer - response already committed


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?


Solution

  • 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.