Recently developed a MIS system, Java technology, B / S structure. Customer Reporting system reports use a crystal statement. How to implement a crystal statement in JSP? Looking through all the information, the Web implementation of the crystal report is a lot, but It is Microsoft's .NET solution. How to achieve the dynamic display of the crystal report in JSP? After a period of pain, now introduce my program to everyone, I hope that the big brother is criticized.
My question: It is necessary to display the crystal report and to display data according to the value of the value (BH) of the parameter (BH) I passed. And you can export the crystal report to Word, Excel, PDF.
My Solution: 1: Download Crystal Reports 10 for Bea WebLogic Workshop 2: Installing Crystal Reports 10 for Bea WebLogic Workshop; 3: If the installation is successful, in C: / Program Files / Common Files / Crystal Decisions / 2.5 / bin directory Under, you can find a crdb_javaserver.ini file. This file is important, the content is related to how the crystal statement accesses the database. My crystal report uses JDBC to access the Oracle database in a JDBC. The configuration is as follows:
[Common] PATH = C: / BEA / JDK141_05 / BinclassPath = 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/websph ~ 1/Applic ~ 1/v5. 1.1 / runtimes / base_v51 / lib / naming.jar; D: /Progra ~ 1/ibm/websph ~ 1/Applic ~1/v51/runtimes/base_v51/lib/namingclient.jar; D: / Program ~ 1 / IBM / WebSph ~ 1 / Applic ~ 1 / V5.1.1 / Runtimes / Base_v51 / lib / namingserver.jar; D: /Progra ~ 1/IBM/websph ~ 1/Applic ~ 1/v51/lib / cmimpl.jar; DEBSPH ~ 1/Applic ~ 1/v51/runtimes/base_v51/lib/j2cimpl.jar;c :/projects/drivers/jdbc/weblogic.jar;c : / Program Files / Common Files / Crystal Decisions / 2.5 / bin / crdbjavaser.jar; c: /bea/weblogic81/server/lib/ojdbc14.jar
Iorfilelocation = $ {temp} javaservertimeout = 1800JVMMAXHEAP = 640000jvmminHeap = 32000000
[CRDB_JDBC] CacheRowSetSize = 100JDBCURL = jdbc: oracle: thin: @ 192.168.1.99: 1521: pwsc JNDIURL = JDBCUserName = testuserJDBCDriverName = oracle.jdbc.driver.OracleDriverJNDIUserName = weblogicJNDIConnectionFactory = JNDIInitContext = / GenericJDBCDriverBehavior DB2 [CRDB_XML] PREREADNBYTE = 5000CacheRowSetSize = = 100XMLLOCALL = schemalocalurl = XMLHTTPURL = SchemaHttpurl = useMPFile = true
The above is the configuration file, you need to explain: 1: Since I use a JDBC to access the database, I must add C: /bea/weblogic81/server/lib/ojdbc14.jar in ClassPath, which is JDBC Drive 2: [CRDB_JDBC]: JDBCURL = JDBC: Oracle: Thin: @ 192.168.1.99: 1521: PWSC is the database name I want to access .jdbcusername = testUser is setting a username, jdbcdrivername = oracle.jdbc.driver.OracleDriver Is a JDBC driver name
Four: Edit the report I want to use in the Crystal Report Editor. Since I need to pass the parameters to the report, I set a parameter field in the report, and the crystal report can display the corresponding data according to this parameter field. The parameter field is named BH, and the following code is written in the formula editor: if {? Bh} <> "" "{gg_bpbj.jlbh} = {? Bh} else {gg_bpbj.jlbh} =" 1 "or {gg_bpbj.jlbh } <> "1" The above code means: If the BH parameter is not empty, the {gg_bpbj.jlbh} is displayed is equal to the record of the parameter BH, otherwise it will display all records (everyone may think {gg_bpbj.jlbh} = "1 "OR {gg_bpbj.jlbh} <>" 1 "is ridiculous, yeah, I started to write" True ", you can run well in the crystal report editor, but there is a problem on the web. Helpless, only this, :()
5: Newly built a web project in WebLogic Workshop, select the project folder with right click, select "Crystal Report", then automatically build a CrystalReportViewers10 directory in the root of the web project, there is a crystal report The page element system required automatically adds the JAR package required to develop crystal reports, a total of 13 JAR files (under the web_inf / lib directory), and add some crystal report-specific labels (under Web_INF / SRC)
Six: Special attention is: Web.xml file under web_inf also change, the changed web.xml file is as follows: XML Version = "1.0" Encoding = "UTF-8"?> " !- 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, pleaseconsult the Web.xml Deployment Descriptor Elements chapter of the "Developing WebLogic ServerApplications "documentation on edocs.bea.com. ->
Load-on-startup> 1 loading-on-startup> servlet>
Netui-tags-template.tld taglib-uri>
7: Put the edited crystal report (after the life of the RPT) in a directory (I have built a report directory yourself, specifically stores report files)
8: You can write the program: 1. Write the ReportInit class. Role: Use to pass the parameters BH. This class is critical, which is responsible for passing Java parameter values to crystal report files. 2. Add showrpt operation in page stream: actually Instantiate a ReportInit class .3. Write the viewer.jsp file to dynamically display the crystal report.
Behind my source code, please criticize the correct.
Problems: 1: There is a problem when the crystal report is exported into a PDF file: Chinese characters are displayed as garbled, I don't know why? This problem is bothered for a few months, can't solve the print / export page Hanheng: in CrystalReportViewers10 / JS There is a series of strings named JS files, I guessing is used to internationalize, but I modify strings_en, js, strings_en.js, no effect. 3: After displaying the crystal report in the browser, close the display page, The crystal report is still connected to a database (can be viewed by Select * from V $ session). If you add a disconnected statement in viewer.jsp: viewer.dispose (); rPTSource.dispose (); Can display a report. And print and export cannot work.
The above problem is troubled for a long time, although it does not affect the use, but the heart is still not very cool. I hope the expert guidance.
Source program:
/ * * CREATE DATE: 2004-8-14 11:06 * Create By: Li Chunlei * Purpose: Set the query parameters of the crystal report (currently supporting the query of 7 parameters, 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 fields; public ReportInit () {rptSource = null; fields = null;} public IReportSource getRptsource () {return rptSource;} public Fields getFields () {return fields;} public boolean setReport ( String fname, String rptID) {// get query 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 (); // number 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 (); value value value = new value (); values value value value = new value (); value value value = 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: No. Pfield1.SetReportName (""); pfield1.setname ("BH"); PfieldDv1.SetValue (BH); PfieldDv1.setDescription ("Number); VALS1. Add (pfielddv1); pfield1.setcurrentValues (VALS1); // Set the second parameter: unit department pfield2.setReportName (""); pfield2.setname ("dw"); pfielddv2.setValue (""); Pfielddv2.setdescription ("Order Department"); Vals2.Add (PfieldDv2); Pfield2.SetCurrentValues (VALS2); // Set the third parameter: people pfield3.setReportName (""); pfield3.setname ("ren") Pfielddv3.SetValue (""); PfieldDv3.SetDescription ("Applicant, Director, Manager, Hearers, 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 (pfieldddv5); pfield5.setcurrentValues (VALS5); // Set the sixth query field pfield6.setReportName (" Field6.setname); PfieldDv6.SetValue (""); Pfielddv6.setdescription ("Query field 6: Change reason, work content, etc."); vals6.add (pfieldddv6); 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 ( "ending time"); vals4_JSSJ.add (pfieldDV4_JSSJ); pfield4_JSSJ.setCurrentValues (vals4_JSSJ); // parameter field and Report related FIEL DS.Add (pfield1); Fields.Add (Pfield2); Fields.Add (Pfield3); Fields.Add (Pfield5); 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: / ** * @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 Fields = rpt.getFields (); IReportSource rptSource = rpt.getRptsource (); this.getSession () setAttribute ( "fields", fields);.. this.getSession () setAttribute ( "rptSource", rptSource); return new Forward ( " ");} else {return new forward (" false ");}}}} Return New Forward (" false ");}
//viewer.jsp is used to dynamically display the crystal report. The contents of theViewer.jsp are 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.GetaTribute ("Nametest"); // system.out.println ("Name =" Name);%> <% IReportSource rptSource = (IReportSource) session.getAttribute ( "rptSource"); Fields fields = (Fields) session. GetaTtribute ("Fields"); connectioninfos connfos = 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);