作为Web应用程序的一部分在Tomcat上运行JasperViewer [英] Running JasperViewer on Tomcat as part of a web application

查看:29
本文介绍了作为Web应用程序的一部分在Tomcat上运行JasperViewer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到 JasperViewer ( JasperReports 的默认预览组件)是 Swing 组件,因此有什么方法可以转换或嵌入它在网络应用程序中?有人说我应该使用 Java Web Start ,但是从我从此链接 JWS 对于在客户端计算机上下载和安装应用程序很有用,而我们的情况并非如此.其他可能可行的解决方案(也许只是从理论上来说)是将 jFrame 转换为 jApplet ,如简短描述的

I have learned that JasperViewer (default preview component of JasperReports) is a Swing component, so is there any way to convert or embed it in a web application? Some say I should use Java Web Start, but from what i have learned from this link JWS is useful to download and install an application on client machine and this is not our case. the other work around that it may work (maybe just in theory) is converting jFrame to jApplet as briefly described in this link

  1. 您是否尝试过这些解决方案中的任何一种,它们是否起作用?
  2. 您知道其他解决此问题的方法吗?

推荐答案

如果您知道如何生成报告,则可以在servlet内轻松完成该报告,并将生成的文件发送给客户端.使用JWS应用程序或Applet很有可能意味着该报告是在客户端生成的,并且原始数据以及所有依赖项也可供客户端使用.

If you know how to generate a report, you can easily do it inside a servlet and send the generated file to the client. Using a JWS application or an Applet would most likely mean that the report is generated client-side and that the raw data plus all the dependencies are also available to the client.

下面的代码假定您正在生成PDF文件

The code below assumes that you're generating a PDF file

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 ...)来扩展上面的示例以支持多种导出格式

You can extend the example above to support multiple export formats by setting the correct content type and using the matching JRXYZExporter (JRHtmlExporter, JExcelApiExporter,...)

如果您需要更多可定制的内容,则可能还需要研究碧玉服务器

If you need something more customizable, you might also want to look into Jasper Server

这篇关于作为Web应用程序的一部分在Tomcat上运行JasperViewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆