在jsp中避免java.lang.IllegalStateException

时间:2013-05-15 10:04:41

标签: jsp

我尝试在jsp中下载生成的pdf.but我的日志显示以下error.how可以避免此错误。我的代码和错误如下

List<SDO> sdolist = new ArrayList<SDO>();   
    try{
        sdolist = TravelAdvanceRequest.generatePDF(objSession,objList); //calling service
        infoMsg="Pdf Generation Success";
        if (sdolist != null) {
            StringType stringMsg = (StringType) sdolist.get(0);
            infoMsg="pdf generation success......";
            System.out.println("pdf generation success......");
            System.out.println("file path===========>>"+stringMsg.getString());
            ServletOutputStream op          =response.getOutputStream();

         //throw new IllegalStateException("MyException");
        String filename = stringMsg.getString() == null ? "" : stringMsg.getString();
        File                f           =   new File(filename);         
        int                 length      =   0;

        ServletContext      context     =   getServletConfig().getServletContext();
        String              mimetype    =   context.getMimeType( filename );
        if(f.isFile()){
            response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" ); 
            response.setContentLength( (int)f.length() );
            response.setHeader( "Content-Disposition", "attachment; filename=\"Axis template for "+strCurrency+" account.pdf\"" );
            byte[] bbuf = new byte[1000];
            DataInputStream in = new DataInputStream(new FileInputStream(f));

       while ((in != null) && ((length = in.read(bbuf)) != -1))
            {
                op.write(bbuf,0,length);
            }

            in.close();
            response.getOutputStream().flush();
           // op.flush();
            op.close(); 
            return;
        }else{
            System.out.println("Exception throws there is no such file in the Directory : "+filename);
        }

    }

    }catch(IllegalStateException e1)
    {
        System.out.println("Illegal State Exception......"+e1.getMessage());    
    }
    catch(Exception e)
    {
        System.out.println("Error in Pdf Creation......"+e.getMessage());
        errMsg=e.getMessage();

    }

java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:619)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
    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:188)
    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118)
    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:77)
    at org.apache.jsp.DebitDetails_jsp._jspService(DebitDetails_jsp.java:374)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tuscany.sca.host.webapp.TuscanyServletFilter.doFilter(TuscanyServletFilter.java:103)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    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:235)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

您收到此错误的原因是因为您正在使用JSP执行逻辑。 JSP使用Writer的{​​{1}}对象呈现其输出,但您也在JSP中调用response。因此,您会遇到异常,因为您尝试同时使用getOutputStreamOutputStream。你没有展示你的整个JSP代码,但我猜你在主逻辑之后正在做一些输出(可能是一些空白字符)。正如@VigneshVino所说,你应该在servlet中做你的逻辑。 JSP应仅用于呈现视图。