Running JasperViewer on Tomcat as part of a web application

后端 未结 1 779
半阙折子戏
半阙折子戏 2021-01-19 16:53

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

1条回答
  •  伪装坚强ぢ
    2021-01-19 17:34

    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.

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

    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

    0 讨论(0)
提交回复
热议问题