java.lang.IllegalStateException: Cannot forward after response has been committed - Part II

Ok, perhaps some of you remember my post about IllegalStateException and what caused it on my first post. You can find it here. Right now, I encountered another one and this time, the servlet caused the exception and not the JSP. Got the solution right here. First, take a look at my code.



1
2
3
4
5
6
7
8
9
10
    if ( searchesList.size() != 0 && webSearchList.size() != 0 && searchEngineList.size() != 0 )
    {
      request.getRequestDispatcher(FORWARD_VIEW_MONTHLY).forward(request, response);
    }
    else
    {
      request.getRequestDispatcher(FORWARD_NORECORDS).forward(request, response);
    }
    
    return;


For some reason, it just throws that IllegalStateException. The main point here through is that the servlet doesn't exit the doPost() or doGet() method right away. The code can be further improved by doing this.



1
2
3
4
5
6
7
8
9
10
    if ( searchesList.size() != 0 && webSearchList.size() != 0 && searchEngineList.size() != 0 )
    {
      request.getRequestDispatcher(FORWARD_VIEW_MONTHLY).forward(request, response);
      return;
    }
    else
    {
      request.getRequestDispatcher(FORWARD_NORECORDS).forward(request, response);
      return;
    }


I haven't tested this yet on our development server. But I'm sure that it will work and get rid of that IllegalStateException thing. Big Smile

UPDATE: 01/23/2007

How silly of me to post this solution without testing it first! It was very stupid of me to say that the return stuff solved the problem. I just wrestled for another whole day searching for the solution because the problem still persisted even after this post. Luckily, I was able to find a solution.

Just replace the lines that use the RequestDispatcher's forward method to include like this

request.getRequestDispatcher(FORWARD_NORECORDS).forward(request, response);

to

request.getRequestDispatcher(FORWARD_NORECORDS).include(request, response);

Whew! Sorted!


Published 01-18-2007 5:11 AM by lamia
Filed under: , ,

Comments

Sunday, September 02, 2007 8:10 PM by platformer

# re: java.lang.IllegalStateException: Cannot forward after response has been committed - Part II

my question is same! but i dont use "request.getRequestDispatcher(FORWARD_NORECORDS).include(request, response);"

.Not find reason

Wednesday, January 23, 2008 1:43 AM by nampreet

# re: java.lang.IllegalStateException: Cannot forward after response has been committed - Part II

it really solved my problem too .But can you please explain what might have causing the error in the first place and why  using include solved the problem

Wednesday, June 25, 2008 10:51 PM by Sandeep

# re: java.lang.IllegalStateException: Cannot forward after response has been committed - Part II

Can any body Please help me ! I am getting same proble by control is not Dispatching to JSP list.

Here is MY CODE:

try {

boolean temp;        

       temp=ClientSaleDAO.getUpdatedStatusInfo(csalvo,Integer.parseInt(req.getParameter("clientaccountno")));            

    if(temp) {    

    tabAction.actualTabProcess(req,res,selectedSubTabId);      

RequestDispatcher rd1=req.getRequestDispatcher("pages/client/sales.jsp?UPDATEDSTATUS=YES");

rd1.forward(req, res);

return;

}

    }catch (Exception e) {

e.printStackTrace();

}