在jsp中避免java.lang.IllegalStateException [英] Avoiding java.lang.IllegalStateException in jsp

查看:76
本文介绍了在jsp中避免java.lang.IllegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在jsp中下载生成的pdf,但是我的日志显示以下错误.如何避免该错误.我的代码和错误如下

i try to download a generated pdf in jsp.but my log shows following error.how can i avoid this error.my code and error are the following

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)

推荐答案

出现此错误的原因是因为您正在使用JSP进行逻辑处理. JSP使用responseWriter对象呈现其输出,但是您也在JSP内部调用getOutputStream.因此,由于尝试同时使用OutputStreamWriter,因此会出现异常.您没有显示完整的JSP代码,但是我猜您在执行主逻辑后正在执行一些输出(可能是一些空白字符).正如@VigneshVino所说,您应该改为在servlet中执行逻辑. JSP仅应用于呈现视图.

The reason you're getting this error is because you're doing your logic with a JSP. A JSP renders its output with using the Writer object of the response, but you are calling getOutputStream inside the JSP as well. Therefore, you'll get an exception since you're trying to use both the OutputStream and the Writer at the same time. You haven't showed your entire JSP code, but I'm guessing you're doing some output (maybe some blank characters) after your main logic. As @VigneshVino says, you should do your logic inside a servlet instead. A JSP should only be used to render the view.

这篇关于在jsp中避免java.lang.IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆