The most reliable and efficient way to execute a report through a web app (for this example I’ll be using servlets) is to have the report pre-compiled and ready. You can place ur SQL Qurrey inside the report itself or pass a java ResultSet object when creating a report.
First you need to convert your compiled .jasper file to a java object. Jasper API provides the class called JasperReport for this purpose. We also need to know the templateURL (The physical location of the jasper file on the server)
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperReport;
JasperReport jasperReport = (JasperReport) JRLoader.loadObject (templateURL);
Then Create the JasperPrint Object using the parameters and the resultset.
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JRResultSetDataSource(resultSet));
resultSet = the SQL result set containing your data.
Parameters = the HashMap containing your parameters.
Then Load a ByteStream and “Flush” The report
OutputStream outputStream = servletResponse.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
exporter.exportReport();
byteStream = baos.toByteArray();
outputStream.write(byteStream);
outputStream.flush();
outputStream.close();
You can call this code and flush the contents to a new window or put it in the same screen. For HTML Export you can use JRHtmlExporter instead of JRPdfExporter.
8 comments:
Hi,
Could you explain how to flush the contents to a new window with this code?
Thanks
if ur using a web app open a new window and point the bytestream to it..
Hi, i'm using an applet and i don't really know if there is a way to do the same with.
Can u help me?
Thanks
create a JasperPrint object as shown above and use the JRViewer Class (which extends Swing.JPanal)
JRViwer jrViwer = new JRViewer(jasperPrint);
after that u can just add it to the applet you want.
http://forum.springsource.org/showthread.php?t=13233
Hi,
I want to use JRResultSetDataSource as the data source for designing my UI in iReport.
I could not find any way to use it directly where i can specify my class returning the ResultSet.
Could you please help me how to use JRResultSetDataSource in iReports?
Do i need touse 'Custom JRDataSource' and point it to my own custom class which uses JRResultSetDataSource in its builder?
Any sample example will be a great help.
Thanks in advance.
Vibhav Agrawal
@Vibhav
See the constructor for JRResultSetDataSource. It takes a SQL Result set
Check this: http://www.slideshare.net/ajdgeniz/manual-reportes-jsp
interesting blog
Post a Comment