The Java Viewer JSP Application


Many of the classes in the Java Viewer class collection need to identify the report that is to be displayed on the web page. The report is specified as the value of the reportlocation tag in the CrystalReportEngine-config.xml file, which you learned about in the previous section of this chapter. The content of the reportlocation tag is the location of the directory that contains the report. The reportlocation tag does not contain the name of the report. You identify the report by declaring a report source object that points to the Java Reporting Component. Your JSP application must implement the IReportSource interface. This interface contains methods needed to declare and use a report source object.

Caution  

Before attempting to declare a report source object, make sure that you have provided a value for the reportlocation tag and specified the data source in JNDI. The reportlocation identifies the relative path where the report may be found, and JNDI identifies the source of the data for the report.

The first step to declare a report source is to import the three classes into your JSP application. These are

  • JPEReportSourceFactory

  • .IReportSourceFactory2

  • .IReportSource

Here s how this is done:

 <%@ page import= 
"com.crystaldecisions.reports.reportengineinterface.JPEReportSourceFactory,
com.crystaldecisions.sdk.occa.report.reportsource.IReportSourceFactory2,
com.crystaldecisions.reports.reportengineinterface.IReportSource"
%>

Next, you need to declare an instance of the JPEReportSourceFactory. The JPEReportSourceFactory defines the createReportSource() method that is used to create a report source object. The createReportSource() method requires two arguments. The first argument is the name of the report, including the path. The second argument is the local setting, which is returned by the getLocale() method. The createReportSource() method returns a reference to the report source, which is assigned to an IReportSource reference object. This is shown here:

 IReportSourceFactory2 rptSrcFactory = new JPEReportSourceFactory(); 
IReportSource rptSource = (IReportSource)
rptSrcFactory.createReportSource("/reports/myReport.rpt", request.getLocale());

In the createReportSource method, the name of the report is passed relative to the reportlocation tag identified in the CrystalReportEngine-config.xml file. In this case, the Java Reporting Component will look to the reportlocation tag and then navigate to the reports folder contained at this location to find the myReport.rpt file.

Next, you ll need to create and initialize a Java Viewer. You do this by declaring a CrystalReportViewer object, as shown here:

 CrystalReportViewer javaViewer = new CrystalReportViewer(); 

You need to associate the Java Viewer with the report that you want the Java Viewer to display in the web page that is generated by your Java application. You create this association by calling the setReportSource() method that is defined in the CrystalReportViewer class. The setReportSource() method requires one argument, which is the IReportSource object that is returned by the createReportSource() method. Here s how to set the report source for the Java Viewer:

 javaViewer.setReportSource(rptSource); 

Next, you need to launch the Java Viewer. You do this by calling the processHttpRequest() method, which launches the Java Viewer in the current browser window. The processHttpRequest() method requires four arguments. The first argument is the javax.servlet.http.HttpServletRequest object ( ˜request in all JSP pages by default). The second argument is the javax.servlet.http.HttpServletResponse object ( ˜response in all JSP pages by default). The third argument is a reference to the Java servlet context, which is returned by the getServletConfig() and getServletContext() methods. The fourth argument is the java.io.Writer object (you can use the default ˜out variable or pass a null value), which is set to null. The call to the processHttpRequest() method is shown here:

 viewer.processHttpRequest(request, response, getServletConfig().getServletContext(), null); 

Be forewarned that displaying a Crystal report can tie up resources. Therefore, it is important to manage those resources carefully within your JSP application. The easiest way to do this is to perform your own brand of garbage collection after the report is displayed. Garbage collection is a simple process to perform and will free resources that are no longer needed by the Java Viewer to display the report. You perform garbage collection by calling the dispose() method that is defined in the CrystalReportViewer class. Here s the call you need to make in your JSP application:

 JavaViewer.dispose() 

So, let s put together the concepts covered so far to create a JSP application that displays a Crystal report on the screen. The following example illustrates a JSP application that displays the report myReport that is located in the report directory:

 <%@ page import="com.crystaldecisions.report.web.viewer.CrystalReportViewer" %> 
<%@ page import=
"com.crystaldecisions.reports.reportengineinterface.JPEReportSourceFactory,
com.crystaldecisions.sdk.occa.report.reportsource.IReportSourceFactory2,
com.crystaldecisions.reports.reportengineinterface.IReportSource" %>
<%
IReportSourceFactory2 rptSrcFactory = new JPEReportSourceFactory();
IReportSource rptSource = (IReportSource) rptSrcFactory.
createReportSource("/reports/myReport.rpt", request.getLocale());
CrystalReportViewer javaViewer = new CrystalReportViewer();
javaViewer.setReportSource(rptSource);
javaViewer.processHttpRequest(request, response, getServletConfig().
getServletContext(), out);
javaViewer.dispose();
%>



Crystal Reports 10
Crystal Reports 10: The Complete Reference
ISBN: B005DI80VA
EAN: N/A
Year: 2004
Pages: 223
Authors: George Peck

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net