JSP CRYSTAL REPORT

Source: Internet
Author: User
Tags dname server memory apache tomcat

 

The development ide I use is eclipse3.2, and the server is Tomcat 5.5.

Create a Crystal Reports Web project

Select Apache Tomcat v5.5 at target Runtime

In the next step, Crystal Reports Java reporting component, dynamic web module, and Java are selected by default. These three are required. You can also add struts, later settings will require you to add the struts class library. Add the example database and report to the end by default.

This is a Crystal Reports Web project (the above steps should be completed by people on earth)

There is a crystalreport1.rpt and crystalreport_viewer.jsp in it.

Right-click crystalreport_viewer.jsp-> running mode-> run on server, and click Finish.

You should be able to see an empty crystal report (Why? Of course, crystalreport1.rpt itself is empty)

In the sample reports folder, there are four examples. RPT report file, right-click any of them, select Crystal Reports-> Create viewer JSP, And A *-Viewer will be created. JSP file. Run this file and you will see a crystal report.

I think this should be no problem for most people. The rest is to analyze all the things in this project.

Bytes --------------------------------------------------------------------------------------------------------------------
What is required in any project? For this reason, I specially set up a new Tomcat project. It is best to create a simple JSP page to test whether your Tomcat project can run normally.

Through my own experiment, if you run the Crystal Report in JSP of other projects, you need the class file and configuration.

1. Add a user Library such as crystalreports libraries and add the following *. Jar

Commons-collections-3.1.jar, commons-configuration-1.2.jar,
Commons-lang-2.1.jar,
Commons-logging.jar,
Concurrent. jar,
Crystalcharting. Jar
Crystalcommon. Jar
Crystalcontentmodels. Jar
Crystaldatabaseconnectors. Jar
Crystalexporters. Jar
Crystalexportingbase. Jar
Crystalformulas. Jar
Crystalqueryengine. Jar
Crystalreportengine. Jar
Crystalreportingcommon. Jar
Derby. Jar
Icu4j. Jar
Jrcadapter. Jar
Jrcerom. Jar
Keycodedecoder. Jar
Log4j. Jar
Metafilerenderer. Jar
Msbase. Jar
MSSQLServer. Jar
Msutil. Jar
Rasapp. Jar
Rascore. Jar
Reportprinter. Jar
Rpoifs. Jar
Serialization. Jar
Uriutil. Jar
Webreporting. Jar
Webreporting-jsf.jar
Xercesimpl. Jar
Xml-apis.jar
Xtreme. Jar

(A total of 36 jar files, some of which are already available in other libraries, just add this user-defined Library to the Project)

2. Place the crystalreportviewers folder in the previous example in the same directory as the WEB-INF folder. This is the basic framework of the Crystal Report, which is equivalent to the source code of the Crystal Report control in. net.

3. Put the crystal-tags-reportviewer.tld under the WEB-INF, the main label

4. Add web. XML in the middle of <web-app> to read crystalreportviewers

<Context-param>
<Param-Name> crystal_image_uri </param-Name>
<Param-value> crystalreportviewers </param-value>
</Context-param>
<Context-param>
<Param-Name> crystal_image_use_relative </param-Name>
<Param-value> webapp </param-value>
</Context-param>

5. Put crconfig. XML in the SRC folder. The configuration file of the crystal report. If you have registered, the registration code will also be in this file.

Crconfig. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Crystalreportengine-configuration>
<Reportlocation>.../</reportlocation>
<Timeout> 0 </timeout>
<Externalfunctionlibraryclassnames>
<Classname/>
</Externalfunctionlibraryclassnames>
</Crystalreportengine-configuration>

Configure timeout Interval
Using the crconfig. xml file, you can configure the timeout interval (in minutes) for determining when to discard the inactive report source ). This is necessary because the inactive report source still consumes system resources (such as disk space used by database connections, server memory, and temporary files ). You can set the timeout tag value in the crconfig. xml file to specify the timeout interval. By default, the timeout interval is 10 minutes. By setting this value to 0, you can set Java reporting component to no timeout.

The timeout interval applies only to non-active reports. A report that is being processed does not time out because it exceeds this value. Each time a report source request is successfully completed, the timeout timer is reset. If a report source is not used during the timeout interval, the report source is discarded and its resources are provided to other processes.

6. insert this code on the JSP page

<% @ Taglib uri = "/crystal-tags-reportviewer.tld" prefix = "crviewer" %>
<Crviewer: viewer reportsourcetype = "reportingcomponent" viewername = "rcname-Viewer" reportsourcevar = "rcname" isownpage = "true">
<Crviewer: Report reportname = "rcname. rpt"/>
</Crviewer: viewer>
Like using the crystal report control in. net, not only the viewer, but also the partviewer.

The configuration in (1) remains unchanged. The previous article used the crviewer label to display the Crystal Report in JSP.

This article teaches you how to write your own code to enjoy the Crystal Report

Report_source.jsp

<% @ Page contenttype = "text/html" %>
<% @ Page pageencoding = "UTF-8" %>

<% // Crystal Java reporting component (JRC) imports. %>
<% -- Jrcerom. jar -- %>
<% @ Page import = "com. crystaldecisions. Reports. SDK. *" %>
<% -- Rascore. jar -- %>
<% @ Page import = "com. crystaldecisions. SDK. occa. Report. Lib. *" %>

<%
// LOCATION OF THE CRYSTAL REPORT
Final string report_name = "view_report.rpt ";
%>

<%

Try ...{
// Open a report
Reportclientdocument reportclientdoc = new reportclientdocument ();
Reportclientdoc. Open (report_name, 0 );

// Put the report source into the session and pass it to the report display page
Session. setattribute ("reportsource", reportclientdoc. getreportsource ());

// Go to the report display page
Response. sendredirect ("crystalreportviewer. jsp ");

}
Catch (reportsdkexception ex )...{
Out. println (Ex );
}
Catch (exception ex )...{
Out. println (Ex );
}
%>
The above code can be encapsulated in JavaBean.

Reportclientdoc. getdatabasecontroller (). Logon (username, password );
Set the database Login User. If the user who browses this report needs to set different permissions, you need to set the preceding permissions.

Crystalreportviewer. jsp

<% @ Page contenttype = "text/html" %>
<% @ Page pageencoding = "UTF-8" %>

<% // Crystal report viewer imports. %>
<% -- Webreporting. jar -- %>
<% @ Page import = "com. crystaldecisions. Report. Web. Viewer. *" %>
<% -- Rascore. jar -- %>
<% @ Page import = "com. crystaldecisions. Reports. SDK. *" %>

<%
// Create a viewer object instance and set
Crystalreportviewer viewer = new crystalreportviewer ();
Viewer. setownpage (true );
Viewer. setownform (true );
Viewer. setprintmode (crprintmode. ActiveX );

// Obtain the report source from the session
Object reportsource = session. getattribute ("reportsource ");
Viewer. setreportsource (reportsource );

// DISPLAY THE CRYSTAL REPORT
Viewer. processhttprequest (request, response, this. getservletconfig (). getservletcontext (), null );

%>
Method 2: directly use a page

Crystalreportviewer. jsp

<% @ Page contenttype = "text/html" %>
<% @ Page pageencoding = "UTF-8" %>

<% // Crystal Java reporting component (JRC) imports. %>
<% -- Jrcerom. jar -- %>
<% @ Page import = "com. crystaldecisions. Reports. SDK. *" %>
<% -- Rascore. jar -- %>
<% @ Page import = "com. crystaldecisions. SDK. occa. Report. Lib. *" %>
<% -- Webreporting. jar -- %>
<% @ Page import = "com. crystaldecisions. Report. Web. Viewer. *" %>

<%
// LOCATION OF THE CRYSTAL REPORT
Final string report_name = "view_report.rpt ";
%>

<%
Try ......{
// Open a report
Reportclientdocument reportclientdoc = new reportclientdocument ();
Reportclientdoc. Open (report_name, 0 );

// Put the report source into the session and pass it to the report display page
// Session. setattribute ("reportsource", reportclientdoc. getreportsource ());

// Create a viewer object instance and set
Crystalreportviewer viewer = new crystalreportviewer ();
Viewer. setownpage (true );
Viewer. setownform (true );
Viewer. setprintmode (crprintmode. ActiveX );

// Obtain the report source from the session
// Object reportsource = session. getattribute ("reportsource ");
// Viewer. setreportsource (reportsource );
Viewer. setreportsource (reportclientdoc. getreportsource ());

// DISPLAY THE CRYSTAL REPORT
Viewer. processhttprequest (request, response, this. getservletconfig (). getservletcontext (), null );


// Go to the report display page
// Response. sendredirect ("crystalreportviewer. jsp ");
}
Catch (reportsdkexception ex )......{
Out. println (Ex );
}
Catch (exception ex )......{
Out. println (Ex );
}
%>
I personally feel that the first method is good. The report source is separated from the display, which is secure and easy to reuse.

In. net, you can easily use SQL statements to filter report data, but this function is not set in cr4e, but can be completed by writing code.

Here is an example of a simple SQL statement used to filter data.

The project still uses the temporary Object Storage Service (omcat) project.

Create a class to implement this function

Jrc_resultset_performance.java

Package com. jrc. util;
Import java. SQL .*;
Import javax. servlet. http .*;

Import com. crystaldecisions. Reports. SDK .*;
Import com. crystaldecisions. SDK. occa. Report. Lib .*;

Public class jrc_resultset_datasource ...{
Private string report_name = "";
Public jrc_resultset_datasource (string report_name )...{
This. report_name = report_name;
}

/***//**
* @ Return report_name
*/
Public String getreport_name ()...{
Return report_name;
}

/***//**
* @ Param report_name the report_name to be set
*/
Public void setreport_name (string report_name )...{
Report_name = report_name;
}

/***//**
* Connect to the database and use SQL query statements to query and return the result set.
*/
Private Static resultset getresultsetfromquery (string query, int scrolltype)
Throws sqlexception, classnotfoundexception ...{
Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ");
Final string dbusername = "username ";
Final string dbpassword = "password ";
Final string connection_url = "JDBC: Microsoft: sqlserver: // localhost: 1433; database = dname ";

Java. SQL. Connection connection = drivermanager. getconnection (connection_url, dbusername, dbpassword );
Statement statement = connection. createstatement (scrolltype, resultset. concur_read_only );

Return statement.exe cutequery (query );

}
/***//**
* Using SQL statements to filter report data does not have to be miserable in. net.
*/
Public Boolean isreportsourceinsession (string session_name, httpsession session) throws reportsdkexception, sqlexception, classnotfoundexception ...{
Boolean flag = false;
Try ...{
// OPEN THE CRYSTAL REPORT
Reportclientdocument reportclientdoc = new reportclientdocument ();
Reportclientdoc. Open (report_name, 0 );
// In an SQL query statement, the number of returned fields must be the same as that in the report. The number of returned fields must be unique or distinct, and the field type must be the same as that in the report. Other fields can be used regardless of the data.
// Fill in the from table, such as the database name. DBO. database table. It is best to create an alias.
String query = "select TT. test_1, TT. test_2, TT. test_3, TT. test_4 from dname. DBO. Test TT ";

Resultset = This. getresultsetfromquery (query, resultset. type_scroll_insensitive );

String tablealias = reportclientdoc. getdatabasecontroller (). getdatabase (). gettables (). gettable (0). getalias ();
// Put the result set into the report. A datasource is automatically generated.
Reportclientdoc. getdatabasecontroller (). setdatasource (resultset, tablealias, "resultsettable ");
Session. setattribute (session_name, reportclientdoc. getreportsource ());
Flag = true;
Return flag;
} Catch (exception e )...{
// Todo: handle exception
E. printstacktrace ();
Return flag;
}

}
}

Note that the number of fields in the database query result set must be the same as the number of fields in the report, and the type must be the same. Otherwise, an error will occur.

The name of the table in the SQL statement must be complete, such as the database name. DBO. database table. It is best to create an alias.

Display page

Result_viewer.jsp

<% @ Page import = "com. jrc. util. jrc_resultset_datasource" %>
<% -- Webreporting. jar -- %>
<% @ Page import = "com. crystaldecisions. Report. Web. Viewer. *" %>
<% -- Jrcerom. jar -- %>
<% @ Page import = "com. crystaldecisions. Reports. SDK. *" %>
<%
Jrc_resultset_datasource jrcd = new jrc_resultset_datasource ("resultset. rpt ");
If (! Jrcd. isreportsourceinsession ("reportsource", session)
Response. sendredirect ("error.html ");
Crystalreportviewer crviewer = new crystalreportviewer ();
Crviewer. setownpage (true );
Crviewer. setownform (true );
Crviewer. setprintmode (crprintmode. ActiveX );

Object reportsource = session. getattribute ("reportsource ");
Crviewer. setreportsource (reportsource );

Crviewer. processhttprequest (request, response, this. getservletconfig (). getservletcontext (), null );
%>
Note that the above points should be okay.

PS: some practical code segments of Crystal Reports are listed at the bottom of the snippets view.

Cr viewer tags, opens and queries reports, opens reports, views reports, views reports, and sets database logon, exports reports as PDF, and exports reports as RTF. This is easy to use.

In eclipse, how does one insert pojo into the crystal report? This is what we need to do in the fourth article.

First, we should create an object class.

Here I use orderitems. Java as an example, code

Package com. jrc. beans;

Public class orderitems ...{
Public int ID; // the ID of the corresponding database table
Public int quantity; // number of products, which are not available in database tables
Public orderitems (int id, int quantity )...{
Super ();
This. ID = ID;
This. Quantity = quantity;
}
/***//**
* @ Return ID
*/
Public int GETID ()...{
Return ID;
}
/***//**
* @ Param ID: ID to be set
*/
Public void setid (int id )...{
This. ID = ID;
}
/***//**
* @ Return quantity
*/
Public int getquantity ()...{
Return quantity;
}
/***//**
* @ Param quantity the quantity to be set
*/
Public void setquantity (INT quantity )...{
This. Quantity = quantity;
}
}

If you have not created a report, create a report, such as report. Rpt, and open the report.

In the project resource manager, click orderitems. + (the image cannot be sent) in front of Java. You can see "Green Point orderitems". Right-click "Crystal Reports-" and add it to the current crystal report. Alternatively, you can directly pull "Green Point orderitems" to the report.

In this way, orderitems will be added to the report data and a database table will be added to the report data, such as products (product_id, product_name, product_price ,....,)

If there is no problem, report. the data in the RPT report contains the orderitems table and the products table, and left-click the ID of the orderitems table to pull it to the products_id of the Products table. You can find that there is a line between them.

In this way, orderitems. ID is associated with products. products_id. The default Line property in the middle is equal to or you can change it to another one.

Right-click report. rpt-Crystal Reports-create viewer JSP-select viewer API code insert-select use pojo to fill the report and connect to the Crystal Report page viewer-OK

A new Report-viewer.jsp page is coming out. However, it is useless now. You need to change a bit of stuff in it.

Note: The jrchelpersample class is used in the Report-viewer.jsp. This is a very good tool class in the CR project created in (1). Copy it to your project if it's okay, this class is used in many places. This class is included in the package com. businessobjects. samples.

String reportname = ""; // enter your report name

List dataset = new arraylist ();
Dataset. Add (New orderitems ());
Dataset. Add (New orderitems ());
Dataset. Add (New orderitems ());
Dataset. Add (New orderitems ());
Dataset. Add (New orderitems ());
You can use the orderitems constructor to add a dataset. You can use many methods to add a dataset. You only need to add an exact and effective orderitems object.

Remove /**/

Start Tomcat and run the Report-viewer.jsp.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.