IllegalStateException:HttpServlet.service中的null

时间:2012-06-26 19:56:17

标签: java java-ee servlets illegalstateexception

我只是在请求失败时尝试向客户端发回错误。这是我的代码的样子:

response.sendError(HttpServletResponse.SC_BAD_REQUEST, 
        "Email and username are required fields.");

此代码抛出以下错误:

java.lang.IllegalStateException: null
    at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407) ~[catalina-6.0.26.jar:6.0.26]
    at com.****.****.****.*********Servlet.service(********Servlet.java:68) ~[********Servlet.class:na]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) ~[javaee-api-5.1.2.jar:5.1.2]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) ~[catalina-6.0.26.jar:6.0.26]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[catalina-6.0.26.jar:6.0.26]
    at org.red5.logging.LoggerContextFilter.doFilter(LoggerContextFilter.java:78) ~[red5.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) ~[catalina-6.0.26.jar:6.0.26]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[catalina-6.0.26.jar:6.0.26]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) ~[catalina-6.0.26.jar:6.0.26]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [catalina-6.0.26.jar:6.0.26]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:465) [catalina-6.0.26.jar:6.0.26]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [catalina-6.0.26.jar:6.0.26]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [catalina-6.0.26.jar:6.0.26]
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:555) [catalina-6.0.26.jar:6.0.26]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [catalina-6.0.26.jar:6.0.26]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) [catalina-6.0.26.jar:6.0.26]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852) [tomcat-coyote-6.0.26.jar:6.0.26]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) [tomcat-coyote-6.0.26.jar:6.0.26]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) [tomcat-coyote-6.0.26.jar:6.0.26]

为什么要扔在这里?我在这个调用之前没有做任何事情,它与响应对象有任何关系,我甚至没有触摸它,直到在if子句中发送此错误。为什么我会收到此错误?

2 个答案:

答案 0 :(得分:2)

HttpServletResponse(由ResponseFacade实现)抛出IllegalStateException的唯一原因是之前已经提交了响应。它已经被某人感动过了。

答案 1 :(得分:0)

如果您的ResponseFacade没有覆盖response.sendError()或者它导致转发到错误页面,则会发生这种情况,因为响应在转发到错误页面之前已经提交。

要解决此问题,

  • 确保您没有冲洗回复
  • 如果响应由于达到最大缓冲区大小而自动提交,请尝试增加缓冲区大小。