Developing A Web Project For JasperReports
Developing A Web Project For JasperReports
JasperReports
First create a database table, which shall be the data source for creating the reports, with
the following SQL script. If a database table has already been created, the table may be
used for this article too.
Next, configure a data source in WebLogic server with JNDI name jdbc/OracleDS. Next,
we need to download some JasperReports JAR files including dependencies. Download
the JAR/ZIP files listed below and extract the zip/tar.gz to a
directory, c:/jasperreports for example.
jasperreports/files/jasperreports/JasperReports%204.7.0/
itext-2.1.0 http://mirrors.ibiblio.org/pub/mirrors/maven2/com/
lowagie/itext/2.1.0/itext-2.1.0.jar
commons-beanutils-1.8.3-bin.zip http://commons.apache.org/beanutils/download_beanutils.cgi
commons-digester-2.1.jar http://commons.apache.org/digester/download_digester.cgi
commons-logging-1.1.1-bin http://commons.apache.org/logging/download_logging.cgi
All the JasperReports libraries are open source. We shall be using the following JAR
files to create a JasperReports report:
poi-3.8-20120326.jar,
Apache Jakarta POI classes and dependencies.
poi-excelant-3.8-20120326.jar,
poi-ooxml-3.8-20120326.jar,
poi-ooxml-schemas-3.8-20120326.jar,
poi-scratchpad-3.8-20120326.jar
Add the Jasper Reports required by the JAR files to the \\user_projects\domains\
base_domain\bin\startWebLogic.bat script's CLASSPATH variable:
set SAVE_CLASSPATH=%CLASSPATH%;C:\jasperreports\commonsbeanutils-
1.8.3\commons-beanutils-1.8.3.jar;C:\jasperreports\commonsbeanutils-
1.8.3\commons-beanutils-bean-collections-1.8.3.jar;C:\
jasperreports\commons-beanutils-1.8.3\commons-beanutils-core-
1.8.3.jar;C:\jasperreports\commons-digester-2.1.jar;C:\jasperreports\
commons-logging-1.1.1\commons-logging-1.1.1.jar;C:\jasperreports\
itext-2.1.0.jar;C:\jasperreports\jasperreports-4.7.0.jar;C:\
jasperreports\poi-3.8\poi-3.8-20120326.jar;C:\jasperreports\poi-
3.8\poi-scratchpad-3.8-20120326.jar;C:\jasperreports\poi-3.8\poiooxml-
3.8-20120326.jar;C:\jasperreports\poi-3.8.jar;C:\jasperreports\
poi-3.8\poi-excelant-3.8-20120326.jar;C:\jasperreports\poi-3.8\poiooxml-
schemas-3.8-20120326.jar
Select File | New | Other. In New wizard select Web | Dynamic Web Project. In
Dynamic Web Project configuration specify a Project name (PDFExcelReports for
example), select theTarget Runtime as Oracle WebLogic Server 11g R1 ( 10.3.5).
Click on Next.
Select the default Java settings; that is, Default output folder as build/classes, and
then click on Next. In WebModule, specify ContextRoot as PDFExcelReports
and Content Directory as WebContent. Click on Finish. A web project
for PDFExcelReports gets generated. Right-click on the project node
in ProjectExplorer and select Project Properties. In Properties, selectProject
Facets. The Dynamic Web Module project facet should be selected by default as
shown in the following screenshot:
Next, create a User Library for JasperReports JAR files and dependencies.
Select Java Build Path in Properties. Click on Add Library. In Add Library,
select User Library and click onNext. In User Library, click on User Libraries.
In User Libraries, click on New. In New User Library, specify a User library
name (JasperReports) and click on OK. A new user library gets added to User
Libraries. Click on Add JARs to add JAR files to the library. The following screenshot
shows the JasperReports that are added:
Creating the configuration file
We require a JasperReports configuration file for generating reports. JasperReports
XML configuration files are based on the jasperreport.dtd DTD, with a root element of
jasperReport. We shall specify the JasperReports report design in an XML configuration
bin file, which we have called config.xml. Create an XML file config.xml in the
webContent folder by selecting XML | XML File in the New wizard.
Some of the other elements (with commonly used subelements and attributes) in a
JasperReports configuration XML file are listed in the following table:
The PDF report has the columns CatalogId, Journal, Publisher, Edition,
Title, and Author. Specify a report band for the report title. The ReportTitle parameter
is invoked using the $P {ReportTitle} expression. Specify a column header using
the columnHeader element. Specify static text with the staticText element. Specify the
report detail with the detail element. A column text field is defined using
the textField element. The dynamic value of a text field is defined using
the textFieldExpression element:
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Cata
logId}]]></textFieldExpression>
</textField>
Specify a page footer with the pageFooter element. Report parameters are defined using
$P{}, report fields using $F{}, and report variables using $V{}. The config. xml file is
listed as follows:
The following code snippet specifies the parameter for the report title, the SQL query to
generate the report with, and the report fields. The resultset from the SQL query gets
bound to the fields.
<title>
<band height="50">
<textField>
<reportElement x="350" y="0" width="200" height="50" />
<textFieldExpression class="java.lang.
String">$P{ReportTitle}</textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band>
</band>
</pageHeader>
<columnHeader>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[CATALOG ID]]></text>
</staticText>
<staticText>
<reportElement x="125" y="0" width="100" height="20"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[JOURNAL]]></text>
</staticText>
<staticText>
<reportElement x="250" y="0" width="150" height="20"/>
<textElement> <font isUnderline="false"
reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[PUBLISHER]]></text>
</staticText>
<staticText>
<reportElement x="425" y="0" width="100" height="20"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[EDITION]]></text>
</staticText>
<staticText>
<reportElement x="550" y="0" width="200" height="20"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[TITLE]]></text>
</staticText>
<staticText>
<reportElement x="775" y="0" width="200" height="20"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[AUTHOR]]></text>
</staticText>
</band>
</columnHeader>
The following code snippet shows how to add the report detail, which consists of values
retrieved using the SQL query from the Oracle database:
<detail>
<band height="20">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Cata
logId}]]></textFieldExpression>
</textField>
<textField pattern="0.00">
<reportElement x="125" y="0" width="100" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Jour
nal}]]></textFieldExpression>
</textField>
<textField pattern="0.00">
<reportElement x="250" y="0" width="150" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Publ
isher}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="425" y="0" width="100" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Edit
ion}]]></textFieldExpression>
</textField>
<textField pattern="0.00">
<reportElement x="550" y="0" width="200" height="20"/>
<textFieldExpression class="java.lang.
String"><![CDATA[$F{Title}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="775" y="0" width="200" height="20"/>
<textFieldExpression class="java.lang.
String"><![CDATA[$F{Author}]]></textFieldExpression>
</textField>
</band>
</detail>
Add the column and page footer including the page number as follows:
<columnFooter>
<band>
</band>
</columnFooter>
<pageFooter>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="40" height="15"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Italic"/>
</textElement>
<text><![CDATA[Page #]]></text>
</staticText>
<textField>
<reportElement x="40" y="0" width="100" height="15"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Italic"/>
</textElement>
<textFieldExpression class="java.lang.
Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band>
</band>
</summary>
</jasperReport>
We need to create a JAR file for the config.xml file and add the JAR file to the WebLogic
Server's domain's lib directory. Create a JAR file using the following command from the
directory containing the config.xml as follows:
To create a JSP file, go to File | New | Other. In New, expand Web and select JSP
File and click on Next, as shown in the following screenshot:
Select the folder to create a JSP file as WebContent, specify a File
name(CatalogPDFReport.jsp or CatalogExcelReport.jsp), and click on Next.
Select the default JSP file template and click on Finish. The JSPs get added to the
project.
The JasperReports process involves creating a report design, compiling the report
design to a runnable format, setting the report parameters, creating a JDBC connection
to Oracle database, creating a print of the report, and exporting the print to the required
format such as a PDF or an Excel spreadsheet. In the CatalogPDFReport.jsp, first
import the JasperReports packages and other required packages for creating
an InitialContext object and obtaining aConnection object as follows:
InputStream input=this.getClass().getClassLoader().
getResourceAsStream("config.xml");
A JasperDesign object represents the report design. Compile the report design file to
create aJasperReport object. The compilation of the report design file validates the
JasperReports XML file (config.xml) with the jaspereports.dtd DTD and converts the
report expressions into a ready-to-evaluate form.
The database data binding to the report fields gets applied in the report configuration
file config.xml, which we created earlier.
exporterXLS.exportReport();
Just exporting doesn't generate the report. We need to output the Excel report using
theOutputStream object. Write the Excel report using
the ByteArrayOutputStream object. Subsequently, flush and close
the OutputStream object as shown in the following code snippet:
ouputStream.write(byteArrayOutputStream.toByteArray());
ouputStream.flush();
ouputStream.close();
In the following section, we shall package and deploy the web application to generate a
JasperReports PDF report and an Excel report from the example Catalog table.
Target Description
The clean target may be run to delete the WAR file and recompile it.
Start the WebLogic server if not already started. The JasperReports WAR file gets
deployed to WebLogic Server and gets listed in the Deployments table as shown in the
following screenshot:
JasperReports is not without its drawbacks. A tight-coupling between the report data
and the report layout implies that the developer would need to be familiar with both the
development aspect of obtaining the database data and the design aspect of the report
layout.
Summary
In this article we demonstrated the use of the Oracle Enterprise Pack for Eclipse’s
integrated support for WebLogic server to deploy and run any web application that
requires an application server. First, we configured a datasource JNDI in WebLogic
server. We created and deployed a web application for JasperReports to the WebLogic
server, and subsequently ran the web application to create a PDF report.