JasperReports: Calling report from servlet [duplicate]

给你一囗甜甜゛ 提交于 2020-01-11 02:31:48

问题


I'm new to JasperReports and dont know how to call jasper file from servlet. My report contains the pie chart.


回答1:


You can prepare the Jasper file and stream it to the client.

bytes[] byteStream = JasperRunManager.runReportToPdf("myJasperReport.jasper",paramMap,databaseConn);

OutputStream outStream = servletResponse.getOutputStream();
response.setHeader("Content-Disposition","inline, filename=myReport.pdf");
response.setContentType("application/pdf");
response.setContentLength(byteStream.length);
outStream.write(bytes,0,bytes.length);



回答2:


A complete way to do this from the servlet would be:

public void myServletMethod(HttpServletRequest request, HttpServletResponse response) throws IOException{
            JasperReport jasperReport = null;
            JasperDesign jasperDesign = null;
            Map parameters = new HashMap();
            String path = getServletContext().getRealPath("/WEB-INF/");
            jasperDesign = JRXmlLoader.load(path+"/relative/path/to/MyReport.jrxml");
            jasperReport = JasperCompileManager.compileReport(jasperDesign);
            byte[] byteStream = JasperRunManager.runReportToPdf(jasperReport, parameters, **new DataSourceOfYourPreference**);                            
            OutputStream outStream = response.getOutputStream();
            response.setHeader("Content-Disposition","inline, filename=myReport.pdf");
            response.setContentType("application/pdf");
            response.setContentLength(byteStream.length);
            outStream.write(byteStream,0,byteStream.length);    

    }



回答3:


Here is a dummy report created within a Servlet file.

It is the same as it would be in normal Java class.

Just make sure you have the imports for your jasper report classes at the top of the file.

The bellow example builds a report from an XML datasource.

public class JasperServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        try {
            String reportFile = "myJasperReport.jrxml";
            File outputFile = new File("Report.pdf");
            HashMap hm = new HashMap();

            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory
                    .newDocumentBuilder();
            Document document = documentBuilder.parse(new File("myXml.xml"));

            // Compile the report
            JasperReport report = JasperCompileManager
                    .compileReport(reportFile);
            JRXmlDataSource xml = new JRXmlDataSource(document, "/xml/root");
            // Fill the report
            JasperPrint print = JasperFillManager.fillReport(report, hm, xml);
            // Create an Exporter
            JRExporter exporter = new JRPdfExporter();
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE, outputFile);
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            // Export the file
            exporter.exportReport();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


来源:https://stackoverflow.com/questions/2399507/jasperreports-calling-report-from-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!