5.) Servlets and Java Server Pages

5.1.) Servlets

5.1.1.) Introduction

As described on the Sun MicrosystemsTM web site (http://java.sun.com), "Java Servlets provide web developers with a simple, consistent mechanism for extending the functionality of a web server and for accessing existing business systems.  A servlet can almost be thought of as an applet that runs on the server side -- without a face.  Java Servlets have made many web applications possible.

Today, servlets are a popular choice for building interactive web applications.  Servlet containers are available for Apache Web Server, iPlanet Web Server (formerly Netscape Enterprise Server), Microsoft IIS, and others.  Servlet containers can also be integrated with web-enabled application servers, such as BEA WebLogic Application Server, IBM WebSphere, Netscape Application Server, and others."

The sections below describe how to set up an example servlet provided with EspressReport (located in EspressReport/help/examples/servlet/StreamingReport directory).  Please note that each section deals specifically with the example servlet. They can however be used as a guide for setting up and running your own servlets as well as the other example servlets given.

In addition to this servlet, other servlet examples have also been provided.  They range from the simple showing of a report (either in HTML or DHTML format) to the streaming of a report containing charts directly to the browser.  Setup and running of these examples can be done using the following sections as a guide.

5.1.2.) Setup

Note that even though the instructions refer to the Windows platforms, they can be used for Unix/Linux platforms as well.  Changing the file names and path to conform to the Unix/Linux standard is the only necessary step.

The examples all use single threaded model for simplicity's sake.  However, EspressReport API can also be used in a multi-threaded environment also.

The following uses the StreamingReport servlet example under EspressReport/help/examples/servlet.

5.1.3.)Running Under

5.1.3.1.) Apache Tomcat 5.5.20 or 6.0.10

  1. Please, be sure, that you have Tomcat server in version 5.5.20 or 6.0.10 installed.
    (Probably this guide is also applicable for previous versions of Tomcat server.)

    There are two installation file versions for Windows operating system (for every version) of ApacheTomcat server:

  2. Classpath – You should include ReportDesigner.jar and servlet-api.jar files in the classpath.

    1. ReportDesigner.jar file can be found in EspressReport/lib directory

    2. servlet-api.jar can be found in Tomcat installation directory
      (apache-tomcat-5.5.20\common\lib\ directory if you use zip installation file;
      C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\ directory as the default Tomcat installation directory when using exe installation file)

    Here is the example classpath setting (using command line console):

    set classpath=%classpath%;C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar;C:\EspressReport\lib\ReportDesigner.jar

    (when using Tomcat 5.5 zip installation file)

    set classpath=%classpath%;C:\apache-tomcat-5.5.20\common\lib\servlet-api.jar;C:\EspressReport\lib\ReportDesigner.jar

    (when using Tomcat 5.5 installer - exe installation file)

    Note: For Tomcat 6.0 version, please use following:

    set classpath=%classpath%;C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;C:\EspressReport\lib\ReportDesigner.jar

    (when using Tomcat 6.0 zip installation file)

    set classpath=%classpath%;C:\apache-tomcat-6.0.10\lib\servlet-api.jar;C:\EspressReport\lib\ReportDesigner.jar

    (when using Tomcat 6.0 installer - exe installation file)

    Note: Classpath does not necessarily need to be set, but it simplifies “.java” files compilation
    (you don’t need to add classpath there).

    Note: You can set Classpath variable also using MyComputer -> Administrate -> Details tab window.


  3. Compiling the example file (streamingReportServlet.java)

    Compile command:

    javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar;C:\EspressReport\lib\ReportDesigner.jar" streamingReportServlet.java

    (for Tomcat 5.5 version)

    javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;C:\EspressReport\lib\ReportDesigner.jar" streamingReportServlet.java

    (for Tomcat 6.0 version)

    (if classpath environment variable was not set in step 2 of this guide)

    or simply:

    javac streamingReportServlet.java

    (if classpath was set properly)


  4. Copying to Tomcat’s webapps\ROOT\WEB-INF\classes directory

    Copy streamingReportServlet.class file to Tomcat’s webapps\ROOT\WEB-INF\classes directory.

    Note: Please note that webapps\ROOT\WEB-INF\classes directory does not need to exist by default and you will have to create it.


  5. Adding necessary libraries for running the servlet to lib dir

    In order to run the example, you also need to add libraries to Apache Tomcat’s webapps\ROOT\WEB-INF\lib directory. Please copy ReportDesigner.jar file placed in EspressReport\lib directory to webapps\ROOT\WEB-INF\lib directory.

    Note: Please note that webapps\ROOT\WEB-INF\lib directory does not need to exist by default and you will have to create it.


  6. Registering your servlet appplication in web.xml file of your Tomcat server.

    Before you can run your servlet application you need to modify web.xml file, that is placed in webapps\ROOT\WEB-INF\ directory of your Tomcat server.

    For registering streamingReportServlet, please add the following code:

      <servlet>
        <servlet-name>streamingReportServlet</servlet-name>
        <servlet-class>streamingReportServlet</servlet-class>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>streamingReportServlet</servlet-name>
        <url-pattern>/servlet/streamingReportServlet</url-pattern>
      </servlet-mapping>
    

    between <web-app> and </web-app> xml tags.

    Note: When using Tomcat in version 5.5.20, you can also enable automatic invoking of all servlets placed in webapp directory. Then you don’t need to register your servlet applications.
    This is intended only for test purposes. Using the invoker servlet in a production environment is not recommended and is unsupported.
    For more information consult documentation of your Tomcat server.


  7. Start (restart) the Tomcat server

    If you used zip installation file, please run startup.bat (startup.sh for Linux) file from bin directory of Tomcat installation directory.
    For exe installation files you need to startup the Tomcat service. You can use Tomcat’s “Monitor Tomcat” application for this purpose.

    Note: If your Tomcat server is already running, you probably need to restart it (shutdown and startup again) to make the example runnable.


  8. Running the example

    The last step is running the example. To run the example, please run index.html file placed in EspressReport\help\examples\servlet\StreamingReport directory in your browser.
    After clicking the GetReport button, the streamingReportServlet is called and appropriate report is generated.

    Note: Please be sure, that EspressManager is running before you run the example. If it is not running, empty report is generated.


Note that the servlet as designed and given will only work if both the servlet runner and the client browser are on the same machine.
To allow the client to be on a different machine, modify the line in Servlet.html file (placed in EspressReport\help\examples\servlet\StreamingReport directory )
from:

<form action="http://127.0.0.1:8080/servlet/streamingReportServlet" method="post">

to:

<form action="http://<machine_name>:8080/servlet/streamingReportServlet" method="post">

Note: If you use Automatic settings discovery in your web browser, there can be problem reaching 127.0.0.1 machine (according to your network settings.
Please use "localhost" instead.

<form action="http://localhost:8080/servlet/streamingReportServlet" method="post">


5.1.3.2.) JRun 3.1

5.1.3.3.) ColdFusion Server 4.5

5.1.3.4.) WebLogic 6.0

To set up and run the servlet example, first, go to the wlserver6.0\config\examples directory.  Modify both "setExamplesEnv.cmd” and “startExamplesServer.cmd” files according to the following instructions:

5.1.3.4.1.) WebLogic 9.2

The following instructions show how to set up and run the servlet example under WebLogic 9.2. The instructions assume that you have WebLogic 9.2 server installed on the system. The location of the WebLogic installation will be referenced as <WL_INSTALL_DIR> and the location of the EspressReport installation will be referenced as <ER_INSTALL_DIR>.

To set up and run the servlet example, firstly, you go to the <WL_INSTALL_DIR>/samples/domains/wl_server/bin directory. Modify the setExamplesEnv.cmd file according to the following instructions.

Next, follow the steps below (note that the files are under <ER_INSTALL_DIR>\help\examples\servlet\StreamingReport directory):

Note: For troubleshooting please check for typing errors.

5.1.3.5.) WebSphere 3.5

5.1.3.5.1.) WebSphere 6.1

The following instructions show how to set up and run the servlet example under WebSphere 6.1. The instructions assume that you have WebSphere 6.1 server installed on the system. The location of the WebSphere installation will be referenced as <WS_INSTALL_DIR> and the location of the EspressReport installation will be referenced as <ER_INSTALL_DIR>.

To set up and run the servlet example, firstly, copy <ER_INSTALL_DIR>\lib\ReportAPIWithChart.jar and <ER_INSTALL_DIR>\lib\qblicense.jar files to the <WS_INSTALL_DIR>\AppServer\lib directory.

Next, follow the steps below (note that the files are under <ER_INSTALL_DIR>\help\examples\servlet\StreamingReport directory):

Note: For troubleshooting please check for typing errors.

5.1.3.6.) WebSphere Express Server 5.0

5.1.3.7.) JBoss 4.0.5

The following instructions show how to set up and run the servlet example under JBoss 4.0.5. The instructions assume that you have JBoss 4.0.5 server installed on the system. The location of the JBoss 4.0.5 installation will be referenced as <JB_INSTALL_DIR> and the location of the EspressReport installation will be referenced as <ER_INSTALL_DIR>.

To set up and run the servlet example, firstly, you go to the <JB_INSTALL_DIR>\bin directory. Modify the run.bat file according to the following instructions.

Next, follow the steps below (note that the files are under <ER_INSTALL_DIR>\help\examples\servlet\StreamingReport directory):

Note: For troubleshooting please check for typing errors.

5.1.3.8.) Orion 2.0.7

The following instructions show how to set up and run the servlet example under Orion 2.0.7. The instructions assume that you have Orion 2.0.7 server installed on the system. The location of the Orion installation will be referenced as <OR_INSTALL_DIR> and the location of the EspressReport installation will be referenced as <ER_INSTALL_DIR>.

Follow the steps below (note that the files are under <ER_INSTALL_DIR>\help\examples\servlet\StreamingReport directory):

Note: For troubleshooting please check for typing errors.

5.1.3.9.) JRun 4 (with Update 6)

The following instructions show how to set up and run the servlet example under JRun 4 (with Update 6). The instructions assume that you have JRun server installed on the system. The location of the JRun installation will be referenced as <JR_INSTALL_DIR> and the location of the EspressReport installation will be referenced as <ER_INSTALL_DIR>.

Follow the steps below (note that the files are under <ER_INSTALL_DIR>\help\examples\servlet\StreamingReport directory):

Note: For troubleshooting please check for typing errors.

5.1.3.10.) Oracle 10g (10.1.3.1.0)

The following instructions show how to set up and run the servlet example under Oracle 10g (10.1.3.1.0). The instructions assume that you have Oracle 10g (10.1.3.1.0) server installed on the system. The location of the Oracle 10g installation will be referenced as <ORA_INSTALL_DIR> and the location of the EspressReport installation will be referenced as <ER_INSTALL_DIR>.

Follow the steps below (note that the files are under <ER_INSTALL_DIR>\help\examples\servlet\StreamingReport directory):

Note: For troubleshooting please check for typing errors.

5.1.3.11.) Sun Java System Application PE (9.0, 8.2)

The following instructions show how to set up and run the servlet example under Sun Java System Application PE (9.0 or 8.2). The instructions assume that you have Sun Java System Application PE server installed on the system. The location of the Sun App Server installation will be referenced as <SAP_INSTALL_DIR> and the location of the EspressReport installation will be referenced as <ER_INSTALL_DIR>.

Follow the steps below (note that the files are under <ER_INSTALL_DIR>\help\examples\servlet\StreamingReport directory):

Note: For troubleshooting please check for typing errors.

5.1.3.12.) Sun Java System WebServer 7.0

The following instructions show how to set up and run the servlet example under Sun Java System WebServer 7.0. The instructions assume that you have Sun Java System WebServer 7.0 installed on the system. The location of the Sun WebServer installation will be referenced as <SWS_INSTALL_DIR> and the location of the EspressReport installation will be referenced as <ER_INSTALL_DIR>.

Follow the steps below (note that the files are under <ER_INSTALL_DIR>\help\examples\servlet\StreamingReport directory):

Note: For troubleshooting please check for typing errors.

5.1.3.13.) Resin 3.1.0

The following instructions show how to set up and run the servlet example under Resin 3.1.0. The instructions assume that you have Resin 3.1.0 installed on the system. The location of the Resin installation will be referenced as <RES_INSTALL_DIR> and the location of the EspressReport installation will be referenced as <ER_INSTALL_DIR>.

Follow the steps below (note that the files are under <EC_INSTALL_DIR>\help\examples\servlet\DataFile directory):

Note: For troubleshooting please check for typing errors.

5.1.3.14.) ColdFusion MX 7.02

The following instructions show how to set up and run the servlet example under ColdFusion MX 7.02. The instructions assume that you have ColdFusion MX 7 application server installed on the system. The location of the ColdFusion MX 7.02 installation will be referenced as <CF_INSTALL_DIR> and the location of the EspressReport installation will be referenced as <ER_INSTALL_DIR>.

Follow the steps below (note that the files are under <ER_INSTALL_DIR>\help\examples\servlet\StreamingReport directory):

Note: For troubleshooting please check for typing errors.

5.1.4.) Running the Servlet

After setting up the servlet per the instructions given above, open index.html (located in the EspressReport/help/examples/servlet/StreamingReport directory) from a browser.  Select from the parameters given and then click on Get Report.

5.2.) Java Server Pages (JSP)

5.2.1.) Introduction

Java Server Pages allows the separation of the dynamic content from the static HTML.  JSPs allow HTML to be written in the traditional manner and then the code for the dynamic content is enclosed within the HTML within special tags, which usually start with "<%" and end with "%>"

The JSPs can be placed along with regular HTML files and look like HTML files in many ways.  However, at run time, (JSP's they) get converted to normal servlets while the static HTML pages are printed.

Java Server Pages are basically an extension of the Servlet technology.  However, there is one very big advantage in using JSPs.  Java Server Page technology separates the user interface from content generation enabling designers to change the overall page layout without altering the underlying dynamic content.

EspressReport provides a few JSP examples.  The example we will be looking at EspressReport/help/examples/jsp/StreamingReport.  This example can also be used as a guide to run the other JSP examples as well as your own JSP code.

5.2.2.) Running Under

5.2.2.1.) Apache Tomcat 4.0

5.2.2.2.) WebSphere 3.5

5.3.) Saving the Report to a File versus Sending Report to a Browser

You can use both servlets and JSPs to return a report back to the browser.  The report can either be shown in an applet (i.e., it can be interactive) or be shown in a static format (i.e., HTML, DHTML, PDF).  Net traffic and bandwidth availability should be the key factors in deciding whether to use applets or static formats.

The reports returned to the browsers can be done in two ways; they can be saved to a file or sent directly to the browser.  In the first scenario, the report is saved on the server side and the file location is sent to the client browser which then shows the report.&bsp; In the second scenario, the report itself is sent, as an output stream, to the client browser.

5.3.1.) Saving the Report to a file

Servlets and JSPs can save reports to files if the reports shown need to be re-used or a copy is needed.  Here the reports are saved to a file on the server side (whether the report is shown in an applet or in a static format is immaterial).  However, these types of servlets and JSPs need to be constructed carefully.  If multiple clients hit the page, it is possible for one client to see another client's report.  And the browsers might sometimes cache a previous report (in the case of a static format) and will not show the new report unless refreshed/reloaded again.  And since the files are saved on the server side, care must be taken that the files are not overridden before the client browser has seen the report and purged periodically to avoid taking up server space.

In both cases, a report is exported to the desired format.

A HTML file can then be returned to the browser which contains the report location within the HTML file so that the browser can view it.

Here Report Viewer has been used.  Please note that when using Report Viewer in the above code, EspressManager MUST be running.

In the case of a static format, calling the HTML file (or providing a link to the PDF file within a HTML file) is sufficient to display the report.

Using this method, you can separate the pages returning the report from your servlet/JSP and design them well in advance and outside of your servlet/JSP.  You can use a database or a hashtable to keep track of the reports generated and show them again as needed, as well as have the reports re-generated again periodically.

To see an example of a servlet saving the report as a HTML or DHTML file on the server side, please go to EspressReport/help/examples/servlet/Report.

When you export reports, in HTML or DHTML format, containing charts, you MUST use the method setHTMLParameters to set the directory the charts have to be exported to, the http URL mapped to that directory and to set the name of the export chart.  Thus in the exported report, <img src> tags point to the URL and file name specified (with extensions).

For example, given a report with 3 charts in it and the following method call:

When the report is exported, the above code sees to it that the chart gets exported to the D:/inetpub/wwwroot/EspressReport/chartimages/ directory, with the charts being named ReportWithChart_0, ReportWithChart_1 and ReportWithChart_2 (the extension depends on the image type set).  And in the HTML/DHTML report, the following lines of code will be present

in place of the charts in the report.

To see an example of a servlet saving the report, which contains charts, as a HTML or DHTML file on the server side, please go to EspressReport/help/examples/servlet/ReportWithChart.

You can also add in charts, which contain hyperlinks, to the report.  When you export these types of reports to a DHTML or HTML file, the map file is automatically generated within the DHTML/HTML file as long as the option for the map file is selected in the Designer, or by setting the option in the ReportAPI.  The option is set by getting a handle to the ReportChartObject and then using the method setExportMapFile.

5.3.2.) Sending the Report Directly to a Browser

Servlets and JSPs can send reports directly to the browser so that they need not be saved as files on the server side.  Thus disk space on the server is not being filled up and maintenance of files need not be done.   However, a permanent copy of the report cannot be made (unless printed from the browser), so if needed again, the report has to be re-generated.

Sending reports to the browser differs from saving them to files, as here reports are sent as an outputstream.

For a static format, on the servlet side, an outputstream is created and the report is exported to the outputstream.

In the browser, the servlet URL can be used to view the streamed HTML page.  For example, you can enter the URL http://machine_name:8080/servlet/OutputStreamServlet to generate the report and stream it to the browser as an HTML page.

For an interactive report, the report is exported as a string.

On the HTML side, you can use Report Viewer to view the interactive report.  Here the HTML is embedded in the servlet code itself as shown :-

To see an example of a servlet streaming the report as a HTML or DHTML file, please go to EspressReport/help/examples/servlet/StreamingReport.

On a JSP, you have to create a Java Bean object (myStreamReport), which creates a report and returns it as a String object.  The JSP page has to be modified as shown :-

where myStreamReport Java bean will contain the following code :-

To see an example of a JSP streaming the report as a HTML file, please go to EspressReport/help/examples/jsp/StreamingHTML.

When you stream reports (in HTML or DHTML format) containing charts, you can either use the method setHTMLParameters which saves the charts to a physical location on the server side OR you can stream the charts using the RPTImageGenerator servlet provided.  To use this servlet, you must make it accessible from your servlet runner/application server.  The URL to access this servlet MUST be http://<machine name>:<port number>/servlet/RPTImageGenerator.  The source code for this servlet is in the EspressReport/ImageGenerator directory.  Then in your servlet/jsp code, you must use the QbReport.setDynamicExport(boolean state, String serverName, int servletRunnerPort) .

For example, given a report with 3 charts in it and the following method call:

When the report is exported, the above code sees to it that the chart gets streamed using the machine Aphrodite and the port 8080.  The RPTImageGenerator servlet gets called using the URL, http://Aphrodite:8080/servlet/RPTImageGenerator and thus streams the chart back to the browser.

You can also change the URL for this servlet by using the setServletDirectory(String) method in the QbReport class.  For instance, given the example above, if the following line of code were added:

then the links will show http://Aphrodite:8080/EspressReport/Test/RPTImageGenerator instead of the default servlet/ in the link.

Further, if you are using SSL or wants to use a relative link to locate the RPTImageGenerator, you can use the following methods to configure the SRC attribute of the IMG tag that is used to locate the RPTImageGenerator:

To see an example of a servlet streaming the report, which contains charts,  as a HTML or DHTML file, please go to EspressReport/help/examples/servlet/StreamingReportWithChart.

You can also add in charts, which contain hyperlinks, to the report.  When you export these types of reports to a DHTML or HTML file, the map file is automatically generated within the DHTML/HTML file as long as the option for the map file is selected in the Designer, or by setting the option in the ReportAPI.  The option is set by getting a handle to the ReportChartObject and then using the method setExportMapFile.