0% found this document useful (0 votes)
77 views23 pages

Developing A Web Project For JasperReports

The document outlines the steps to set up a development environment for JasperReports projects, including installing required software, configuring a database, adding JAR files to the classpath, and creating a sample report configuration XML file to define report elements like the title, header, and detail sections.

Uploaded by

Payal Mohanty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views23 pages

Developing A Web Project For JasperReports

The document outlines the steps to set up a development environment for JasperReports projects, including installing required software, configuring a database, adding JAR files to the classpath, and creating a sample report configuration XML file to define report elements like the title, header, and detail sections.

Uploaded by

Payal Mohanty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Developing a Web Project for

JasperReports

Setting the environment


First, we need to install the required software, Oracle Enterprise Pack for Eclipse 12c,
fromhttp://www.oracle.com/technetwork/middleware/ias/ downloads/wls-main-
097127.htmlusing Installers with Oracle WebLogic Server, Oracle Coherence and Oracle
Enterprise Pack for Eclipse, and download the Oracle Database 11g Express Edition
fromhttp://www.oracle.com/technetwork/products/express-
edition/overview/index.html. Setting the environment requires the following tasks:

 Creating database tables

 Configuring a data source in WebLogic Server 12c

 Copying JasperReports required JAR files to the server classpath

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.

CREATE TABLE OE.Catalog(CatalogId INTEGER PRIMARY KEY, Journal


VARCHAR(25), Publisher VARCHAR(25),Edition VARCHAR(25), Title
Varchar(45), Author Varchar(25));
INSERT INTO OE.Catalog VALUES('1', 'Oracle Magazine', 'Oracle
Publishing', 'Nov-Dec 2004', 'Database Resource Manager', 'Kimberly
Floss');
INSERT INTO OE.Catalog VALUES('2', 'Oracle Magazine', 'Oracle
Publishing', 'Nov-Dec 2004', 'From ADF UIX to JSF', 'Jonas Jacobi');
INSERT INTO OE.Catalog VALUES('3', 'Oracle Magazine', 'Oracle
Publishing', 'March-April 2005', 'Starting with Oracle ADF ', 'Steve
Muench');

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.

JAR/ZIP Donwload URL


jasperreports-4.7.0.jar http://sourceforge.net/projects/

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

poi-bin-3.8-20120326 zip or tar.gz http://poi.apache.org/download.html#POI-3.8

All the JasperReports libraries are open source. We shall be using the following JAR
files to create a JasperReports report:

JAR File Description

commons-beanutils-1.8.3.jar JavaBeans utility classes

commons-beanutils-bean-collections-1.8.3.jar Collections framework extension classes

commons-beanutils-core-1.8.3.jar JavaBeans utility core classes

commons-digester-2.1.jar Classes for processing XML documents.


commons-logging-1.1.1.jar Logging classes

iText-2.1.0.jar PDF library

jasperreports-4.7.0.jar JasperReports API

 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

Creating a Dynamic Web project in Eclipse


First, we need to create a web project for generating JasperReports reports.

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:

XML Element Description Sub-Elements Attributes

jasperReport Root Element reportFont, parameter, name, columnCount,


queryString, field, variable, pageWidth, pageHeigh
group, title, pageHeader, orientation,
columnHeader, detail, columnWidth,
columnFooter, pageFooter. columnSpacing,
leftMargin, rightMargin
topMargin,
bottomMargin.

reportFont Report level font - name, isDefault,


definitions fontName, size, isBold
isItalic, isUnderline,
isStrikeThrough,
pdfFontName,
pdfEncoding,
isPdfEmbedded

parameter Object references used in parameterDescription, name, class


generating a report. defaultValueExpression
Referenced with
P${name}

queryString Specifies the SQL query - -


for retrieving data from a
database.

field Database table columns fieldDescription name, class


included in report.
Referenced with
F${name}

variable Variable used in the variableExpression, name,class.


report XML file. initialValueExpression
Referenced with
V${name}
title Report title band -

pageHeader Page Header band -

columnHeader Specifies the different band -


columns in the report
generated.

detail Specifies the column band -


values

columnFooter Column footer band -

A report section is represented with the band element. A band element


includes staticTextand textElement elements. A staticText element is used to add static
text to a report (for example, column headers) and a textElement element is used to add
dynamically generated text to a report (for example, column values retrieved from a
database table). We won't be using all or even most of these element and attributes.
Specify the page width with the pageWidth attribute in the root element jasperReport.
Specify the report fonts using the reportFont element. The reportElement elements
specify the ARIAL_NORMAL, ARIAL_BOLD, and ARIAL_ITALIC fonts used in the
report. Specify a ReportTitle parameter using the parameter element. The queryString
of the example JasperReports configuration XML filecatalog.xml specifies the SQL
query to retrieve the data for the report.

<queryString><![CDATA[SELECT CatalogId, Journal,


Publisher, Edition, Title,
Author FROM OE.Catalog]]> </queryString>

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:

<?xml version='1.0' encoding='utf-8'?>


<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//
EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="PDFReport" pageWidth="975">

The following code snippet specifies the report fonts:

<reportFont name="Arial_Normal" isDefault="true" fontName="Arial"


size="15" isBold="false" isItalic="false" isUnderline="false"
isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252"
isPdfEmbedded="false"/>
<reportFont name="Arial_Bold" isDefault="false" fontName="Arial"
size="15" isBold="true" isItalic="false" isUnderline="false"
isStrikeThrough="false" pdfFontName="Helvetica-Bold"
pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<reportFont name="Arial_Italic" isDefault="false" fontName="Arial"
size="12" isBold="false" isItalic="true" isUnderline="false"
isStrikeThrough="false" pdfFontName="Helvetica-Oblique"
pdfEncoding="Cp1252" isPdfEmbedded="false"/>

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.

<parameter name="ReportTitle" class="java.lang.String"/>


<queryString><![CDATA[SELECT CatalogId, Journal, Publisher, Edition,
Title, Author FROM Catalog]]></queryString>
<field name="CatalogId" class="java.lang.String"/>
<field name="Journal" class="java.lang.String"/>
<field name="Publisher" class="java.lang.String"/>
<field name="Edition" class="java.lang.String"/>
<field name="Title" class="java.lang.String"/>
<field name="Author" class="java.lang.String"/>

Add the report title to the report as follows:

<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>

Add the column's header as follows:

<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:

>jar cf config.jar config.xml

Add the config.jar file to the \\user_projects\domains\base_domain\lib directory,


which is in the classpath of the server.

Creating a web application


We shall be creating a web application for generating JasperReports reports and
deploying the web application to WebLogic Server. For the web application, we need to
create the JSPsCatalogPDFReport.jsp (for the PDF report)
and CatalogExcelReport.jsp (for the Excel report).

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:

<%@ page import="java.io.*,


java.util.*,
java.sql.Connection,
javax.sql.DataSource,
javax.naming.InitialContext,
net.sf.jasperreports.engine.*, net.sf.jasperreports.engine.design.
JasperDesign,
net.sf.jasperreports.engine.xml.JRXmlLoader,
net.sf.jasperreports.engine.export.*"
%>
Creating a report design
In this section, we shall create an InputStream for the JasperReports configuration
fileconfig.xml and load the XML file:

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.

JasperReport report = JasperCompileManager.compileReport(design);

Setting the report title


We need to set a value for the report title using the ReportTitle parameter. Create
a HashMapfor parameters and set the parameter value using the put method as follows:

Map parameters = new HashMap();


parameters.put("ReportTitle", "PDF JasperReport");

Creating a JDBC connection


Now, obtain a JDBC connection to retrieve data from the database, in order to create
your PDF/Excel report. First, create an InitialContext object. Subsequently, look up the
WebLogic Server data source that we created earlier using the JNDI lookup. Obtain
a Connection object using the getConnection method.

InitialContext initialContext = new InitialContext();


DataSource ds = (DataSource)initialContext.lookup("jdbc/OracleDS");
Connection conn = ds.getConnection();

The database data binding to the report fields gets applied in the report configuration
file config.xml, which we created earlier.

Generating a PDF report


To view the report, generate a JasperPrint document, which may be viewed, printed, or
exported to other formats, from the compiled report design.
JasperPrint print = JasperFillManager.fillReport(report,
parameters, conn);

The Map parameters in the fillReport(JasperReport,Map,Connection) method consist


of the parameter values for the parameters specified in the parameter element of the
XML configuration file. The conn argument is the JDBC connection we generated in the
previous section. A JasperReports report may be exported to an XML file, a PDF file, an
HTML file, a CSV file, or an Excel XLS file; to export the JasperReports report we've just
generated to a PDF file, use the exportReportToPdfStream method of
the JasperExportManager class as follows:

OutputStream output=new FileOutputStream(new File("C InputStream


input=this.getClass().getClassLoader().getResourceAsStream("config.
xml");CatalogPDFReport.pdf"));
JasperExportManager.exportReportToPdfStream(print, output);

The completed CatalogPDFReport.jsp JSP is listed as follows:

<%@ page contentType="text/html;charset=windows-1252"%>


<%@ page
import="java.io.*,java.util.*,java.sql.Connection,javax.sql.
DataSource,javax.naming.InitialContext,net.sf.jasperreports.
engine.*,net.sf.jasperreports.engine.design.JasperDesign,net.
sf.jasperreports.engine.xml.JRXmlLoader"%>
<%
InputStream input = this.getClass().getClassLoader()
.getResourceAsStream("config.xml");
JasperDesign design = JRXmlLoader.load(input);
JasperReport report = JasperCompileManager.compileReport(design);
Map parameters = new HashMap();
parameters.put("ReportTitle", "PDF JasperReport");
InitialContext initialContext = new InitialContext();
DataSource ds = (DataSource) initialContext.lookup("jdbc/OracleDS");
Connection conn = ds.getConnection();
JasperPrint print = JasperFillManager.fillReport(report,
parameters, conn);
OutputStream output = new FileOutputStream(new File(
"catalogPDFReport.pdf"));
JasperExportManager.exportReportToPdfStream(print, output);
%>

Creating an Excel report


Similarly, create the CatalogExcelReport.jsp file for the Excel report. Create
an InputStreamfor config.xml, create a JasperDesign object, compile the design to
create a JasperReport, set the ReportTitle parameter value, create an InitialContext,
look up the WebLogic Server data source, obtain a Connection object, and create
a JasperPrint with the same procedure as for the PDF report. Create
a ByteArrayOutputStram for the catalogExcelReport.xls report as follows:
OutputStream ouputStream=new FileOutputStream(new
File("catalogExcelReport.xls"));
ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();

Create a JRXlsExporter object and set the parameter values for


the JASPER_PRINT andOUTPUT_STREAM parameters as follows:

JRXlsExporter exporterXLS = new JRXlsExporter();


exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, print);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,
byteArrayOutputStream);

Export the Excel report using the exportReport method as follows:

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();

The CatalogExcelReport.jsp is listed as follows:

<%@ page contentType="text/html;charset=windows-1252"%>


<%@ page
import="java.io.*,java.util.*,java.sql.Connection,javax.sql.
DataSource,javax.naming.InitialContext,net.sf.jasperreports.
engine.*,net.sf.jasperreports.engine.design.JasperDesign,net.
sf.jasperreports.engine.xml.JRXmlLoader,net.sf.jasperreports.engine.
export.*"%>
<%
InputStream input = this.getClass().getClassLoader()
.getResourceAsStream("config.xml");
JasperDesign design = JRXmlLoader.load(input);
JasperReport report = JasperCompileManager.compileReport(design);
Map parameters = new HashMap();
parameters.put("ReportTitle", "Excel JasperReport");
InitialContext initialContext = new InitialContext();
DataSource ds = (DataSource) initialContext.lookup("jdbc/OracleDS");
Connection conn = ds.getConnection();
JasperPrint print = JasperFillManager.fillReport(report,
parameters, conn);
OutputStream ouputStream = new FileOutputStream(new File(
"catalogExcelReport.xls"));
ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS
.setParameter(JRXlsExporterParameter.JASPER_PRINT, print);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,
byteArrayOutputStream);
exporterXLS.exportReport();
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.

Packaging and deploying the web application


In this section, we package the web application and deploy the web application to
WebLogic Server using a build.xml file. Create an XML file build.xml in the project
folder JasperReports. In the build.xml file, define the properties listed in following
table:

Property Description Value

web.module The web module folder ${basedir}/WebContent

weblogic.home WebLogic home directory C:\Oracle\Middleware

weblogic.server WebLogic Server directory ${weblogic.home}/wlserver_12.1/server

build.dir Build directory ${basedir}/build

weblogic.domain.dir WebLogic base domain ${weblogic.home}/user_projects/


directory
domains/base_domain

deploy.dir WebLogic server deploy ${weblogic.domain.dir}/autodeploy


directory
Define a path element to include all the JAR files in the WebLogic modules directory,
WebLogic server lib directory, and WebLogic domain lib directory. Add the following
tasks to build.xml:

Target Description

prepare Make the required directories

war Package the JSPs, catalog.xml, WEB-INF/web.xml into


WAR file.

deploy Deploy the WAR file to WebLogic server

clean Delete the generated artifacts

The build.xml file is listed as follows:

<?xml version="1.0" encoding="UTF-8"?>


<!--
WebLogic build file
-->
<project name="JasperReports" default="deploy" basedir=".">
<property name="web.module" value="${basedir}/WebContent" />
<property name="weblogic.home" value=" C:/Oracle/Middleware/" />
<property name="weblogic.server" value="${weblogic.home}/
wlserver_12.1/server" />
<property name="build.dir" value="${basedir}/build" />
<property name="weblogic.domain.dir"
value="${weblogic.home}/user_projects/domains/base_domain" />
<property name="deploy.dir"
value="${weblogic.domain.dir}/autodeploy" />
<path id="classpath">
<fileset dir="${weblogic.home}/modules">
<include name="*.jar" />
</fileset>
<fileset dir="${weblogic.server}/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${weblogic.domain.dir}/lib">
<include name="*.jar" />
</fileset>
<pathelement location="${build.dir}" />
</path>
<property name="build.classpath" refid="classpath" />
<target name="prepare">
<mkdir dir="${build.dir}" />
</target>
<target name="war" depends="prepare">
<war warfile="${build.dir}/jasperreports.war">
<fileset dir="WebContent">
<include name="*.jsp" />
</fileset>
<fileset dir="WebContent">
<include name="catalog.xml" />
</fileset>
<fileset dir="WebContent">
<include name="WEB-INF/web.xml" />
</fileset>
<fileset dir="WebContent">
<include name="WEB-INF/lib/*.jar" />
</fileset>
</war>
</target>
<target name="deploy" depends="war">
<copy file="${build.dir}/jasperreports.war" todir="${deploy.dir}"
/>
</target>
<target name="clean">
<delete file="${build.dir}/jasperreports.war" />
</target>
</project>

The directory structure of the project is shown as follows:


Right-click on build.xml and in Run As select 3 Ant build… as shown in the following
screenshot:
Select the deploy target and click on Run.
As the targets have dependency on preceding targets, all the targets except the clean
target get run.

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:

Running the web application


Run the CatalogPDFReport.jsp with the URL http://loclahost:7001/
jasperreports/CatalogPDFReport.jsp. A PDF report gets generated. The PDF report
catalogPDFReport.pdf gets generated in the \\user_projects\domains\ base_domain
directory

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.

You might also like