title: Closed error when using HttpServlet with JAVA
body: <p>I wrote HttpServlet doGet method this is the servlet:</p>
  <pre><code>public class PrintDocsServlet extends HttpServlet {
  
      private static final int BAD_REQUEST = 400;
  
  
      public void init() throws ServletException {
  
      }
  
      public void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          try {
              String id = request.getParameter(&quot;id&quot;);
              String format = request.getParameter(&quot;format&quot;);
              user_format = format == null ? &quot;pdf&quot; : format;
              
              if(id != null &amp;&amp; !id.isEmpty())
              {
                  printFile(response, id, user_format);
              }
              else
              {
                  response.sendError(BAD_REQUEST);
              }
  
          } catch(Exception e) {
              session = null;
              e.printStackTrace();
              response.sendError(BAD_REQUEST);
          } finally {
              if(session != null)
              {
                  DfcSessions.release(session);
              }
          }
      }
  
      private void printFile(HttpServletResponse response, String id, String format) throws DfException, IOException {
  
  
              ByteArrayInputStream content = null;
  
              String fileName = &quot;view.&quot; + format;
              
              response.setContentType(fileType);
              response.addHeader(&quot;Content-Length&quot;, Long.toString(obj.getContentSize()));
              
  
              FileInputStream content = new FileInputStream(&quot;C:/demo.pdf&quot;);
  
              ServletOutputStream out = response.getOutputStream();
  
              IOUtils.copy(content, out);
              response.setHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; + fileName);
  
              if(content != null)
              {
                  content.close();
              }
              if(out != null)
              {
                  out.flush();
                  out.close();
              }
          }
      }
  }
  </code></pre>
  <p>this code working perfect with jetty 8.1.16 on port 8888 but when I using jetty 9.4.38 i got this error:</p>
  <pre><code>org.eclipse.jetty.io.EofException: Closed
         at org.eclipse.jetty.server.HttpOutput.checkWritable(HttpOutput.java:768)
         at org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:792)
         at com.github.ziplet.filter.compression.ThresholdOutputStream.write(ThresholdOutputStream.java:93)
         at com.github.ziplet.filter.compression.CompressingServletOutputStream.write(CompressingServletOutputStream.java:67)
         at org.springframework.security.web.util.OnCommittedResponseWrapper$SaveContextServletOutputStream.write(OnCommittedResponseWrapper.java:645)
         at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2147)
         at org.apache.commons.io.IOUtils.copy(IOUtils.java:2102)
         at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2123)
         at org.apache.commons.io.IOUtils.copy(IOUtils.java:2078)
         at com.servlets.PrintDocsServlet.printFile(PrintDocsServlet.java:128)
         at com.servlets.PrintDocsServlet.doGet(PrintDocsServlet.java:57)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:645)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
         at org.eclipse.jetty.servlet.ServletHolder$NotAsync.service(ServletHolder.java:1452)
         at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:791)
         at org.eclipse.jetty.servlet.ServletHandler$ChainEnd.doFilter(ServletHandler.java:1626)
         at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94)
         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
         at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
         at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
         at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
         at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:118)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:158)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:155)
         at com.emc.xcp.rest.security.filter.BasicAuthFilter.doFilterInternal(BasicAuthFilter.java:62)
         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter.doFilterInternal(DefaultLogoutPageGeneratingFilter.java:52)
         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:216)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at com.emc.xcp.rest.security.filter.CSRFTokenFilter.doFilter(CSRFTokenFilter.java:22)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
         at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
         at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
         at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
         at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
         at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
         at com.emc.xcp.rest.security.filter.RepositoryInfoFilter.doFilter(RepositoryInfoFilter.java:33)
         at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
         at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
         at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
         at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
         at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
         at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
         at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
         at com.emc.xcp.ui.LocaleFilter.doFilter(LocaleFilter.java:26)
         at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
         at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
         at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
         at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
         at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
         at com.emc.xcp.ui.web.filter.CustomCharEncodingFilter.doFilterInternal(CustomCharEncodingFilter.java:43)
         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
         at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
         at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
         at com.emc.xcp.ui.RestCorsFilter.doFilterInternal(RestCorsFilter.java:49)
         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
         at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
         at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
         at com.github.ziplet.filter.compression.CompressingFilter.doFilter(CompressingFilter.java:304)
         at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
         at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
         at com.emc.xcp.ui.CompressingFilterProxy.doFilter(CompressingFilterProxy.java:38)
         at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:201)
         at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
         at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:548)
         at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
         at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:602)
         at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
         at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235)
         at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624)
         at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)
         at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1435)
         at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188)
         at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:501)
         at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594)
         at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186)
         at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1350)
         at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
         at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:234)
         at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146)
         at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
         at org.eclipse.jetty.server.Server.handle(Server.java:516)
         at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:388)
         at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:633)
         at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:380)
         at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:279)
         at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)
         at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105)
         at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104)
         at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:336)
         at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:313)
         at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:171)
         at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:129)
         at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:383)
         at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:882)
         at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1036)
         at java.base/java.lang.Thread.run(Thread.java:834)
  2022-03-29 08:17:49.614:WARN:oejs.HttpChannel:qtp1971764991-171: /MevakerApp/PrintDocsServlet
  java.lang.IllegalStateException: COMPLETED
         at org.eclipse.jetty.server.HttpChannelState.sendError(HttpChannelState.java:896)
         at org.eclipse.jetty.server.Response.sendError(Response.java:471)
         at org.eclipse.jetty.server.Response.sendError(Response.java:442)
         at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:132)
         at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:132)
         at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:132)
         at org.springframework.security.web.util.OnCommittedResponseWrapper.sendError(OnCommittedResponseWrapper.java:115)
         at com.servlets.PrintDocsServlet.doGet(PrintDocsServlet.java:67)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:645)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
         at org.eclipse.jetty.servlet.ServletHolder$NotAsync.service(ServletHolder.java:1452)
         at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:791)
         at org.eclipse.jetty.servlet.ServletHandler$ChainEnd.doFilter(ServletHandler.java:1626)
         at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94)
         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
         at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
         at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
         at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
         at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:118)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:158)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:155)
         at com.emc.xcp.rest.security.filter.BasicAuthFilter.doFilterInternal(BasicAuthFilter.java:62)
         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter.doFilterInternal(DefaultLogoutPageGeneratingFilter.java:52)
         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:216)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at com.emc.xcp.rest.security.filter.CSRFTokenFilter.doFilter(CSRFTokenFilter.java:22)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
         at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
         at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
         at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
         at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
         at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
         at com.emc.xcp.rest.security.filter.RepositoryInfoFilter.doFilter(RepositoryInfoFilter.java:33)
         at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
         at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
         at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:193)
         at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)
         at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
  </code></pre>
  <p>at this line:</p>
  <pre><code>IOUtils.copy(content, out);
  </code></pre>
  <p>what can i do? Considering I can not change the jetty version, what can i do with this?
  i tried to using IOUtils.copy alternative but there is same error, the error saying that the servletresponseoutput is closed.</p>
  <p>tnx</p>