Advertisment

Generating reports with Jasper Reports

author-image
CIOL Bureau
Updated On
New Update

By: Anadi Misra



Jasper Reports is a popular open source reporting solution written completely in Java.

It can be integrated into any Java or

Java EE
project. We know about its ability to create reports in multi-formats

and its ability to integrate with web pages. However, this product  is not well

documented. In this article, we look into Jasper Reports and associated tools

and their applicability to various scenarios.

































Direct

Hit!
Applies to: Java

developers
USP: Simplify

report making process in Java using the Jasper toolkit
Links:

href="http://jasperreports.sourceforge.net/">http://jasperreports.sourceforge.net/ 
Google keywords:

Open Source

Reports


 



 



 



How it works

Jasper Reports uses XML to define its

report files. The format used is rather conventional and the definition files
are saved with a JRXML extension. This definition file contains all the

information about the report such as title, headers and footers, and of course,

the report details. All this information is characterized and defined within

respective tags. 



After the report has been defined, it needs to be compiled into

a binary format to view the report in an existing Java application. You can even

integrate it with web pages such as those from JSP or even JSF pages. To use it,

you simply need to download the package from http://jasperreports.

sourceforge.net and make sure you have the JARs in the DIST folder of the Jasper

Reports package folder and the path included in the CLASSPATH variable.













src='/IMG/174/12174/openway01-may2k6.jpg' width=300

border=0>
The Jasper Assistant plugin for

Eclipse allows visual configuration of data source and designs the report from

the 'Jasper Assistant Perspective'


Sample report

The first step in using Jasper

Reports is to write the report definition file. You can use any text or WYSIWYG
editor. We will design a simple report that displays 'Hello World' in a PDF

document. The report definition invariably includes the following

definitions.    



For the sake of simplicity, we have omitted the tags that define the header and footer respectively.

All the report data is displayed under the tag, ie, after all

formatting information (such as headers and title) has been defined. The content

of report is defined under the detail tag. The tag used here

displays static text in the report. The tag defines a report section; all

of the elements contain a 'band' element as its only child element.



Next, we

need to compile this into a binary file. This can be achieved by calling the

compileReport()
method on the 'net.sf.jasperreports.engine.

JasperCompileManager'
class. Of many methods available, in this example, we will

use the one that takes a single String parameter. The following code handles

report display in the Java class.



// import the library



import net.sf.jasperreports.engine.*;

...



JasperReport jasRep;

JasperPrint jasPrnt;



Try {

      jasRep =

JasperCompileManager.compileReport(
        Â

"reports/demo.jrxml");
      jasPrnt =

JasperFillManager.fillReport(
          jasRep, new HashMap(),

new JREmptyDataSource());
    Â

JasperExportManager.exportReportToPdfFile(
          jasPrnt,

"reports/demo.pdf");
} catch (JRException exp) {



      exp.printStackTrace();

}

















src='/IMG/175/12175/openway02-may2k6.jpg' width=250

border=0>
Use any text editor to write the XML
file that creates the report. Jasper then pulls required data from a data source

and saves it


The first statement compiles the report into a binary format.

The binary skeletal framework of the report then needs some data, (static “Hello

World!” text in our case) which is provided by the fillReport() method in the

code. 



Lastly, we have exported the report to a PDF file using the

exportReportToPdfFile()
as seen in the preceding code snippet. For this example

to compile and run, you need the following string in your CLASSPATH:

jasperreports-1.2.1.jar; commons-beanutils-1.5.jar; commons-collections-2.1.jar;

commons-digester- 1.7.jar; commons-logging-1.0.2.jar; commons-logging-api-1.0.2.

jar; itext-1.3.1.jar.



The version numbers can be different depending on the version

of jasper reports being used. Also note that you need to import

'poi-2.0-final-20040126.jar'
instead of 'itext-1.3.x.jar' if you intend to

export to an Excel file.



Jasper tools

The above example was a demonstration

of Jasper Reports, which in any case is more capable than what we
made it to do. For more advanced scenarios, you can use the GUI based Jasper

Assistant, which helps you visually build the report, saving your time and

effort. Jasper Assistant can be downloaded from href="http://www.jasperassistant.com">www.jasperassistant.com.Â




The assistant is based on Eclipse's plug-in architecture. So

all you need to do is download the package and extract it to the Eclipse

installation folder and start IDE. However, you need also plug-ins such as EMF

(Eclipse Modeling Framework) SDK and GEF (Graphical Editing Framework) SDK

installed in your Eclipse IDE before you can use the Jasper Assistant. The

plug-ins are available at Eclipse's website for free.



Once all the plug-ins are installed, you can simply add a

report to your application. First, create a data source for your report through

the Data Sources section available in 'Window>Preferences>Jasper

Assistant'. Switch to new perspective from Window>Open

Perspective>Other>Jasper Assistant and then choose the File>

New>Report menu to create new Report.



The New Report Wizard then guides you through the initial

steps of report creation, where it connects to the database over a data source

and lets you specify the query or select the fields that will be displayed in

the report. To design the report, you can drag the fields you need to show into

the design editor. Also, you can add bands such as title, page header and column

headers and also format them from the 'Properties' window in the Jasper

Assistant Perspective.



Tweaking app servers for Jasper reports

Jasper

Reports can also be displayed from Web pages. However, you need to
make following changes in your app server for that. For Sun's application

server, add the below line in the D:\Sun\

AppServer\domains\domain1\config\server.policy file, assuming that 'domain1' is

the currently used domain in the application server.



grant{ permission

java.lang.RuntimePermission "createClassLoader"};



Similarly, for Tomcat, add the following line to the startup

script (startup.bat for Windows and startup.sh for Linux):



CATALINA_OPTS="$CATALINA_OPTS

-Djava.awt.headless=true"



So, whenever you have to make complex reports, you know which

way you have to head.

Source: PCQuest

tech-news