JasperReports:如何在JSP页面中显示报表?

时间:2010-08-04 16:37:15

标签: excel jsp pdf jasper-reports

如何在JSP页面中显示jasper报告?我正在使用iReport 1.3.3工具创建报告。

我能够创建报告,但我很难在JSP页面中显示该报告。

注意:我需要以PDF和Excel格式显示报告。

1 个答案:

答案 0 :(得分:2)

也许我可以选择你的问题。我举了这个例子。

package reports;

/**
 *
 * @author felix
 */

@javax.servlet.annotation.WebServlet(name="Reports", urlPatterns={"/reports"})
public class HtmlReportServlet extends javax.servlet.http.HttpServlet {
    //public static final String REPORT_DIRECTORY = "/reports";

    @Override
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        //javax.servlet.ServletContext context = this.getServletConfig().getServletContext();
        java.io.InputStream reportSource = getClass().getResourceAsStream("/jrmain/test.jasper");
        java.util.Map<String, Object> params = new java.util.HashMap<String, Object>();

        org.apache.log4j.Logger logger = org.apache.log4j.Logger.getRootLogger();
        logger.setLevel((org.apache.log4j.Level)org.apache.log4j.Level.WARN);

        java.io.PrintWriter printWriter = response.getWriter();
        try {
            net.sf.jasperreports.engine.JasperPrint jasperPrint = net.sf.jasperreports.engine.JasperFillManager.fillReport(reportSource, params, new apps.dbase.DBaseLink().OpenLink());
            net.sf.jasperreports.engine.export.JRHtmlExporter htmlExporter = new net.sf.jasperreports.engine.export.JRHtmlExporter();
            response.setContentType("text/html");
            request.getSession().setAttribute(net.sf.jasperreports.j2ee.servlets.ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
            htmlExporter.setParameter(net.sf.jasperreports.engine.JRExporterParameter.JASPER_PRINT, jasperPrint);
            htmlExporter.setParameter(net.sf.jasperreports.engine.JRExporterParameter.OUTPUT_WRITER, printWriter);
            htmlExporter.setParameter(net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI, "image?image=");
            htmlExporter.exportReport();
            printWriter.close();

        } catch (Exception e) {
            // display stack trace in the browser
            e.printStackTrace(printWriter);
        }
    }
}