Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jetty webSocket : java.lang.IllegalStateException: Committed

Tags:

servlets

jetty

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??

like image 222
Pawan Avatar asked Nov 30 '22 22:11

Pawan


1 Answers

java.lang.IllegalStateException: Committed

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:

java.lang.IllegalStateException: Response headers have already been sent. Are you trying to return a result after sending content?

Typically this exception happens when you go and call:

 resp.getOutputStream();  // or getWriter()

and then later try to do a redirect or something:

 resp.sendRedirect("/someOtherUrl");
 // or
 return new ModelAndView("redirect:/someOtherUrl");

Once you get the OutputStream or Writer so you can write body bytes to the client, Jetty has to commit the response and send the HTTP 200 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 a ModelAndView(...) or just change the handler to return void.

like image 141
Gray Avatar answered Dec 04 '22 10:12

Gray