0

I am integrating JasperReports in my project but I am new to Jasper Report, So I have written a servlet , which will compile, fill report and export to pdf. After that I have to send outputstreem to jsp and display the pdf on client side.

I have done like this.

  <a href="VehicleReports">Vehicle Report</a>

and in doPost() method I have written like this.

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection con=null;
        try {
          response.setContentType("application/pdf"); 
          con=VehicleDetails.getConnection();
          InputStream inputStream = new FileInputStream ("E:/ReportFld/report4.jrxml");
          Map parameters = new HashMap();
          JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
          JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
          @SuppressWarnings("unchecked")
          JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, con);
          JasperExportManager.exportReportToPdfFile(jasperPrint, "D:/reports/vehicle2_jasper.pdf");
            int len = (int)new File("D:/reports/vehicle2_jasper.pdf").length(); 
            response.setContentLength(len); 
            byte[] buf = new byte[len]; 
            FileInputStream pdfin = new FileInputStream("D:/reports/vehicle2_jasper.pdf"); 
            pdfin.read(buf); 
            pdfin.close(); 
            OutputStream pdfout = response.getOutputStream(); 
            pdfout.write(buf,0,len); 
            pdfout.flush();  
            request.getRequestDispatcher("/vehicleReport.jsp").forward(request, response);

        }catch(Exception e){ 
           e.printStackTrace();
         }finally{
            try {
                con.close();
            }catch (SQLException e) { 
                e.printStackTrace();
            }
        }
    }

But here what I feel is, the response is not sending to /vehicleReport.jsp file , instead the generated pdf is getting downloaded. and also I am getting error in console as,

java.lang.IllegalStateException: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:348)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
at com.reports.VehicleReports.doGet(VehicleReports.java:78)

How can I solve this can any one help me in this?

Alex K
  • 22,315
  • 19
  • 108
  • 236
Raghu
  • 1,324
  • 7
  • 24
  • 47
  • possible duplicate of [How to display the reports on client side jsp?](http://stackoverflow.com/questions/25908004/how-to-display-the-reports-on-client-side-jsp) – Alex K Sep 22 '14 at 10:23
  • check this http://stackoverflow.com/questions/2123514/java-lang-illegalstateexception-cannot-forward-after-response-has-been-committe – Killer Sep 22 '14 at 10:24
  • @AlexK yes it was good but I want to display it in jsp, by doing above method its getting downloaded. – Raghu Sep 22 '14 at 11:13

1 Answers1

0

Since your approach is not working, you can try an alternate one. First get to your view (JSP) and then call the controller to generate PDF and display there. Suppose the URL patter for your servlet is PdfView, then you can write.

<a href="PdfView">View Pdf</a>

or better embed the pdf in an iframe

<iframe src="PdfView" width="100%" height="600"></iframe>

From your controller remove the line

request.getRequestDispatcher("/vehicleReport.jsp").forward(request, response);

Alternatively you can also give the complete path of your generated file and skip over the whole servlet part. But that may expose your directory structure to client which you would want to avoid that.

Moreover, since JasperReport can also create the 'stream' without creating the actual file in server, you can use that too. This will create the file on the client browser and don't have to bother about cleaning your server. See [here](http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperExportManager.html#exportToPdfStream(java.io.InputStream, java.io.OutputStream))

JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);

The JSP code to get things done as per your last comment. A little jquery is very helpful in this case.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){


    $('#form').submit(function(){

        var action = "VehicleReports?vehicleModel=";
        var input = $('#input').val();

        action = action+input;

        alert(action);

        $('#frame').attr("src",action);

        return false;
    });
});

</script>

</head>
<body>


<form action="" method="get" id="form">

    <input type="text" id="input"/>
    <input type="submit" value="submit" />

</form>


<iframe src="" width="100%" height="600" id="frame"></iframe>

</body>
</html>
ares
  • 4,283
  • 6
  • 32
  • 63
  • This is fine , but in project it has to be dynamic. – Raghu Sep 22 '14 at 11:15
  • It has another problem also, here it works fine for `` tag but it wont work for `
    `
    – Raghu Sep 22 '14 at 11:16
  • you can use a hidden form field to pass the parameter you want in case of forms. In case of href you can use `PdfView?param=value` where you can pass the value from your webpage. – ares Sep 22 '14 at 11:21
  • yes I understand `exportReportToPdfStream()` method will produce streem. but my problem is, how to send this streem to next jsp file and using that how can I display report. Can you help me in this with some code snnipet. – Raghu Sep 22 '14 at 11:21
  • I already mentioned how to do that, get to your JSP first and then call your controller from there. Please refer to my previous comment on how to pass dynamic value to controller – ares Sep 22 '14 at 11:25
  • no its not going to servlet , I am trying with button , `
    `
    – Raghu Sep 22 '14 at 11:51
  • If I press submit I want to display the vehicles of given model. – Raghu Sep 22 '14 at 11:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61676/discussion-between-raghu-and-ares). – Raghu Sep 22 '14 at 12:17
  • How I am trying is , there is a text box and submit button , just bellow to that empty frame is there. I enter the value in textbox and submit it. the report should display in empty frame. – Raghu Sep 22 '14 at 12:20