Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

java - Why does JasperViewer only works on localhost?

I was just wondering why JasperViewer only works on localhost. When I deployed my project to server, clients can not be able to view the reports anymore.

String reportDir = getServletContext().getRealPath("WEB-INF/classes/com/proj/reports");
        String fileName = reportDir + "" + request.getParameter("reportName") + ".jasper";
        File outReportDir = new File("C:/REPORTS_FOLDER");
        outReportDir.mkdir();
        long millis = System.currentTimeMillis();
        String outFileName = outReportDir + "" + request.getParameter("reportName") + "_" + millis + ".pdf";
        HashMap parameters = new HashMap();
        parameters.put("P_BOOKING_MONTH", request.getParameter("selMonth"));
        parameters.put("P_BOOKING_YR", request.getParameter("selYear"));

        try {
            Connection conn = ConnectionUtil.getConnection();
            JasperPrint print = JasperFillManager.fillReport(fileName, parameters, conn);
            JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName);
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            exporter.exportReport();

            JasperViewer.viewReport(print, false); }...
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

JasperViewer is a swing component it is launched on the computer that executes the command (normally used in installed applications), hence if you execute the command on server it will open on server (or throw a HeadlessException, if not configured to have a screen), conclusion we can't use this command in our server application.

You could use an applet to launch the command on client computer but I strongly recommend not to use this (its support in browser is decreasing, so you can't be sure that it's working for all clients)

Normally what is done instead is that an export to pdf (html or other format of choice) is sent to client browser, client can open file with favorite program and preview it.

In your example code you are already exporting to pdf, pass the pdf directly to client. Hence remove

JasperViewer.viewReport(print, false);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...