MoienGK
MoienGK

Reputation: 4654

Running JasperViewer on Tomcat as part of a web application

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. Have you tried any of these solutions and did they work?
  2. Do you know any other solution to this problem?

Upvotes: 4

Views: 1995

Answers (1)

Guillaume
Guillaume

Reputation: 14656

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

Upvotes: 2

Related Questions