我是JasperReports
的新手,并且不知道如何从servlet调用jasper文件。我的报告包含饼图。
答案 0 :(得分:5)
您可以准备Jasper文件并将其传输到客户端。
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);
答案 1 :(得分:1)
这是在Servlet文件中创建的虚拟报告。
它与普通Java类中的相同。
只需确保您在文件顶部有jasper报告类的导入。
下面的示例从XML数据源构建报告。
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();
}
}
}