Reference Crystal Reports: http://www.newasp.net/tech/java/15560.html

Source: Internet
Author: User
Tags tld
Jsp implementation of the crystal report (Development Tool: BEA WebLogic Workshop)

Recently, a MIS system, Java technology, and B/S structure was developed for the customer. The customer requested that the system report should adopt a crystal report.
How to Implement the Crystal Report in JSP? After searching all the materials, there are many web implementations of the crystal report, but they all talk about how to achieve dynamic display of the Crystal Report in Microsoft's. Net solution. jsp?
After a period of hard work, I want to introduce my solution to you.

My questions:
You need to dynamically display the Crystal Report, and dynamically display the data based on the value of a parameter (BH) that I passed. You can also export the Crystal Report to Word, Excel, PDF, and other formats.

My solutions:
1. Download Crystal Reports 10 For BEA WebLogic Workshop
2. Install Crystal Reports 10 For BEA WebLogic Workshop;
3. If the installation is successful, find the crdb_javaserver.ini file in the C:/program files/common files/crystal decisions/2.5/bin directory. This file is very important and contains content.
It is related to how the Crystal Report accesses the database. My Crystal Report uses JDBC to access the Oracle database. The configuration is as follows:

[Common]
Path = C:/BEA/jdk142.1605/bin
Classpath = C:/projects/Drivers/jdbc/classes12.jar;
C:/projects/Drivers/jdbc/msbase. jar;
C:/projects/Drivers/jdbc/MSSQLServer. jar;
C:/projects/Drivers/jdbc/msutil. jar;
C:/projects/Drivers/jdbc/common. jar;
C:/projects/Drivers/jdbc/db2fs. jar;
C:/projects/Drivers/jdbc/db2java.zip;
C:/projects/Drivers/jdbc/db2jcc. jar;
C:/projects/Drivers/jdbc/weblogic. jar;
D:/progra ~ 1/IBM/web7d ~ 1/applic ~ 1/v5.1.1/runtimes/base_v51/lib/naming. jar;
D:/progra ~ 1/IBM/web7d ~ 1/applic ~ 1/v5.1.1/runtimes/base_v51/lib/namingclient. jar;
D:/progra ~ 1/IBM/web7d ~ 1/applic ~ 1/v5.1.1/runtimes/base_v51/lib/namingserver. jar;
D:/progra ~ 1/IBM/web7d ~ 1/applic ~ 1/v5.1.1/runtimes/base_v51/lib/cmimpl. jar;
D:/progra ~ 1/IBM/web7d ~ 1/applic ~ 1/v5.1.1/runtimes/base_v51/lib/j2cimpl. jar;
C:/projects/Drivers/jdbc/weblogic. jar;
C:/program files/common files/crystal decisions/2.5/bin/crdbjavaserver. jar;
C:/BEA/weblogic81/Server/lib/ojdbc14.jar

Iorfilelocation =$ {temp}
Assumervertimeout = 1800
Jvmmaxheap = 64000000
Jvmminheap = 32000000

[Crdb_jdbc]
Cacherowsetsize = 100
Jdbcurl = JDBC: oracle: thin: @ 192.168.1.99: 1521: PWSC
Jndiurl =
Jdbcusername = testuser
Jdbcdrivername = oracle. JDBC. Driver. oracledriver
Jndiusername = weblogic
Jndiconnectionfactory =
Jndiinitcontext =/
Genericjdbcdriverbehavior = DB2

[Crdb_xml]
Prereadnbyte= 5000
Cacherowsetsize = 100
Xmllocalurl =
Schemalocalurl =
Xmlhttpurl =
Schemahttpurl =
Usetempfile = true

The preceding configuration file must be noted as follows:
1: Because I use JDBC to access the database, C:/BEA/weblogic81/Server/lib/ojdbc14.jar must be added in classpath. It is a JDBC driver.
2: In [crdb_jdbc]: jdbcurl = JDBC: oracle: thin: @ 192.168.1.99: 1521: PWSC is the name of the database I want to access. jdbcusername = testuser is the username set,
Jdbcdrivername = oracle. JDBC. Driver. oracledriver is the JDBC driver name.

4. Edit the report to be used in the crystal report editor. because I need to dynamically pass parameters to the report, I set a parameter field in the Report. Based on this parameter field, the crystal report can display the corresponding
Data, the parameter field is named BH, and the following code is written in the Public Editor:
If {? BH} <> "" then
{Gg_bpbj.jlbh} = {? BH}
Else
{Gg_bpbj.jlbh} = "1" or {gg_bpbj.jlbh} <> "1"
The code above indicates that if the BH parameter is not blank, {gg_bpbj.jlbh} is displayed as the record with the same value as the BH parameter; otherwise, all records are displayed.
(You may think that {gg_bpbj.jlbh} = "1" or {gg_bpbj.jlbh} <> "1" is ridiculous. Yeah, I started to write it as "true, in the crystal report editor, it works well, but it is displayed on the web.
Question ,:()

5. Create a new web project in WebLogic Workshop, right-click the project folder, select "new", and select "Crystal Report ", A new one is automatically created under the root directory of the web project.
Crystalreportviewers10 Directory, which contains the page elements required by the crystal report. The system automatically adds the jar packages required for the development of the crystal report. There are 13 jar files (under the web_inf/lib directory ),
Added some special labels for Crystal Reports (under web_inf/src)

6. Note that the Web. xml file under web_inf is also changed. The modified web. xml file is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype web-app public "-// Sun Microsystems, Inc. // DTD web application 2.3 //" http://java.sun.com/dtd/web-app_2_3.dtd ">
<! -- The Web. xml file is a configuration file used to control the behavior of WebLogic Server.
In most cases, you will not need to modify this file. For more information on web. XML, please
Consult the Web. xml deployment descriptor elements chapter of the "Developing WebLogic Server
Applications "documentation on edocs.bea.com. -->
<Web-app>
<Display-Name> workshop application </display-Name>
<Context-param>
<Param-Name> weblogic. httpd. inputcharset./* </param-Name>
<Param-value> GBK </param-value>
</Context-param>
<Context-param>
<Param-Name> crystal_image_uri </param-Name>
<Param-value>/webmis/crystalreportviewers10 </param-value>
</Context-param>
<Filter>
<Filter-Name> pageflowjspfilter </filter-Name>
<Filter-class> com. Bea. wlw. netui. pageflow. pageflowjspfilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> pageflowjspfilter </filter-Name>
<URL-pattern> *. jsp </url-pattern>
</Filter-mapping>
<Listener>
<Listener-class> com. Bea. wlw. runtime. Core. servlet. webappcontextlistener </listener-class>
</Listener>
<! -- Standard action servlet configuration (with debugging) -->
<Servlet>
<Servlet-Name> messageservlet </servlet-Name>
<Servlet-class> com. infoearth. servlets. gettaskservlet </servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet>
<Servlet-Name> action </servlet-Name>
<Servlet-class> com. Bea. wlw. netui. pageflow. dynamicsubappactionservlet </servlet-class>
<Init-param>
<Param-Name> config </param-Name>
<Param-value>/WEB-INF/jpf-struts-config.xml </param-value>
</Init-param>
<Init-param>
<Param-Name> config/-Global </param-Name>
<Param-value>/WEB-INF/jpf-Struts-config -- Global. xml </param-value>
</Init-param>
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> 2 </param-value>
</Init-param>
<Init-param>
<Param-Name> detail </param-Name>
<Param-value> 2 </param-value>
</Init-param>
<Load-on-startup> 2 </load-on-startup>
</Servlet>

<Servlet>
<Servlet-Name> drawimage </servlet-Name>
<Servlet-class> com. infoearth. drawimage. drawimageservlet </servlet-class>
</Servlet>

<Servlet-mapping>
<Servlet-Name> drawimage </servlet-Name>
<URL-pattern> drawimage </url-pattern>
</Servlet-mapping>

<! -- Struts action servlet mappings -->
<! -- Note that because struts takes the * last * mapping here as the extension to add
Actions posted from forms, we must have *. Do come after *. jpf. -->
<Servlet-mapping>
<Servlet-Name> action </servlet-Name>
<URL-pattern> *. jpf </url-pattern>
</Servlet-mapping>
<Servlet-mapping>
<Servlet-Name> action </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>
<Servlet-mapping>
<Servlet-Name> messageservlet </servlet-Name>
<URL-pattern>/messageservlet </url-pattern>
</Servlet-mapping>
<Mime-mapping>
<Extension> doc </extension>
<Mime-type> application/MSWord </mime-type>
</Mime-mapping>
<Mime-mapping>
<Extension> XLS </extension>
<Mime-type> application/MSExcel </mime-type>
</Mime-mapping>
<Welcome-file-List>
<Welcome-File> login/logincontroller. jpf </welcome-File>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
<Error-page>
<Error-code> 500 </error-code>
<Location>/error. jsp </location>
</Error-page>
<! -- Define the netui tag library TLDs -->
<Taglib>
<Taglib-Uri> netui-tags-html.tld </taglib-Uri>
<Taglib-location>/WEB-INF/netui-tags-html.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-Uri> netui-tags-databinding.tld </taglib-Uri>
<Taglib-location>/WEB-INF/netui-tags-databinding.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-Uri> netui-tags-template.tld </taglib-Uri>
<Taglib-location>/WEB-INF/netui-tags-template.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-Uri>/crystal-tags-reportviewer.tld </taglib-Uri>
<Taglib-location>/WEB-INF/crystal-tags-reportviewer.tld </taglib-location>
</Taglib>
</Web-app>

Where:

<Context-param>
<Param-Name> crystal_image_uri </param-Name>
<Param-value>/webmis/crystalreportviewers10 </param-value>
</Context-param>
It is critical to set the crystalreportviewers10 path for the crystal report (the crystalreportviewers10 directory created in step 5). If the setting is incorrect, click
When the export and print buttons are displayed, a page error is reported, and the export and other icons of the crystal report cannot be displayed normally.

7. Put the edited crystal report (suffixed with RPT) in a directory (I created a report directory to store report files)

8: you can write a program:
1. Write the reportinit class. function: it is used to pass the parameter BH. This class is critical and is responsible for passing the Java parameter values to the crystal report file.
2. Add the showrpt operation to the page stream: in fact, it is to instantiate a reportinit class.
3. Compile the viewer. jsp file to dynamically display the crystal report.

I have the source code later. Please criticize and correct it.

Problems:
1: Why is the Chinese character garbled characters displayed when the crystal report is exported to a PDF file? This problem has been bothering me for several months and cannot be solved.
2: Unable to print/export the page in Chinese: Under the crystalreportviewers10/JS directory, there are a series of JS files named after strings. I guess they are used for internationalization pages, but I modified
Strings_zh, JS, strings_en.js, no effect.
3: After the crystal report is displayed in the browser, the display page is closed. The crystal report is still connected to the database (select * from V $ session can be used to view the number of Oracle connections ).
In viewer. jsp, add the following disconnect statement:
Viewer. Dispose ();
Rptsource. Dispose ();
Only one page of reports can be displayed. Printing and exporting cannot work.

The above problems have plagued me for a long time. Although they do not affect the use, they are still not very nice. I hope you can give me some advice.

Source program:

/*
* Create Date:
* Create by: Li chunlei
* Purpose: Set the query parameters of the crystal report. (currently, seven parameters are supported. useful parameters are numbered)
*/

Package com. infoearth. Report;
Import com. crystaldecisions. Report. Web. Viewer .*;
Import com. crystaldecisions. SDK. occa. Report. Data .*;
Import com. crystaldecisions. Reports. reportengineinterface. jpereportsourcefactory;
Import com. crystaldecisions. SDK. occa. Report. reportsource. ireportsourcefactory2;
Import com. crystaldecisions. SDK. occa. Report. reportsource. ireportsource;

Public class reportinit
{
Private ireportsource rptsource;

Private fields;

Public reportinit (){
Rptsource = NULL;
Fields = NULL;
}

Public ireportsource getrptsource (){
Return rptsource;
}

Public fields getfields (){
Return fields;
}

Public Boolean setreport (string fname, string rptid ){
// Obtain query Conditions
String filename = fname + ". rpt ";
String bH = rptid;
String Path = "report/" + filename; // report file path
Try {
Ireportsourcefactory2 RSF = new jpereportsourcefactory ();
Java. util. locale localetest = java. util. locale. China;
Rptsource = (ireportsource) RSF. createreportsource (path, localetest );
Fields = new fields ();
Parameterfield pfield1 = new parameterfield (); // No.
Parameterfield pfield2 = new parameterfield (); // Unit
Parameterfield pfield3 = new parameterfield (); // person
Parameterfield pfield5 = new parameterfield (); // field5
Parameterfield pfield6 = new parameterfield (); // field6
Parameterfield pfield4_kssj = new parameterfield (); // start time
Parameterfield pfield4_jssj = new parameterfield (); // End Time

Values vals1 = new values ();
Values vals2 = new values ();
Values vals3 = new values ();
Values vals5 = new values ();
Values vals6 = new values ();
Values vals4_kssj = new values ();
Values vals4_jssj = new values ();

Parameterfielddiscretevalue pfielddv1 = new parameterfielddiscretevalue ();
Parameterfielddiscretevalue pfielddv2 = new parameterfielddiscretevalue ();
Parameterfielddiscretevalue pfielddv3 = new parameterfielddiscretevalue ();
Parameterfielddiscretevalue pfielddv5 = new parameterfielddiscretevalue ();
Parameterfielddiscretevalue pfielddv6 = new parameterfielddiscretevalue ();
Parameterfielddiscretevalue pfielddv4_kssj = new parameterfielddiscretevalue ();
Parameterfielddiscretevalue pfielddv4_jssj = new parameterfielddiscretevalue ();
// Set the first parameter: Number
Pfield1.setreportname ("");
Pfield1.setname ("bh ");
Pfielddv1.setvalue (BH );
Pfielddv1.setdescription ("no ");
Vals1.add (pfielddv1 );
Pfield1.setcurrentvalues (vals1 );
// Set the second parameter: Department
Pfield2.setreportname ("");
Pfield2.setname ("DW ");
Pfielddv2.setvalue ("");
Pfielddv2.setdescription ("issuing department ");
Vals2.add (pfielddv2 );
Pfield2.setcurrentvalues (vals2 );
// Set the third parameter: person
Pfield3.setreportname ("");
Pfield3.setname ("Ren ");
Pfielddv3.setvalue ("");
Pfielddv3.setdescription ("Applicant, downstream person, operator, owner, etc ");
Vals3.add (pfielddv3 );
Pfield3.setcurrentvalues (vals3 );
// Set the fifth query field
Pfield5.setreportname ("");
Pfield5.setname ("field5 ");
Pfielddv5.setvalue ("");
Pfielddv5.setdescription ("query field 5: Task source line name, etc ");
Vals5.add (pfielddv5 );
Pfield5.setcurrentvalues (vals5 );
// Set the sixth query field
Pfield6.setreportname ("");
Pfield6.setname ("field6 ");
Pfielddv6.setvalue ("");
Pfielddv6.setdescription ("query field 6: Reasons for change, work content, etc ");
Vals6.add (pfielddv6 );
Pfield6.setcurrentvalues (vals6 );
// Set the start time
Pfield4_kssj.setreportname ("");
Pfield4_kssj.setname ("kssj ");
Pfielddv4_kssj.setvalue ("");
Pfielddv4_kssj.setdescription ("Start Time ");
Vals4_kssj.add (pfielddv4_kssj );
Pfield4_kssj.setcurrentvalues (vals4_kssj );
// Set the end time
Pfield4_jssj.setreportname ("");
Pfield4_jssj.setname ("jssj ");
Pfielddv4_jssj.setvalue ("");
Pfielddv4_jssj.setdescription ("End Time ");
Vals4_jssj.add (pfielddv4_jssj );
Pfield4_jssj.setcurrentvalues (vals4_jssj );
// Associate parameter fields with reports
Fields. Add (pfield1 );
Fields. Add (pfield2 );
Fields. Add (pfield3 );
Fields. Add (pfield5 );
Fields. Add (pfield6 );
Fields. Add (pfield4_kssj );
Fields. Add (pfield4_jssj );
Return true;
}
Catch (exception e ){
System. Out. println ("Class: reportinit error:" + E );
Return false;
}
}
}

// Add the following operations to the page stream:
/**
* @ Jpf: Action
* @ Jpf: Forward name = "false" Path = "error. jsp"
* @ Jpf: Forward name = "success" Path = "viewer. jsp"
*/
Protected forward showrpt ()
{
String rptid = This. getrequest (). getparameter ("rptid ");
String rptbh = This. getrequest (). getparameter ("rptbh ");
String rptname = dbctrl. getrptcnname (rptid );
If (rptname! = NULL) & (rptbh! = NULL )){
Reportinit RPT = new reportinit ();
Boolean result = Rpt. setreport (rptname, rptbh );
If (result = true ){
Fields = Rpt. getfields ();
Ireportsource rptsource = Rpt. getrptsource ();
This. getsession (). setattribute ("fields", fields );
This. getsession (). setattribute ("rptsource", rptsource );
Return new forward ("success ");
} Else {
Return new forward ("false ");
}
}
Return new forward ("false ");
}

// Viewer. JSP is used to dynamically display the content of the crystal report. Viewer. jsp as follows:

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" %>
<% @ Page import = "com. crystaldecisions. Report. Web. Viewer .*,
Com. crystaldecisions. SDK. occa. Report. Data .*,
Com. crystaldecisions. Reports. reportengineinterface. jpereportsourcefactory,
Com. crystaldecisions. SDK. occa. Report. reportsource. ireportsourcefactory2,
Com. crystaldecisions. SDK. occa. Report. reportsource. ireportsource "%>
<% @ Taglib uri = "netui-tags-html.tld" prefix = "netui" %>
<%
// Fields rptsource
// String name = (string) request. getattribute ("nametest ");
// System. Out. println ("name =" + name );
%>
<! -- ../Crystalreportviewers10/CSS/default.css -->
<LINK rel = "stylesheet" type = "text/CSS" href = "../crystalreportviewers10/CSS/default.css">
<%
Ireportsource rptsource = (ireportsource) Session. getattribute ("rptsource ");
Fields = (fields) Session. getattribute ("fields ");
Connectioninfos conninfos = new connectioninfos ();
Iconnectioninfo conninfo1 = new connectioninfo ();
Conninfo1.setusername ("testuser ");
Conninfo1.setpassword ("test ");
Conninfos. Add (conninfo1 );
Crystalreportviewer viewer = new crystalreportviewer ();
Viewer. setreportsource (rptsource );
Viewer. setdatabaselogoninfos (conninfos );
Viewer. setparameterfields (fields );
Viewer. setenableparameterprompt (true );
Viewer. setownpage (true );
Viewer. setownform (true );
Viewer. setprintmode (crprintmode. ActiveX );
Viewer. sethasexportbutton (true );
Viewer. sethasprintbutton (true );
Viewer. sethaslogo (false );
Viewer. processhttprequest (request, response, getservletconfig (). getservletcontext (), OUT );
Viewer. Refresh ();
// Viewer. Dispose ();
// Rptsource. Dispose ();
%>

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.