Crystal Reports® for Borland® JBuilder® Tutorial - Embedding the Java Viewer into your JSP Pages
Overview
This document contains a series of tutorials that demonstrates how to use the
Crystal Reports Java Viewer SDK provided with Borland® JBuilder X. The
tutorials assume that you are familiar with JSP programming. Since each tutorial
builds on the previous ones, it is recommended that you work through the series
sequentially.
The first tutorial shows you how to create a report source programmatically. A
report source is an object that represents a single instance of a report.  You must
be able to create a report source before you can a) use the viewer to embed a
Crystal Report inside your JSP page, or b) use the export control to export a
Crystal Report to a variety of file formats – including PDF and RTF. The
remaining tutorials then show you how to use the report source in conjunction
with the export control and the viewer.
Contents
H OW TO C REATE A R EPORT S OURCE (2)
H OW TO U SE THE J AVA V IEWER (3)
Creating and initializing a Java viewer (3)
Launching the viewer (3)
Performing garbage collection (4)
H OW TO EXPORT A REPORT (5)
Creating and initializing an Export Control (5)
Exporting the report (6)
Performing garbage collection (7)
H OW TO SET A PARAMETER FIELD (8)
Creating and initializing parameter fields (8)
Setting parameter fields (9)
H OW TO SET A DATABASE LOGON (11)
Creating and initializing database logon information (11)
Setting database logon information (12)
A PPENDIX A (14)
Report sources (14)
Java Reporting Component (14)
Java Reporting Component configuration (15)
How to Create a Report Source
Before you can use the export control or the viewer in your JSP pages, you must
obtain a report source.
Other report sources include the Crystal Report Application Server (RAS) and
the Crystal Enterprise Page Server.  The RAS report source is used when it is
desired to remote the report processing aspect of your application, or when
programmatic report modification at runtime is required.  The Crystal Enterprise
Page Server is also used to remote the report processing function, but in
addition, it provides a host of other services commonly associated with an
enterprise-wide reporting system.  These services include:
• Staggering the execution of reports to minimize system load, commonly
referred to as scheduling
• Archiving snapshots, or instances, of processed reports for auditing or time
series analysis
• Zero-client ad-hoc analysis
• Maintaining a repository of report objects which can be reused or
centralized for single-point update
• Managing an enterprise-wide, distributed, clustered, fail-safe reporting
infrastructure
Creating a Java Reporting Component report source only requires you to have
the location of the report you wish to view or export. The report location can be
specified using a file system path, UNC address, or URL. For more information
on the Java Reporting Component report source and specifying paths, see
“Appendix A”. Also, for the Java Reporting Component to correctly retrieve
data for a report, the report’s data sources must be correctly specified through a
JDBC connection string, or, if desired, a Java Naming and Directory Interface
(JNDI) connection.
NOTE The Java Reporting Component uses one of two mechanisms to establish connections to
data sources.  At report design-time (i.e. when reports are designed using the desktop
visual report design environment) a report designer can establish a connection to a data
source via a hard-coded JDBC connection string or via a JNDI look-up.  The JNDI lookup
has the advantage of employing indirection to delay data source binding.  At runtime,
when a report template is processed by the Java Reporting Component, the hand-coded
JDBC connection string entered at design-time can be used, or it can be overridden in the
deployment descriptor (l).  The JNDI approach relies on a JNDI directory service
running on your application server to determine how to connect to the data sources
specified in a report. Once the connection information has been retrieved from the JNDI
server, the Java Reporting Component uses the information to establish a JDBC
connection to the data source. To ensure that the Java Reporting Component can
successfully establish a connection when retrieving report data, ensure that the JNDI
entries for the required data sources are correctly configured. For more information on
how to configure a JNDI data source entry, please consult your application server's and
JDBC driver's documentation.
1. Ensure that you have imported the JPEReportSourceFactory class and the
IReportSource and IReportSourceFactory2 interfaces.
<%@ page
import=”portengineinterface.JPEReportSourceFactor
y,
%>
2. Create a new JPEReportSourceFactory object.
IReportSourceFactory2 rptSrcFactory = new JPEReportSourceFactory();
3. Call the IReportSourceFactory2 object’s createReportSource method,
passing it the path to the desired report and the current locale settings. The
IReportSource object can now be used by the viewer or export control.
String report = “/reports/sample.rpt”;
IReportSource reportSource = (IReportSource)
How to Use the Java Viewer
This tutorial demonstrates how to use the Java viewer programmatically to
display a report in a web browser.
The tutorial contains the following sections:
• Creating and initializing a Java viewer
• Launching the viewer
• Performing garbage collection
Creating and initializing a Java viewer
The viewer is simply an instance of a CrystalReportViewer class. A
CrystalReportViewer object has many set methods that affect how it displays
reports in a web browser. Some of them need to be called before the viewer can
render a report. One of these methods is used to set the report source (recall
from above that the Java Reporting Component, the Report Application Server
and the Crystal Enterprise Page Server are all possible report sources) You need
to set the viewer’s report source to the appropriate report processing entity.
1. Instantiate a CrystalReportViewer object.
CrystalReportViewer viewer = new CrystalReportViewer();
2. Set the viewer’s report source by calling its setReportSource method,
passing it a reference to a report source object.
The viewer has now been created and initialized.
viewer.setReportSource(reportSource);
NOTE Once the viewer is created and initialized, you can set a variety of properties related to its
display characteristics, database logon handling, and parameter prompting. For more
information, see the CrystalReportViewer documentation in the Crystal Reports forjsp帮助文档
JBuilder Java API Reference provided in JBuilder X.
Launching the viewer
Once you have created and initialized a Java viewer, call its processHttpRequest
method to launch it in a web browser.
This example assumes that you have set up the viewer properly, and that you
have valid request, response, and session objects.
1. Call the processHttpRequest method to launch the viewer in the current
browser window.
viewer.processHttpRequest(request, response,
getServletConfig().getServletContext(), null);
NOTE If the viewer's content is going to be displayed more than once, using the getHtmlContent method is more efficient. This is because the request is processed only once and the
resulting HTML string can be used multiple times.
Performing garbage collection
In order to ensure the maximum performance for your web applications, you
must ensure that the dispose method is called whenever you are finished with
the viewer. This ensures that any resources being tied up are freed for use by
other applications.
1. Call the viewer’s dispose method.
viewer.dispose();
Example
The following example is a simple JSP page that demonstrates how to use the
viewer to display a simple report.
viewreport.jsp
<%@ page import="port.web.viewer.CrystalReportViewer"
%>
<%@ page
import=”portengineinterface.JPEReportSourceFactor
y,
%>
<%
String report = “/reports/sample.rpt”;
rptSrcFactory = new JPEReportSourceFactory();
IReportSourceFactory2
IReportSource reportSource = (IReportSource)
CrystalReportViewer viewer = new CrystalReportViewer();
viewer.setReportSource(reportSource);
response,
viewer.processHttpRequest(request,
getServletConfig().getServletContext(), out);
viewer.dispose();
%>
How to export a report
This tutorial shows you how to use the Export Control to export a Crystal report
to RTF or PDF formats.
The tutorial contains the following sections:
• Creating and initializing an Export Control
• Exporting the report
• Performing garbage collection
• Example
Creating and initializing an Export Control
The export control handles all aspects of exporting the report. It allows you to
preview the exported report within the browser window or export it as an
attachment, prompting the user with a download dialog. An export control is an
instance of the ReportExportControl class.
To create an Export Control
1. Instantiate a ReportExportControl object.
ReportExportControl exportControl = new ReportExportControl();
Once you have instantiated the ReportExportControl object, you must specify
the export format that you want. For the purpose of this example, RTF has been
chosen as the export format.
To specify the export format
1. Create an ExportOptions object.
ExportOptions exportOptions = new ExportOptions();
2. Specify the export format by calling the ExportOptions object’s
setExportFormatType method, passing it a ReportExportFormat constant
representing the desired format.
exportOptions.setExportFormatType(ReportExportFormat.RTF);
NOTE    A list of the valid constants specifying export formats can be found in the
ReportExportFormat class documentation provided in JBuilder X.
Some formats contain additional options that can be configured to customize
how the report is exported. This includes control over what page range is
exported.
To configure format specific options
1. Create the appropriate format options object. In this case, because the
export format is RTF, a RTFWordExportFormatOptions object is created.
RTFWordExportFormatOptions RTFExpOpts = new
RTFWordExportFormatOptions();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。