Developing a Web Project for JasperReports

Exclusive offer: get 50% off this eBook here
Java EE Development with Eclipse

Java EE Development with Eclipse — Save 50%

Develop Java EE applications with Eclipse and commonly used technologies and frameworks with this book and ebook.

$32.99    $16.50
by Deepak Vohra | May 2013 | Enterprise Articles Java

Eclipse IDE for Java EE provides project facets for various types of projects. In this article, we discuss a more generic project facet for web applications called the Dynamic Web Module project facet that adds support for Java Servlet API to build web applications. To demonstrate the use of the Dynamic Web Project we shall be using JasperReports as an example. Preparing presentation-quality reports is a routine occurrence, and several tools & APIs for creating reports are available. For Java developers, one of the most commonly used API is JasperSoft’s open source JasperReports, which is capable of producing a range of outputs, including HTML, PDF, presentations from either static data or data retrieved form a database table using an SQL query, and is designed to be integrated directly into Java/J2EE applications.

In this article by Deepak Vohra author of Java EE Development with Eclipse, we sets out to demonstrate the essentials of how JasperReports can be used in a web application created with Eclipse. The examples created in this article are a PDF report and an Excel report. The article has the following sections.

  • Setting the environment

  • Creating a Dynamic Web project in Eclipse

  • Creating the configuration file

  • Creating a web application

    • Creating a Report Design

    • Setting Report Title

    • Creating a JDBC Connection

    • Generating a PDF Report

    • Creating an Excel Report

  • Packaging and deploying the web application

  • Running the web application

(For more resources related to this topic, see here.)

Setting the environment

First, we need to install the required software, Oracle Enterprise Pack for Eclipse 12c, from http://www.oracle.com/technetwork/middleware/ias/ downloads/wls-main-097127.html using Installers with Oracle WebLogic Server, Oracle Coherence and Oracle Enterprise Pack for Eclipse, and download the Oracle Database 11g Express Edition from http://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,
  • 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

Apache Jakarta POI  classes and dependencies.

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 the Target 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, select Project 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 on Next. 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, queryString, field, variable, group, title, pageHeader, columnHeader, detail, columnFooter, pageFooter.

name, columnCount, pageWidth, pageHeight, orientation, columnWidth, columnSpacing, leftMargin, rightMargin, topMargin, bottomMargin.

reportFont

Report level font definitions

-

name, isDefault, fontName, size, isBold, isItalic, isUnderline, isStrikeThrough, pdfFontName, pdfEncoding, isPdfEmbedded

parameter

Object references used in generating a report. Referenced with P${name}

parameterDescription, defaultValueExpression

name, class

queryString

Specifies the SQL query for retrieving data from a database.

-

-

field

Database table columns included in report. Referenced with F${name}

fieldDescription

name, class

variable

Variable used in the report XML file. Referenced with V${name}

variableExpression, initialValueExpression

name,class.

title

Report title

band

-

pageHeader

Page Header

band

-

columnHeader

Specifies the different columns in the report generated.

band

-

detail

Specifies the column values

band

-

columnFooter

Column footer

band

-

A report section is represented with the band element. A band element includes staticText and 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 file catalog.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.

Java EE Development with Eclipse Develop Java EE applications with Eclipse and commonly used technologies and frameworks with this book and ebook.
Published: December 2012
eBook Price: $32.99
Book Price: $54.99
See more
Select your format and quantity:

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 JSPs CatalogPDFReport.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 a Connection 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 file config.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 a JasperReport 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 HashMap for 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 InputStream for 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 and OUTPUT_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 the OutputStream 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 directory

${weblogic.home}/user_projects/

domains/base_domain

deploy.dir

WebLogic server deploy directory

${weblogic.domain.dir}/autodeploy

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

Resources for Article :


Further resources on this subject:


Java EE Development with Eclipse Develop Java EE applications with Eclipse and commonly used technologies and frameworks with this book and ebook.
Published: December 2012
eBook Price: $32.99
Book Price: $54.99
See more
Select your format and quantity:

About the Author :


Deepak Vohra

Deepak Vohra is a consultant and a principal member of the NuBean.com software company. Deepak is a Sun Certified Java Programmer and Web Component Developer, and has worked in the fields of XML and Java programming and J2EE for over five years. Deepak is the co-author of the Apress book Pro XML Development with Java Technology and was the technical reviewer for the O'Reilly book WebLogic: The Definitive Guide. Deepak was also the technical reviewer for the Course Technology PTR book Ruby Programming for the Absolute Beginner, and the technical editor for the Manning Publications book Prototype and Scriptaculous in Action. Deepak is also the author of the Packt Publishing books JDBC 4.0 and Oracle JDeveloper for J2EE Development; Processing XML documents with Oracle JDeveloper 11g; EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g; and Java 7 JAX-WS Web Services.

Books From Packt


Java EE 5 Development with NetBeans 6
Java EE 5 Development with NetBeans 6

Java EE 5 Development using GlassFish Application Server
Java EE 5 Development using GlassFish Application Server

Java EE 6 with GlassFish 3 Application Server
Java EE 6 with GlassFish 3 Application Server

Java EE 6 Development with NetBeans 7
Java EE 6 Development with NetBeans 7

Java EE 6 Cookbook for Securing, Tuning, and Extending Enterprise Applications
Java EE 6 Cookbook for Securing, Tuning, and Extending Enterprise Applications

JBoss AS7 Development
JBoss AS7 Development

JSF 1.2 Components
JSF 1.2 Components

NetBeans IDE 7 Cookbook
NetBeans IDE 7 Cookbook


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
G
2
N
s
K
Y
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software