Welcome!

Java Authors: Frank Huerta, Adrian Bridgwater, Pieter Van Heck, Esmeralda Swartz, Gary Kaiser

Related Topics: Java

Java: Article

Reporting Made Easy with JasperReports and Hibernate

JasperReports and Hibernate in Web applications

More extensive solutions to using Hibernate with JasperReports, including the use of reflection instead of the name mapping method used in HibernateDataSource, can be found on the Hibernate Web site www.hibernate.org/79.html. Also of interest in this area is the report optimization implementation advocated by John Ferguson Smart in his article "Hibernate Querying 103: Using Hibernate Queries with JasperReports" (see Resources).

Exporting Reports to PDF and XLS Formats in Web Applications
After compiling and filling a JasperReport report exporting it is a fairly simple and straightforward process using the net.sf.jasperreports.engine.JRExporter interface implementations provided. JasperReports can export data to PDF, XLS, CSV, RTF, HTML, and XML from the same report design using the appropriate implementation of the JRExporter interface. The PDF and XLS formats are two of the most common export formats and examples of exporting to these formats from within a Web application can be found in the source code for this article. PrintServlet exports to PDF, while DataExtractServlet exports the same data to an XLS format file.

PrintServlet (see Listing 9) is a an example servlet implementation class using JasperReports to export a report to PDF format. JasperReports makes use of the Open Source iText PDF creation library (see Resources) to generate PDF format files. Once the report is compiled in PrintServlet, the PDF is created and streamed to the Web browser ready for printing using the runReportToPdfStream(InputStream inputStream, OutputStream outputStream, Parameters params, Connection connection) method implemented by the JasperRunManager facade class.

DataExtractServlet (see Listing 10) is an example servlet implementation class using JasperReports to export a report to the XLS format. JasperReports makes use of the Apache POI library (see Resources) to generate XLS format files. Once the report is compiled in DataExtractServlet the XLS file is created in memory and a save dialog is displayed to the user. The servlet uses net.sf.jasperReports.engine.export.JRXlsExporter, one of the concrete implementations of the JRExporter interface provided by JasperReports to export the report. The parameters for exporting the report are initialized using JRXlsExporterParameter variables to set the filled report (JRXlsExporterParameter.JASPER_PRINT) and the output stream (JRXlsExporterParameter.OUTPUT_STREAM) - which is the response object that has had its content type and header set so that the file will be made available to the user for saving rather than displayed as in the PrintServlet example when exportReport() is called.

Useful Hint: By default JasperReport puts page headings at the top of every 'page' of data. When exporting to an XLS format this breaks up the continuous data in a worksheet that contains more than a single 'page' of data. Data continuity can be maintained by passing the type of output format as a parameter to a report template combined with a <printExpression> element based on the passed parameter placed in the <pageHeader> element. The <printExpression> below will result in only the page headings being output to a 'page' if the report is processing the first page when the output format isn't PDF and on every 'page' for PDF output formats.

<printWhenExpression>
<![CDATA[$V{PAGE_NUMBER}.intValue() == 1
|| $P{REPORT_TYPE}.equals("PDF")
? Boolean.TRUE : Boolean.FALSE]]>
</printWhenExpression>

Creating Reports Is Easy and Fun with JasperReports
Hopefully this article has whetted your appetite for exploring the world of report generation using JasperReports, or if you've already discovered JasperReports, that it's provided some ideas on how to delve into creating custom data sources or using new export formats. Understanding and mastering the implementation of the required JRDataSource methods next() and getFieldValue(JRField jrField) opens up any data source for use in generating reports with JasperReports.

Creating reports with JasperReports is made even simpler by some useful tools. iReport (see Resources), an excellent JasperReports template creation tool that allows visual report designs in a GUI application can be used by non-developers to create the JasperReport designs. It also offers substantial developer-focused functionality such as data source connectivity to create report previews outside of an application. JasperAssistant (see Resources), while not Open Source has the advantage of being an Eclipse plug-in for developing JasperReport templates in a similar GUI manner, albeit more developer-oriented. Both offer the benefit of being able to prepare a report design, which can then be provided to a developer for filling, relieving him of the tedious presentation aspect of report generation.

This article has barely scratched the surface of JasperReports' extensive use and functionality but hopefully it's introduced some developers to an extremely useful tool in any Java developer's arsenal. JasperReports can even produce charts and graphs, as well as including images in reports that increase the richness and presentation of an applications reporting system. JasperReports is a powerful API that can take a reporting system to the next level.

Resources

  1. JasperReports Ultimate Guide (Version 2.0), Teodor Danciu, JasperSoft Corporation.
  2. JasperReports Web site - http://jasperreports.sourceforge.net
  3. iReport Website - http://ireport.sourceforge.net
  4. JasperAssistant Web site - www.jasperassistant.com
  5. Hibernate Web site - www.hibernate.org
  6. Using JasperReports with Hibernate - www.hibernate.org/79.html
  7. Hibernate Querying 103: Building Reports with JasperReports - www.javalobby.org/articles/hibernatequery103/?source=archives
  8. iText Website - www.lowagie.com/iText/
  9. Apache Jakarta POI Web site - http://jakarta.apache.org/poi/

More Stories By Peter Sellars

Peter Sellars is the lead devloper at Netbyte Internet Ltd
(www.netbyte.com) in Auckland, New Zealand. His main interests are project
automation and application architecture. He aims to simplify developer jobs by
implementing tools that enable them to utilise their time better. He
enjoys playing soccer and tenpin bowling.

Comments (8) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Abhilash.T.G 10/01/07 10:40:22 AM EDT

Very informative at the same time simple presentation.

Rob K 02/27/06 09:00:51 PM EST

ant build runs (compiles) the reports, looks like directory tree good from build, but doesn't create a .war file?

anyone offer assistance?

also, anyone figure out how to run this under ireport 1.1 and hibernate v3?

thanks
rob

Rob K 02/27/06 01:26:22 PM EST

seems to be very 'glossed' over in terms of what versions of the products are used or setup...

1. like why is this hibernate v2 and not v3?
2. what versions of jasper & ireport are being used?
3. how is a datasource configured for hibernate via ireport?

too much missing that's beyond the code (but would love to hear the 'rest of the story')

rob

Yvonne Gevers 02/27/06 05:23:32 AM EST

Hi,

If I want to put the result of the example into a jsp page, but I don't know how to do this. Can you give me an example of this?

Thank you.

Yvonne

James Nixon 02/20/06 03:39:19 PM EST

http://res.sys-con.com/story/jan06/171467/sellars1101.zip

I found the source by following the "See Listing 1" link on the first page.

Jose Gulisano 02/10/06 12:57:45 PM EST

Any news on source code? I t would really help given the well written article.

Thanks!!

Ashutosh Mittal 02/09/06 12:06:51 PM EST

Where to download the source code for this excellent article?

news desk 01/25/06 09:52:50 PM EST

JasperReports is a valuable and viable reporting solution for Java Web applications. It simplifies report generation through the use of XML report templates that are then compiled using the JasperReports engine for use in reporting modules. These compiled report templates can be filled by data received from a variety of sources including relational databases. JasperReports can be integrated into Web applications and create reports in several file formats including PDF and XLS.