Using PrintWriter and OutputStream

前端 未结 4 1600
不思量自难忘°
不思量自难忘° 2021-01-13 08:33

I am creating a project with struts and I have a problem using Jasper IReports. I want to export some info into a pdf file and I keep getting the java.lang.IllegalStateExcep

相关标签:
4条回答
  • 2021-01-13 08:38

    How exactly is the code invoked? Judging from the stacktrace it look like that you're running the Java class with the handle method using scriptlets inside a JSP file (the inicio2.jsp to be precise). After that the Java class has written the report to the OutputStream, the JSP file would continue with outputting the remnant of the file itself (including whitespace!), which would implicitly invoke the getWriter() to write it to response. Exactly that would cause an IllegalStateException as you're facing now when the getOutputStream() is already been called before in the Java class.

    It's good that Java code is been placed in a Java class, but that doesn't mean that you may still use JSP to invoke it. The JSP should not contain any single line of Java code. JSP itself is as being a view technology part of the output. To fix this all, just have a Struts action class (or a HttpServlet) which you can invoke by a HTML <form> or <a>.

    0 讨论(0)
  • 2021-01-13 08:40

    Would be useful to see the stack trace.

    You might try running a sanity check first though: Modify that code to simply write a static string (hello world) to the ServletOutputStream and set content type to text/html. As that should work fine:

    public void handle(HttpServletResponse res, Connection connection, String path)throws Exception{
    ServletOutputStream out = null;
    try {
        byte[] bytes = "hello world".getBytes();
        res.setContentType("text/html");
        res.setContentLength(bytes.length);
        out = res.getOutputStream();
        out.write(bytes, 0, bytes.length);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        out.flush();
        out.close();
    }
    

    HTH

    0 讨论(0)
  • 2021-01-13 08:58

    Own answer:

    I have put in the path a jrxml that actually connects to the database and gets some real data out and suddendly it worked, the PDF opened in mozilla, but the Exception keeps happening. I do not know how I can throw and exception (I have debugged and the Exception happens in the same place) and be able to see the PDF.

    So I can only keep searching for what actually happens here. I will answer with anything I find (probably on monday).

    Thanks simonlord and Jim Rush for your help! :)

    Edit: By the way, here is the stacktrace (it's in Spanish, if needed I will translate anything you don't understand): Edit again: Seems I didn't copy the '1' in the 15th-Jan-2010... could create confusion about a trace from the 5th-Jan insetad. Corrected.

    15-ene-2010 13:47:03 org.apache.catalina.core.ApplicationDispatcher invoke
    GRAVE: El Servlet.service() para servlet jsp lanzó una excepción
    java.lang.IllegalStateException: getOutputStream() ya ha sido llamado para esta respuesta
        at org.apache.catalina.connector.Response.getWriter(Response.java:607)
        at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)
        at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
        at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
        at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
        at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:171)
        at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:116)
        at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:76)
        at org.apache.jsp.pages.inicio2_jsp._jspService(inicio2_jsp.java:300)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
        at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:584)
        at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:497)
        at org.apache.struts.chain.commands.servlet.PerformForward.handleAsInclude(PerformForward.java:141)
        at org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:92)
        at org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
        at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
        at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
        at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
        at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
        at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
        at java.lang.Thread.run(Unknown Source)
    15-ene-2010 13:47:03 org.apache.struts.chain.commands.AbstractExceptionHandler execute
    ADVERTENCIA: Unhandled exception
    org.apache.jasper.JasperException: getOutputStream() ya ha sido llamado para esta respuesta
        at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:367)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
        at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:584)
        at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:497)
        at org.apache.struts.chain.commands.servlet.PerformForward.handleAsInclude(PerformForward.java:141)
        at org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:92)
        at org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
        at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
        at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
        at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
        at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
        at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
        at java.lang.Thread.run(Unknown Source)
    15-ene-2010 13:47:03 org.apache.struts.chain.commands.ExceptionCatcher postprocess
    ADVERTENCIA: Exception from exceptionCommand 'servlet-exception'
    org.apache.jasper.JasperException: getOutputStream() ya ha sido llamado para esta respuesta
        at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:367)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
        at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:584)
        at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:497)
        at org.apache.struts.chain.commands.servlet.PerformForward.handleAsInclude(PerformForward.java:141)
        at org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:92)
        at org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
        at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
        at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
        at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
        at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
        at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
        at java.lang.Thread.run(Unknown Source)
    15-ene-2010 13:47:03 org.apache.catalina.core.StandardWrapperValve invoke
    GRAVE: Servlet.service() para servlet action lanzó excepción
    java.lang.IllegalStateException: getOutputStream() ya ha sido llamado para esta respuesta
        at org.apache.catalina.connector.Response.getWriter(Response.java:607)
        at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)
        at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
        at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
        at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
        at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:171)
        at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:116)
        at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:76)
        at org.apache.jsp.pages.inicio2_jsp._jspService(inicio2_jsp.java:300)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
        at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:584)
        at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:497)
        at org.apache.struts.chain.commands.servlet.PerformForward.handleAsInclude(PerformForward.java:141)
        at org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:92)
        at org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
        at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
        at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
        at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
        at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
        at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
        at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
        at java.lang.Thread.run(Unknown Source)
    
    0 讨论(0)
  • 2021-01-13 08:59

    Some ideas:

    • PrintWriter isn't going to work if you are working with a binary stream (PDF are binary)
    • The illegal state might occur if there is client side timeout or disconnect. A packet trace of the process will tell you a lot (even if you can't read them well). Look at WireShark or what is available for your platform. Its worth your time to learn at least a little bit about what happens at the wire level.
    • Make sure the data you are getting back from the report generate is actually a pdf. Write it to a file and attempt to open.
    • Some situations need you to set the http length header before writing to the stream otherwise they give up when the data starts to show. Might be necessary here.
    0 讨论(0)
提交回复
热议问题