0

I've been asked to create a report in an existing webapplication (not my own, and I do not have access to its code). I've decided that JasperReports was the easiest, since there are many fine examples around...

I've written the sample below, but the browser displays an empty page, no pdf is showing. However, if I refresh the browser page, sometimes the pdf is shown.

<%@ page  import="java.io.*"%> 
<%@ page  import="javax.servlet.*"%>
<%@ page  import="java.sql.Connection"%>
<%@ page  import="java.sql.DriverManager"%>
<%@ page  import="java.util.HashMap"%>
<%@ page  import="java.util.Map"%>
<%@ page  import="javax.servlet.ServletException"%>
<%@ page  import="javax.servlet.ServletOutputStream"%>
<%@ page  import="net.sf.jasperreports.engine.*"%>
<%@ page contentType="application/pdf" %>


    <%
         Connection conn = null;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection("jdbc:sqlserver://[server][\instance]:1433;databaseName=[databasename];integratedSecurity=True");
        } catch (Exception ex) {
            ex.printStackTrace();


        }


        File reportFile = new File(application.getRealPath("/bespoke/rapport/Planning.jasper"));//your report_name.jasper file
        Map parameters = new HashMap();
        byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, conn);


                response.setContentType("application/pdf");
                response.setContentLength(bytes.length);
                ServletOutputStream outputStream = response.getOutputStream();
                outputStream.write(bytes, 0, bytes.length);
                outputStream.flush();
                outputStream.close();

           %>

The exact error I'm getting is:

java.lang.IllegalStateException: STREAM
at org.mortbay.jetty.Response.getWriter(Response.java:616) ~[jetty-6.1.24.jar:6.1.24]
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:122) ~[servlet-api-2.5-20081211.jar:na]
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:187) ~[jsp-2.1-glassfish-2.1.v20091210.jar:na]
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:180) ~[jsp-2.1-glassfish-2.1.v20091210.jar:na]

Now, I know the issue is in the last bits of code.
Problem is, how do I solve this when I am not allowed to add classes to the original program?
I can only add the .jasper file and by gods grace a simple .jsp page.
Again, I know it is extremely bad form to add code to a jsp, but I am stuck here...

4

2 回答 2

0

将我的代码更改为以下内容:

<%@ page language="java" import="net.sf.jasperreports.engine.*,net.sf.jasperreports.engine.export.*" %>  
<%@ page import="org.apache.commons.lang.time.*" %>  
<%@ page import="java.sql.*,java.io.*" %>
<%
 String filename = request.getParameter("filename");
 String reporttype = request.getParameter("reporttype");
 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
 Connection conn = DriverManager.getConnection("jdbc:sqlserver://[servername]\\[instancename];databaseName=[databasename][:portnumber(remove when dynamic port is used)];integratedSecurity=True");
           System.out.println("Connection Established");
 String path = application.getRealPath("/");

 JasperPrint jasperPrint = JasperFillManager.fillReport(path + "/" + filename, null, conn);
 System.out.println("Report Created...");

 OutputStream ouputStream = response.getOutputStream();

 JRExporter exporter = null;

 if( "pdf".equalsIgnoreCase(reporttype) )
 {
      response.setContentType("application/pdf");
      exporter = new JRPdfExporter();
      exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
      exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
 }
 else if( "html".equalsIgnoreCase(reporttype) )
 {
      exporter = new JRHtmlExporter();
      exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
      exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
 }
 else if( "xls".equalsIgnoreCase(reporttype) )
 {
      response.setContentType("application/xls");
      response.setHeader("Content-Disposition", "inline; filename=\"file.xls\"");

      exporter = new JRXlsExporter();
      exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
      exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
 }
 try 
 {
      exporter.exportReport();
 } 
 catch (JRException e) 
 {
      throw new ServletException(e);
 }
 finally
 {
      if (ouputStream != null)
      {
           try
           {
                ouputStream.close();
           }
           catch (IOException ex)
           {
           }
      }
 }
%>  

很遗憾不得不这样做,但至少它可以解决问题....

于 2013-09-04T13:01:55.410 回答
0

我昨天在 JSP 中创建和返回 Excel 文件时遇到了类似的错误。您需要删除结束 (%>) 和开始 (<%) jsp 标记之间的所有空格。

代替

 <%@ page contentType="application/pdf" %>


 <%
  Connection conn = null;

做了

 <%@ page contentType="application/pdf" %><%
  Connection conn = null;

如果您留出空间,那么 JSP 会调用 response.getOutputStream() 来创建 JspWriter,这是您不想要的,因为您需要自己调用 response.getOutputStream() 来返回二进制文件。

于 2013-08-30T20:31:03.170 回答