Lesson 5: Deploying JReport with Client/Server

There are three methods for you to view your reports via Internet or Intranet.

All these methods are discussed in this tutorial.

Using JReport Client/Server

JReport's Client/Server system allows you to view reports via Internet or Intranet.  In this configuration, JReport Viewer and JReport Server typically run as separate programs on different computers connected in a network.   (Note that however, you can also run them on the same computer during testing of your reports.)  The Viewer sends out a request to the Server.  The Server will run the report and then send the report results that are requested by the Viewer. 

Setting up the Server

Step 1: Install the Server onto a machine from the setup file, jrssetup.exe.  An evaluation version can be freely downloaded from the Jinfonet Web site.  Next, use the ODBC-JDBC to connect your database data source.  Refer to Lesson 1 for further detail.


Step 2:  Double click on the JRServer.bat in the \JRServer\bin subdirectory (assuming that is the place where you installed JReport Server) to start up the server.  The following window should appear: 

l5f1.gif (7277 bytes)

JReport Server will start up automatically with default settings as you can see above. For this sexample, the default port is 8888 and the root directory is the directory where the default document index.html is located.  Check the box to enable directory browsing.  This will allow the client to browse the server directory to find and view a particular report. If you want to change these settings, you must first stop the server by clicking the Stop Server button.  When the server is stopped, the button label will change to Restart Server.  After you make the changes you simply click the button to restart JReport Server to run with the new settings.

Using the Viewer

Step 1: Setup the Viewer on a separate machine by running the setup file, jrvsetup.exe.   If you do not have a separate machine during testing, you can also setup the Viewer on the same machine the Server is running on.

Step 2: Double click JRViewer.bat in the \JRViewer\bin subdirectory to start up the viewer.  The following window should appear. 

L4f2.gif (2913 bytes)

In the JReport Server Location dialog box, enter the URL and port number for the JReport Server.  For example, if you are testing the JReport Server
on a local machine called 'p06_qiuzhf', you will type 'p06_qiuzhf' in the JReport Server Name field and if the port is '8888', type '8888' in Port field.  Click on the OK.

Step 3: The JReport Viewer can be used to run a report stored on the JReport Server (Remote mode) or to view an indexed report result file cached on the local computer (Local mode).  When you clicked the OK button, the Viewer will enter the Remote mode and the following window should appear:

l5f3.gif (7216 bytes)

The left menu displays which catalog files are available to you on the server.  The right menu shows which reports are listed under the catalog file selected.  To view a report from the JReport Viewer, select a report and click on the Open button.  You can also view a report by selecting a report and clicking File/Open and then Remote.  

Sometimes it is desirable to save a report for future use without needing to run it again.  Any time when you view a report using the JReport Viewer you can save it as a local file by clicking File/Save As...  The report will be saved as an indexed report result file.  Once a report is saved, you can use File/Open/Local to open and view this indexed result file instead of running it on the  JReport Server again. 

For this example, we selected the invoice report from the Tutorial catalog on the Server.  Thus we are requesting the Server to run the invoice report Remotely. 

Step 4: Once the result is available, the following window will appear.

L4f4.gif (7544 bytes)

You can use the arrow buttons or the numbers to scroll through the reports.   The Refresh button will reload the reports within a catalog file in case there are changes be made.    


The JReport Viewer Bean

This section is intended only for Java developers. 

In order to make the JReport Viewer easier to be called from user's Java application, the Viewer program is also provided as a Java Bean.  The JReport Viewer Bean can send a request from a client workstation to the Server and instruct the Server to run a report and return results to the Viewer.

Setting up the JReport Viewer Bean

The first thing you need to do is download the jvbean.zip file from Jinfonet web site.  Inside the jvbean.zip file, there are 5 files.  The Sample1.java and Sample2.java files are examples on how to run the JReport Viewer Bean.  Do not just compile and run these files since they contain constants that need to be adjusted for your installed environment.

The file JRViewer.zip should be used when compiling your program which will call the JReport View Bean.  The JRViewer.zip file should also be used when you run your application which calls JReport View Bean. 

There are three methods to invoke the JReport View Bean, depending on how much control your application requires:

  1. One Step Viewer - Your application calls the Viewer, which will get report parameters from the user through a pop-up dialog.
  2. Two Step Viewer - Your application calls the Viewer, which returns the number of parameters needed.  Your program then calls Viewer again and provide it with the required parameters.
  3. Second Step Viewer - Your application already knows the parameters and calls the Viewer directly in one step.

If you know which method you will use at run time, instead of using the JRViewer.zip file, you can use one of the other two (smaller) jar files (OneStepJRViewer.jar and TwoStepsJRViewer.jar).  The following sections show how to use each of the two methods.

Please note that regardless which method you use, getStatus () should be called each time after calling the Viewer Java beans.  If the return string is "OK", the request is accepted, otherwise an error message is returned.


The First Method: One Step JReport Viewer

In this first method, the user's Java applications will call the Java bean JRDirectViewer and provide the following properties:

  1. setServerName ("theSevrerName");
  2. setPort ("thePortNumber");
  3. setReportName ("theReportName");
  4. setCatName ("theCatalogName");
  5. setLoadReport ("Yes/No");

After the Java bean JRDirectViewer is loaded, it will ask the JReport Server to run the report and pop up a dialog on the screen to ask the user to input the report parameter, if needed.  This method of invoking the JReport Viewer Bean has the advantage that it does not require the user's Java application to provide the report parameters.  The disadvantage is that it will display a dialog on the screen to ask user to input the report parameters.

After the Viewer have successfully received the report results, you may want to have them printed.  There are also two methods available for printing reports.   They are:

  1. printReport ()
  2. getPrintStatus ()

Printing is initiated by calling the method printReport ().   While the report is being printed, you can call the getPrintStatus () method to get the current status of the print job.  The return values of the getPrintStatus () are:

  1. "No Print Job"
  2. "Busy"
  3. "OK"
  4. Other error messages if there is an error.

Please note that these two printing methods are also available to the second method of calling JReport Viewer Bean described below.


The Second Method: Two Steps JReport Viewer

The second method of invoking the JReport Viewer Bean is that the user's Java application first calls a Java bean named JRPrepareViewer with the following properties:

  1. setServerName ("theSevrerName")
  2. setPort ("thePortNumber")
  3. setReportName ("theReportName")
  4. setCatName ("theCatalogName")
  5. setLoadReport ("Yes/No")

The JRPrepareViewer Java Bean will return a vector which contains the information about the report. The user's Java application should prepare the parameters based on the information provided by the vector.  The data type of the parameters may be obtained by calling the method getReportParams () which returns a vector. The size of the vector depends on the number of the parameters needed by the report.  Each element in the vector contains the type of one parameter. You can use getType () method to get it.  The following are the types of parameters available:

  1. STRING:
  2. NUMBER:
  3. CURRENCY:
  4. BOOLEAN:
  5. DATE:
  6. TIME:
  7. DATETIME:

After the parameters are prepared, you will then call the second Java bean named JRRunViewer with the following properties:

  1. setServerName ("theSevrerName")
  2. setPort ("thePortNumber")
  3. setReportName ("theReportName")
  4. setCatName ("theCatalogName")
  5. setLoadReport ("Yes/No")
  6. setParameters ("Parameters")

In the second method of invoking the JReport Viewer Bean, our bean will not display any dialog on the screen.  However, the user's Java application has to provide all the necessary information and retain full control to the screen.  We note that there is a lot of redundant parameters in the two calls.  This will be improved in a future version.

The Third Method: Second Step JReport Viewer

This method is identical to the second step of the Two Step method above.   After the parameters are prepared, you simply call the second step of the second Java bean named JRRunViewer with the required properties:

  1. setServerName ("theSevrerName")
  2. setPort ("thePortNumber")
  3. setReportName ("theReportName")
  4. setCatName ("theCatalogName")
  5. setLoadReport ("Yes/No")
  6. setParameters ("Parameters")

The example program Sample1 and  Sample2 illustrate how to call JReport Viewer Beans.  Please refer to it for further details.


Using Browser to View Reports as Applets

There are two ways to use a web browser to view the reports you created.  The first way is to generate the report in HTML format, and the second is to use the JReport Viewer Applet.

If you use a browser to access reports stored in a JReport Server, you do not have to do anything special.  When the browser is connected to the JReport Server using the Server URL, a list of reports will be displayed.

l5f5.gif (14263 bytes)

Choose the report you are interested in and select view in Applet format (the only format supported by the simple server version), and click on the report.  The server will generate a small HTML page with an embedded call to the JReport Viewer Applet,  then send the page to your browser.  The small HTML page will have the following contents:

<html>
<
head>
<
title>Viewer Applet</title>
<
/head>
<
body>
<
p>Please wait for loading applet of JReport Viewer Frame ... </p>
<
hr>
<
p>
<
applet code="jet.thinviewer.ViewerApplet.class" codebase="http://p03_zhangk:8888
"

archive="viewer.jar" width="0" height="0">
<
param name="reportName" value="http://p03_zhangk:8888/invoice.rst">
<
/applet>
<
/p>
<
p>JReport Viewer is running ... </p>
<
hr>
<
/body>
<
/html>

The invoice.rst is the name of the report result file which will be interpreted by the JReport Viewer Applet.  When the Applet is running, the following two windows should be displayed on your screen.

l5f6.gif (9815 bytes)

l5f7.gif (8213 bytes)

 

Publishing Reports in Your HTML Web Pages

Another way to put JReport reports to your web pages is to have your web page request a specific report from JReport Server.  In this way, you can get a fresh report whenever the page is accessed.  This is achieved by making reference to an URL with the following format:

JRServerName:PortNumber/ReportPath/FileName.cls?catalog=CatalogName&report=ReportName&result_type=Type&PN=P V

where:

JRServerName: The name of the JReport Server.
PortNumber: The port number used for the server.
ReportPath: The path where the reports reside on the server.
FileName.cls: The name of the file containing the report description.  Do not forget to specify the file name extension.  Normally report file name has .cls as the extension.
CatalogName: The name of the catalog which contains all the objects (resource) used in the report.
ReportName: The name of the report.
<Type: The report result type.  0 is for Applet and 1 is for HTML.
PN: The name of a parameter used in the report.
PV: The value you are assigning to the parameter
If you have more than one parameter, you can just assign the value to the parameter one by one.  The format is:

PN1=PV1&PN2=PV2&PN3=PV3

Here is an example of the URL which is generated by JReport server when you use a browser to access a report on the server:

http://p03_zhangk:8888/jreports/tutorial/invoice.cls?catalog=Tutorial&
report=invoice&result_type=0&PTODAY=1998-05-21&Submit_Btn=Submit

Remember, the above string should be in one single line.  The last part of the string Submit_Btn=Submit could be ignored.