Error while creating Jasper Report using jsp and servlet

I am creating web application in Java (my first project in Java) and I have to create Reports in that.

I’m using iReport to generate the reports.
using some reference code I have created my servlet code..
but I’m getting java.lang.ClassNotFoundException

Or can someone please share some sample code or example to generate jsper report with servlate
here is my code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stu
    ServletContext ctx = request.getServletContext();
    String reportTemplateFilePath = ctx.getRealPath("D:/Reports/DemoReport.jrxml");
    ByteArrayOutputStream reportStream = getReportOutputStream(reportTemplateFilePath);
    response.setContentLength(reportStream.size());
    response.setContentType("application/pdf");

    ServletOutputStream servletOutputStream =  response.getOutputStream();
    reportStream.writeTo(servletOutputStream);
    servletOutputStream.flush();
}

private ByteArrayOutputStream getReportOutputStream(String reportTemplateFilePath) {
      JasperReport jasperReport;
      ByteArrayOutputStream outputstream = new ByteArrayOutputStream();
      JasperPrint jasperPrint;
      try {
          jasperReport = JasperCompileManager.compileReport(reportTemplateFilePath);
          Map<String, Object> reportParameters = new HashMap<>();
          jasperPrint = JasperFillManager.fillReport(jasperReport, reportParameters, getConnection());
          JRExporter exporter = new JRPdfExporter();
          exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
          exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputstream);
          exporter.exportReport();
      } catch (JRException e) {
          e.printStackTrace();
      }
      return outputstream;
 }


Source: jsp

Leave a Reply