[英]Running JasperViewer on Tomcat as part of a web application
如果您知道如何生成报告,则可以轻松地在servlet内完成报告,并将生成的文件发送给客户端。 使用JWS应用程序或Applet很有可能意味着该报告是在客户端生成的,并且原始数据以及所有依赖项也可供客户端使用。
以下代码假定您正在生成PDF文件
public class ReportServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
// initialize your report objects here
JasperReport jasperReport =
JasperPrint print =
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, resp.getOutputStream());
resp.setContentType("application/pdf");
exporter.exportReport();
} catch (Exception e) {
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error generating report : " + e.getClass() + " " + e.getMessage());
}
}
您可以通过设置正确的内容类型并使用匹配的JRXYZExporter(JRHtmlExporter,JExcelApiExporter等)来扩展上面的示例以支持多种导出格式。
如果您需要更多可定制的内容,则可能还需要研究Jasper Server
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.