I am using Jetty Websockets in my Web Application .
When i am trying to redirect to a logoff jsp , i am getting this error
oejs.ServletHandler:/test
java.lang.IllegalStateException: Committed
at org.eclipse.jetty.server.Response.resetBuffer(Response.java:1069)
at javax.servlet.ServletResponseWrapper.resetBuffer(ServletResponseWrapper.java:232)
at org.eclipse.jetty.http.gzip.GzipResponseWrapper.resetBuffer(GzipResponseWrapper.java:273)
at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:199)
at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:98)
This is the way i am redirecting
RequestDispatcher rd = request.getRequestDispatcher("logoff.jsp");
rd.forward(request, response);
This error is not reproduceble , but could you please tell me when it may occur??
This occurs because your response has already processed a redirect request, you are trying to modify a committed response.
There are two general ways to solve this:
You also get this exception when you call the super method in your own method implementation.
Example:
In my case I had some repository in my
@Service
and I declared it asRepositoryFoo repositoryFoo;
, in the beginning of my classI forgot to add
@Autowired
or even make itprivate
, so it compiled fine and then when running I had thisjava.lang.IllegalStateException: Committed
... I wasted some time before figuring out the reason !Consider you are running
javax.servlet.Filter
on Jetty server, and you face the same exception. The issue here can be described exactly as Gray's description (Thanks Gray). Typically this exception happens when you go and call:then
If you called
resp.getOutputStream();
, make sure you are not usingchain.doFilter(request, response);
on the same request.The reason on my side is using jetty with wrong url:
right:
http://localhost:8080
wrong:
http://localhost:8080/test
I thought I'd provide a more general explanation of what the exception means. First off, Jetty should be ashamed by the exception message. It provides little to no help to the developer unless they already know what it actually means. The exception should be something like:
Typically this exception happens when you go and call:
and then later try to do a redirect or something:
Once you get the
OutputStream
orWriter
so you can write body bytes to the client, Jetty has to commit the response and send the HTTP200
and associated headers, so it can start returning the body bytes. Once that happens, you then can't do a redirect nor make any other changes to the status code or headers.The proper thing to do, once you return body bytes, is to return
null
from the handler instead of aModelAndView(...)
or just change the handler to returnvoid
.