0

Possible Duplicate:
java.lang.IllegalStateException: Cannot forward after response has been committed

What is the usual cause of this kind of error:

com.mycompany.myapp.servlet.TxnDetailsServlet doRequest
ERROR: View failed
java.lang.IllegalStateException: Cannot forward after response has been committed
 at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:312)
 at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
 at com.mycompany.myapp.servlet.TxnDetailsServlet.doRequest(TxnDetailsServlet.java:82)
 at com.mycompany.myapp.servlet.TxnDetailsServlet.doGet(TxnDetailsServlet.java:131)

The servlet process the request (i.e set attributes) then call:

    private void doRequest(HttpServletRequest request) throws IOException, ServletException     {
        // Code omitted
        getServletContext().getRequestDispatcher("/Some.jsp").forward(this.request, this.response);
        // Code omitted
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doGet(request, response);
        doRequest(request);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doPost(request, response);
        doRequest(request);
    }

The servlet does not do anything in reponse.

Community
  • 1
  • 1
quarks
  • 33,478
  • 73
  • 290
  • 513

2 Answers2

0

Don't write to the response output stream if you forward to another servlet/jsp.

opi
  • 244
  • 2
  • 9
0

to add to what others said, response need not be transferred to client(browser) in a single shot. Instead it can be transferred to client in multiple shots as, whenever you call a response.flushBuffer . Once the response starts transferring data to client, you cant do anything that changes response state(setStatus, forward etc)

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42