jetty webSocket : java.lang.IllegalStateException:

2020-07-18 07:05发布

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

6条回答
对你真心纯属浪费
2楼-- · 2020-07-18 07:08

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).
查看更多
欢心
3楼-- · 2020-07-18 07:09

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");
}
查看更多
来,给爷笑一个
4楼-- · 2020-07-18 07:16

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 !

查看更多
\"骚年 ilove
5楼-- · 2020-07-18 07:17

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.

查看更多
可以哭但决不认输i
6楼-- · 2020-07-18 07:22

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

查看更多
成全新的幸福
7楼-- · 2020-07-18 07:34

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.

查看更多
登录 后发表回答