struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dave Newton <davelnew...@gmail.com>
Subject Re: getOutputStream() has already been called for this response
Date Sat, 20 Aug 2011 13:34:50 GMT
A request can only return one response: the file, or a forward (assuming
it's not some sort of streaming forward).

That method is pretty hard to follow, and should be refactored, IMO.

Dave

On Saturday, August 20, 2011, raaja.g <raaja09@gmail.com> wrote:
> Hi,
>
> I am getting the *java.lang.IllegalStateException: getOutputStream()* has
> already been called for this response when I use *return
> map.findForward("success")* at the end of action method. But this
exception
> wont appear if I return null. As I am not using writer object. But this
will
> not display an error message in the jsp through action messages.
>
> I am using struts1.3.
>
> How to avoid the above said exception so that I can generate a pdf when
> success and print a message when failed. As this is occurring because both
> writer and outputstream are using at the same time.
>
> Is it possible to close the outputstream after generating the pdf and
after
> that to send the message struts jsp using default writer.
>
> My code is:
>
> public ActionForward execute(ActionMapping map, ActionForm form,
>                        HttpServletRequest req, HttpServletResponse res)
throws Exception {
>                String savefilename =
req.getParameter("savefilename")+".pdf";
>                FileForm file2 = (FileForm) form;
>                ArrayList list = file2.getList();
>                FormFile f2 = (FormFile) list.get(0);
>                Boolean b1 = validate(f2.getFileName());
>                if(b1==false){
>                        ActionMessages messages = new ActionMessages();
>                messages.add("sts", new
> ActionMessage("common.generatepdf.onlyimages"));
>                saveErrors(req, messages);
>                return map.findForward("success");
>                }
>                ServletOutputStream outStream=null;
>                Document document=new Document(PageSize.A4,0,0,10,10);
>                res.setContentType("application/pdf");
>                res.setHeader("Content-Disposition","attachment;
filename="+savefilename);
>                outStream = res.getOutputStream();
>                PdfWriter writer =
PdfWriter.getInstance(document,res.getOutputStream());
>                writer.setStrictImageSequence(true);
>                document.addHeader("String","Content");
>            document.open();
>            try{
>                for (int count = 0; count < list.size(); count++) {
>                        FormFile f1 = (FormFile) list.get(count);
>                        Boolean b = validate(f1.getFileName());
>                        if(b==true){
>                        Image convertJpg =
Image.getInstance(f1.getFileData());
>                        if(convertJpg.getDpiX()>75 &
convertJpg.getDpiY()>75){
>                                convertJpg.scaleToFit(200, 200);
>                        }
>                        document.add(new Paragraph());
>                                document.add(convertJpg);
>                        }
>                }
>                document.close();
>                writer.close();
>                ActionMessages messages = new ActionMessages();
>                messages.add("sts", new
ActionMessage("common.generatepdf.success"));
>                saveMessages(req, messages);
>            }catch(Exception e){
>                        ActionMessages messages = new ActionMessages();
>                        messages.add("sts", new
ActionMessage("common.generatepdf.fail"));
>                        saveErrors(req, messages);
>            }
>            finally {
>                      if(outStream != null)
>                                try {
>                                        document.close();
>                                        writer.close();
>                                        outStream.close();
>                                        outStream.flush();
>                                catch (IOException e) {
>                                        e.printStackTrace();
>
>                                }
>                }
>
> */stack trace is:/*
>
> java.lang.IllegalStateException: getOutputStream() has already been called
> for this response
>        at
org.apache.catalina.connector.Response.getWriter(Response.java:611)
>        at
>
org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
>        at
>
javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:122)
>        at
org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:187)
>        at
>
org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:180)
>        at
>
org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:237)
>        at
>
org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:173)
>        at
>
org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:124)
>        at
>
org.apache.jsp.pages.common.GeneratePdf_jsp._jspService(org.apache.jsp.pages.common.GeneratePdf_jsp:256)
>        at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109)
>        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
>        at
>
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:389)
>        at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:486)
>        at
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:380)
>        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>        at
>
org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
>        at
>
org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
>        at
>
org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
>        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:727)
>        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>        at
>
org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>        at
>
com.krishct.efiler.common.actions.SessionFilter.doFilter(SessionFilter.java:50)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>        at
>
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
>        at
>
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
>        at
>
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
>        at
>
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
>        at
>
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
>        at
>
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
>        at
> org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
>        at
>
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
>        at
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
>        at java.lang.Thread.run(Unknown Source)
> Aug 20, 2011 6:33:19 PM org.apache.catalina.core.StandardWrapperValve
invoke
> SEVERE: Servlet.service() for servlet action threw exception
> java.lang.IllegalStateException: getOutputStream() has already been called
> for this response
>        at
org.apache.catalina.connector.Response.getWriter(Response.java:611)
>        at
>
org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
>        at
>
javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:122)
>        at
org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:187)
>        at
>
org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:180)
>        at
>
org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:237)
>        at
>
org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:173)
>        at
>
org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:124)
>        at
>
org.apache.jsp.pages.common.GeneratePdf_jsp._jspService(org.apache.jsp.pages.common.GeneratePdf_jsp:256)
>        at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109)
>        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
>        at
>
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:389)
>        at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:486)
>        at
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:380)
>        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>        at
>
org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
>        at
>
org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
>        at
>
org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
>        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:727)
>        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>        at
>
org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>        at
>
com.krishct.efiler.common.actions.SessionFilter.doFilter(SessionFilter.java:50)
>        at
>
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
>        at
> org.apache.catalina.core.ApplicationFilterChai
>
>
>
> --
> View this message in context:
http://struts.1045723.n5.nabble.com/getOutputStream-has-already-been-called-for-this-response-tp4718396p4718396.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message