Pawan
Pawan

Reputation: 32321

jetty webSocket : java.lang.IllegalStateException: Committed

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

Upvotes: 6

Views: 23369

Answers (6)

Rabhi salim
Rabhi salim

Reputation: 506

In my case I had some repository in my @Service and I declared it as RepositoryFoo repositoryFoo;, in the beginning of my class

I forgot to add @Autowired or even make it private, so it compiled fine and then when running I had this java.lang.IllegalStateException: Committed ... I wasted some time before figuring out the reason !

Upvotes: 0

Ahmad AlMughrabi
Ahmad AlMughrabi

Reputation: 1950

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:

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

then

chain.doFilter(request, response);

If you called resp.getOutputStream();, make sure you are not using chain.doFilter(request, response); on the same request.

Upvotes: 0

Y. Bino
Y. Bino

Reputation: 31

The reason on my side is using jetty with wrong url:
right: http://localhost:8080
wrong: http://localhost:8080/test

Upvotes: 1

Gray
Gray

Reputation: 116828

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.

Upvotes: 15

Witek
Witek

Reputation: 6462

You also get this exception when you call the super method in your own method implementation.

Example:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
    super.doPost(req, resp); // <-- THIS IS THE PROBLEM
    resp.sendRedirect("/someOtherUrl");
}

Upvotes: 3

Joe Li
Joe Li

Reputation: 21

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:

  1. find out where the first redirect is and try to modify the logic to prevent the "two redirect" scenario from happening.
  2. put a "return" after each of your redirect (personally I recommend this solution).

Upvotes: 1

Related Questions