6

I have an action called GetFile which directly opens a file download box to open PDF.

Below is the struts.xml configuration and the action class for the same. I am using result type as stream to achieve this. I can see that the pdf download box opens all the times and i am also able to download the file.

But I find that there were some requests last night that had fired the action and it has produced the below error.

    org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
        at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:570)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:452)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
        at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
        at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
        at org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:159)
        at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
        at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:374)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:278)
        at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
        at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
        at deshaw.irweb.web.interceptor.AuthInterceptor.intercept(AuthInterceptor.java:60)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
        at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
        at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:510)
        at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
        at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:294)
        at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:183)
        at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:169)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this response
        at org.apache.catalina.connector.Response.getWriter(Response.java:636)
        at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:205)
        at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
        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:182)
        at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:123)
        at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
        at org.apache.jsp.jsp.ServerError_jsp._jspService(ServerError_jsp.java:157)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
        ... 38 more     

Can someone tell me what could have caused the problem ? I am not able to reproduce the problem currently. I read somewhere that it could be due to closing the download dialog, but it was not the case even after i tried from multiple browsers. Also checked getOutputStream() has already been called for this response , but could not relate directly to my problem as there is no redirection to jsp as the struts2 action directly leads to result stream.

struts.xml

<action name="GetFile" class="Class" method="DownloadFile">
            <result name="success" type="stream">
            <param name="contentType">contentType</param>
            <param name="contentDisposition">fileName</param>
            <param name="bufferSize">1024</param>
            <param name="inputName">inputStream</param> 
            </result>
</action>

Action Class

public String DownloadFile ()
{
        // create the object variable pdfdoc which is a custom object.
        // You can assume that the pdfdoc is properly constructed 
        // and getFileContent does what it is required to do, so
        // that the final pdf is generated.
        ...
        ...
        inputStream = new ByteArrayInputStream(pdfdoc.getFileContent());
        contentType = pdfdoc.getContentType();
        fileName = pdfdoc.getFileName();
        contentDisposition = doc.getContentDisposition();
        bufferSize = 1024;
        return "success";

}
3
  • Can you please post complete stack trace and in short if this behavior is not reproducible its hard to guess user behavior. Jun 1, 2012 at 14:27
  • Does the action take any parameters? Jun 1, 2012 at 22:04
  • Yes the action takes two parameters(id's) which are passed in the query string. This is used inside DownloadFile to aid in finding the appropriate PDF document.
    – Gopal SA
    Jun 2, 2012 at 3:35

1 Answer 1

1
+50

I had a similar problem when using Internet Explorer and fixed it by adding this in the response header:

response.setHeader("Expires","0");
response.setHeader("Pragma","cache");
response.setHeader("Cache-Control","private");

Maybe the requests that you see in your log where generated by Internet Explorer.

Have a look here and here!

Update: To print the browser version in your log you can do something like this:

System.out.println(request.getHeader("User-Agent"));
7
  • Thanks. Although it might be a problem with IE, Can you tell me the exact user behavior in IE that might help me to reproduce the problem?
    – Gopal SA
    Jun 7, 2012 at 4:15
  • Try with several versions of IE and when you test remove your url from trusted sites and watch the log to see what happens when you are prompted with the message that your download was blocked.
    – tibtof
    Jun 7, 2012 at 7:01
  • 1
    @TGV if my answer isn't of any help please tell me and I'll remove it. I don't want to get undeserved bounties.
    – tibtof
    Jun 11, 2012 at 8:03
  • 1
    Of course your answer were very helpful.I tried that from a couple of IE browsers after removing url from trusted sites, and it never generated any error. Also i might be wrong with the version of IE that am using. So this still remains a tough problem to reproduce. Looking at the stack trace, the last invoke done is that of paramsinterceptor, so will this be an issue inside strust2 itself. Basically i am thinking if the browser has still not come into the context at all. Am just guessing.
    – Gopal SA
    Jun 11, 2012 at 9:18
  • Try with IE 6 and 7. Maybe your best approach would be to write the browser version in your log and see if the problem appears only for a certain type of browser.
    – tibtof
    Jun 11, 2012 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.