3

I am using JapserReports for showing reports in Java. I am able to send report in PDF format to the web browser.

Now I want to send the report in HTML format, following is my method for sending report in HTML format.

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    File reportFile = new File(getServletConfig().getServletContext().getRealPath("/rpts/report1.jasper"));
    ServletOutputStream servletOutputStream = response.getOutputStream();
    byte[] bytes = null;
    Map<String,Object> parameter = new HashMap<String,Object>();
    Connection con = DBConnection.getConnection("");
    try
    {
        bytes = JasperRunManager.runReportToHtmlFile(reportFile.getPath(),parameter,con).getBytes();
        response.setContentType("text/html");
        response.setContentLength(bytes.length);
        servletOutputStream.write(bytes, 0, bytes.length);
        servletOutputStream.flush();
        servletOutputStream.close();
    }
    catch (JRException e)
    {
        System.out.println(e);
    }
}

Above method is printing path of generated HTML file. When I check the path then HTML file was generated there with the data.

So am i missing something in the code?

Thanks in advance....

Alex K
  • 22,315
  • 19
  • 108
  • 236
Bhushan
  • 6,151
  • 13
  • 58
  • 91

2 Answers2

3

Try setting Content-Disposition and the html filename.

    response.setHeader("Content-Disposition","inline, filename=myReport.html");
    response.setContentType("text/html");
    response.setContentLength(bytes.length);
    servletOutputStream.write(bytes, 0, bytes.length);
    servletOutputStream.flush();
    servletOutputStream.close();

runReportToHtmlFile method returns absolute file path. It is required to read generated html file into byte array before writing to output stream.

Below code might work for you.

        String reportPath =JasperRunManager.runReportToHtmlFile(reportFile.getPath(), parameter);
        File reportHtmlFile = new File(reportPath);
        FileInputStream fis = new FileInputStream(reportHtmlFile);
        byte[] bytes =  new byte[(int)reportHtmlFile.length()];
        fis.read(bytes);
        resp.setHeader("Content-Disposition","inline; filename=myReport.html");
        resp.setContentType("text/html");
        resp.setContentLength(bytes.length);
        servletOutputStream.write(bytes, 0, bytes.length);
        servletOutputStream.flush();
        servletOutputStream.close();
Avinash K.P
  • 167
  • 7
  • it still doesn't shows HTML file in browser, it shows filename only, but still thanks for answering(and +1), and we have to give `;` instead of `,` after `inline` – Bhushan Mar 14 '13 at 12:31
  • @Bhushan runReportToHtmlFile method returns absolute file path. It is required to read generated html file into byte array before writing to output stream. – Avinash K.P Mar 15 '13 at 18:46
-1

try this

public class ReportExample {



/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

ArrayList<City> cityList=new ArrayList<City>();

City city=new City();

city.setCityName("Kolkata");

city.setCountry("Country");

cityList.add(city);

InputStream inputStream;

try {

inputStream = new FileInputStream ("report1.jrxml");

DataBeanMaker dataBeanMaker = new DataBeanMaker();

JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(cityList);

Map parameters = new HashMap();



JasperDesign jasperDesign = JRXmlLoader.load(inputStream);

JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);

JasperExportManager.exportReportToPdfFile(jasperPrint, "testjasper.pdf"); 

   JasperViewer.viewReport(jasperPrint);

   JasperPrintManager.printReport(jasperPrint, true);

}catch(Exception e)

{e.printStackTrace();

}

}



}

 check the link http://javadispute.com/content/jasper-report-example
Biswajit
  • 2,434
  • 2
  • 28
  • 35
  • thanks for answer, but this example is not related to `how to send JapserReport as HTML`, anyway i found a solution for this using another way – Bhushan Mar 15 '13 at 04:04