开发者

JasperReports: how to display report in JSP page?

开发者 https://www.devze.com 2023-01-10 10:15 出处:网络
How to display jasper reports in JSP page? I am using iReport 1.3.3 tool to create repor开发者_C百科ts.

How to display jasper reports in JSP page? I am using iReport 1.3.3 tool to create repor开发者_C百科ts.

I am able to create reports but I am struggling to display that report in JSP page.

Note: I need to display the report in PDF and Excel format.


Maybe I can give an option to your question. I give you this example.

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);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消