| By Peter Sellars | Article Rating: |
|
| January 25, 2006 08:30 PM EST | Reads: |
139,435 |
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
- JasperReports Ultimate Guide (Version 2.0), Teodor Danciu, JasperSoft Corporation.
- JasperReports Web site - http://jasperreports.sourceforge.net
- iReport Website - http://ireport.sourceforge.net
- JasperAssistant Web site - www.jasperassistant.com
- Hibernate Web site - www.hibernate.org
- Using JasperReports with Hibernate - www.hibernate.org/79.html
- Hibernate Querying 103: Building Reports with JasperReports - www.javalobby.org/articles/hibernatequery103/?source=archives
- iText Website - www.lowagie.com/iText/
- Apache Jakarta POI Web site - http://jakarta.apache.org/poi/
Published January 25, 2006 Reads 139,435
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
![]() |
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 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? 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. |
||||
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- Confessions of a Ulitzer Addict
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- It's the Java vs. C++ Shootout Revisited!
- Cloud Computing Can Revitalize Your Career as Software Developer
- IBM Could "Reinvent" Java: Mills
- Oracle & Cloud Computing: Exclusive Q&A with SVP Richard Sarwal
- A Brief History of Cloud Computing
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?








































