Tomcat 8.5 response.getWriter()偶尔返回null

时间:2019-03-27 12:21:27

标签: java tomcat printwriter tomcat8.5

我正在生产中使用带有Servlet的Tomcat 8.5.23(如果有相关功能,则具有Spring自动装配功能) response.getWriter()偶尔在生产中返回null(无任何特殊问题/重启/参数),相关方法在某些Servlet的末尾被调用:

protected void writeResponse(HttpServletResponse response, String text) {
        try {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            pw.write(text);

我看到了similar bug,但其状态为已解决无效,并且未提供更多信息

这是tomcat的错误吗?我错误地处理了PrintWriter吗?我可以解决它吗?

如果需要更多信息,我会添加

例外

 java.lang.NullPointerException
        at org.apache.catalina.connector.CoyoteWriter.write(CoyoteWriter.java:180)
        at com.MyServlet.writeResponse(MyServlet.java:251)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at com.Filter.EncodingFilter.doFilter(EncodingFilter.java:33)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
        at org.apache.coyote.ajp.AjpProcessor.service(AjpProcessor.java:486)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)

编辑

getWriter方法有警告:

  

可以调用此方法或getOutputStream()来编写正文,但不能同时编写两者,除非已调用reset()。

我在代码可选行中看到将getOutputStream()称为:

response.getOutputStream().write(bytes);

这可能是问题吗?

1 个答案:

答案 0 :(得分:1)

您的过滤器代码看起来不错,问题出在其他地方。已经有一条评论提到getWriter()无法返回null。看一下似乎发生这种情况的代码。代码可能会尝试将writer分配给变量,但是会捕获并忽略该异常。在忽略异常的那一刻之后,有人尝试使用该变量,但是它包含null,因为该赋值没有发生。

可能会发生这些“已调用”错误的常见情况是错误处理。 Servlet开始发送响应,因此称为getWriter()。然后,它会遇到错误,并且错误处理将尝试编写错误响应,因此再次调用getWriter()。您无法重现该问题,因为该错误情况是随机发生的。