This method throws
java.lang.IllegalStateException: Cannot forward after response has been committed
and I am unable to spot the problem. Any help?
int noOfRows = Integer.parseInt(request.getParameter("noOfRows"));
String chkboxVal = "";
// String FormatId=null;
Vector vRow = new Vector();
Vector vRow1 = new Vector();
String GroupId = "";
String GroupDesc = "";
for (int i = 0; i < noOfRows; i++) {
if ((request.getParameter("chk_select" + i)) == null) {
chkboxVal = "notticked";
} else {
chkboxVal = request.getParameter("chk_select" + i);
if (chkboxVal.equals("ticked")) {
fwdurl = "true";
Statement st1 = con.createStatement();
GroupId = request.getParameter("GroupId" + i);
GroupDesc = request.getParameter("GroupDesc" + i);
ResultSet rs1 = st1
.executeQuery("select FileId,Description from cs2k_Files "
+ " where FileId like 'M%' and co_code = "
+ ccode);
ResultSetMetaData rsm = rs1.getMetaData();
int cCount = rsm.getColumnCount();
while (rs1.next()) {
Vector vCol1 = new Vector();
for (int j = 1; j <= cCount; j++) {
vCol1.addElement(rs1.getObject(j));
}
vRow.addElement(vCol1);
}
rs1 = st1
.executeQuery("select FileId,NotAllowed from cs2kGroupSub "
+ " where FileId like 'M%' and GroupId = '"
+ GroupId + "'" + " and co_code = " + ccode);
rsm = rs1.getMetaData();
cCount = rsm.getColumnCount();
while (rs1.next()) {
Vector vCol2 = new Vector();
for (int j = 1; j <= cCount; j++) {
vCol2.addElement(rs1.getObject(j));
}
vRow1.addElement(vCol2);
}
// throw new Exception("test");
break;
}
}
}
if (fwdurl.equals("true")) {
// throw new Exception("test");
// response.sendRedirect("cs2k_GroupCopiedUpdt.jsp") ;
request.setAttribute("GroupId", GroupId);
request.setAttribute("GroupDesc", GroupDesc);
request.setAttribute("vRow", vRow);
request.setAttribute("vRow1", vRow1);
getServletConfig().getServletContext().getRequestDispatcher(
"/GroupCopiedUpdt.jsp").forward(request, response);
}
I removed
Then it worked fine for me
Bump...
I just had the same error. I noticed that I was invoking
super.doPost(request, response);
when overriding thedoPost()
method as well as explicitly invoking the superclass constructorAs soon as I commented out the
super.doPost(request, response);
from withindoPost()
statement it worked perfectly...Needless to say, I need to re-read on
super()
best practices :pA common misunderstanding among starters is that they think that the call of a
forward()
,sendRedirect()
, orsendError()
would magically exit and "jump" out of the method block, hereby ignoring the remnant of the code. For example:This is thus actually not true. They do certainly not behave differently than any other Java methods (expect of
System#exit()
of course). When thesomeCondition
in above example istrue
and you're thus callingforward()
aftersendRedirect()
orsendError()
on the same request/response, then the chance is big that you will get the exception:If the
if
statement calls aforward()
and you're afterwards callingsendRedirect()
orsendError()
, then below exception will be thrown:To fix this, you need either to add a
return;
statement afterwards... or to introduce an else block.
To naildown the root cause in your code, just search for any line which calls a
forward()
,sendRedirect()
orsendError()
without exiting the method block or skipping the remnant of the code. This can be inside the same servlet before the particular code line, but also in any servlet or filter which was been called before the particular servlet.In case of
sendError()
, if your sole purpose is to set the response status, usesetStatus()
instead.Another probable cause is that the servlet writes to the response while a
forward()
will be called, or has been called in the very same method.The response buffer size defaults in most server to 2KB, so if you write more than 2KB to it, then it will be committed and
forward()
will fail the same way:Solution is obvious, just don't write to the response in the servlet. That's the responsibility of the JSP. You just set a request attribute like so
request.setAttribute("data", "some string")
and then print it in JSP like so${data}
. See also our Servlets wiki page to learn how to use Servlets the right way.See also:
Unrelated to your concrete problem, your JDBC code is leaking resources. Fix that as well. For hints, see also How often should Connection, Statement and ResultSet be closed in JDBC?
After return forward method you can simply do this:
It will break the current scope.
even adding a return statement brings up this exception, for which only solution is this code:
Typically you see this error after you have already done a redirect and then try to output some more data to the output stream. In the cases where I have seen this in the past, it is often one of the filters that is trying to redirect the page, and then still forwards through to the servlet. I cannot see anything immediately wrong with the servlet, so you might want to try having a look at any filters that you have in place as well.
Edit: Some more help in diagnosing the problem…
The first step to diagnosing this problem is to ascertain exactly where the exception is being thrown. We are assuming that it is being thrown by the line
But you might find that it is being thrown later in the code, where you are trying to output to the output stream after you have tried to do the forward. If it is coming from the above line, then it means that somewhere before this line you have either:
Good luck!