0
I am with a problem , every time q I will generate a report for the first time it takes to generate, apart from the second time it goes very fast , I’m sending the code that generates the report if anyone can help
public void relatorioUSALL(List lista, String tipo, Map param, String tip, String nomrel) {
try {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context
.getExternalContext().getResponse();
ServletOutputStream responseStream = response.getOutputStream();
String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/Report/" + tipo + ".jrxml");
//InputStream pathjrxml = getClass().getResourceAsStream(path);
//String path = ("C:\\Users\\Administrator\\Desktop\\glassfish4\\glassfish\\domains\\domain1\\applications\\Hibernate\\Report\\" + tipo + ".jrxml" );
if (tip.equals("0")) {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=\"" + nomrel + ".pdf\"");
JasperReport pathReport = JasperCompileManager
.compileReport(path);
JasperPrint print = JasperFillManager.fillReport(pathReport, param,
new JRBeanCollectionDataSource(lista));
JasperExportManager.exportReportToPdfStream(print, responseStream);
responseStream.flush();
responseStream.close();
context.renderResponse();
context.responseComplete();
} else if (tip.equals("1")) {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\"" + nomrel + ".pdf\"");
JasperReport pathReport = JasperCompileManager
.compileReport(path);
JasperPrint print = JasperFillManager.fillReport(pathReport, param,
new JRBeanCollectionDataSource(lista));
JasperExportManager.exportReportToPdfStream(print, responseStream);
responseStream.flush();
responseStream.close();
context.renderResponse();
context.responseComplete();
} else if (tip.equals("3")) {
response.setContentType("application/ms-excel");
response.addHeader("Content-disposition", "attachment; filename=" + nomrel + ".xls");
JasperReport pathReport = JasperCompileManager
.compileReport(path);
JasperPrint print = JasperFillManager.fillReport(pathReport, param,
new JRBeanCollectionDataSource(lista));
ServletOutputStream servletOutputStream = response.getOutputStream();
JRExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, print);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, servletOutputStream);
exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporterXLS.exportReport();
responseStream.flush();
responseStream.close();
context.renderResponse();
context.responseComplete();
} else if (tip.equals("2")) {
response.addHeader("Content-disposition", "attachment; filename=" + nomrel + ".docx");
JasperReport pathReport = JasperCompileManager
.compileReport(path);
JasperPrint print = JasperFillManager.fillReport(pathReport, null,
new JRBeanCollectionDataSource(lista));
ServletOutputStream servletOutputStream = response.getOutputStream();
JRDocxExporter docxExporter = new JRDocxExporter();
docxExporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
docxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream);
docxExporter.exportReport();
FacesContext.getCurrentInstance().responseComplete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Dude, by reading your problem quickly here, according to what you describe may be your Objects list, you didn’t say whether you removed it from a bank or if you’re taking the items straight from the bank to the report via Query, you must be initiating unnecessary items when you have your report generated, add the tag that lists objects in the database the following @Manytoone(fetch = Fetchtype.LAZY) .
– Julio Cesar da Silva Barros
As the friend commented above, the problem may be before performing this function, a lengthy database query for example, can make a simple test to get somewhere: puts at the beginning and end of the method a variable to capture the time it took to execute and at the end, prints the result on the console, so you can already get a sense of whether the problem is really within the method. ex: long inicio = System.currentTimeMillis(); / long fim = System.currentTimeMillis(); System.out.print(end - start);, you will get the response in milliseconds.
– Geferson
I did the test here by taking the generation of the report, leaving only the list being populated by the database and it was quick, so there’s no problem in the database
– Felipe Souza
Felipe Souza, have you found a solution? I’m also having this problem, I’m thinking of fetching the data and working on the application, and letting ireport only display this data.
– Diego Gomes