The JSP implementation of Crystal Report (development tool: BEA WebLogic Workshop)

Source: Internet
Author: User
Tags filter config locale query tld root directory oracle database
Js|web| Crystal Report recently for customers to develop a MIS system, Java technology, B/s structure. The customer requests the system The report uses the Crystal Report.
How to realize the Crystal Report in JSP? Look through all the data, Crystal Report Web implementation is a lot, but all about Microsoft's. NET scenario. How to realize the dynamic display of Crystal Report in JSP?
After a period of painful groping, and now introduce my proposal to everyone, I hope that your eldest brother criticized

My question:
The Crystal report needs to be displayed dynamically, and the data will be displayed dynamically according to the value of one of the parameters (BH) I pass. And you can export the Crystal Report to word,excel,pdf format

My solution:
One: Download Crystal Reports for BEA WebLogic Workshop
Two: Install Crystal Reports for BEA WebLogic workshop;
Third: If the installation is successful, the Crdb_javaserver.ini file can be found in the C:\Program Files\Common Files\Crystal Decisions\2.5\bin directory. The file is important and the contents
Related to Crystal Reports how to access the database. My Crystal report accesses the Oracle database in a JDBC manner, configured as follows:

[Common]
PATH = C:\bea\jdk141_05\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/websph~1/applic~1\v5.1.1\runtimes\base_v51\lib\naming.jar;
D:/progra~1/ibm/websph~1/applic~1\v5.1.1\runtimes\base_v51\lib\namingclient.jar;
D:/progra~1/ibm/websph~1/applic~1\v5.1.1\runtimes\base_v51\lib\namingserver.jar;
D:/progra~1/ibm/websph~1/applic~1\v5.1.1\runtimes\base_v51\lib\cmimpl.jar;
D:/progra~1/ibm/websph~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}
javaservertimeout = 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

These are the configuration files that need to be described:
1: Because I am using the JDBC way to access the database, so classpath must add C:\bea\weblogic81\server\lib\ojdbc14.jar, it is JDBC driver
2:[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 to set the user name,
Jdbcdrivername = Oracle.jdbc.driver.OracleDriver is the JDBC driver name

IV: Edit the report I want to use in the Crystal Report editor. Because I need to dynamically pass parameters to the report, so I set a parameter field in the report, the Crystal Report according to this parameter number segment, you can display the corresponding
Data, the parameter field is named BH, and the following code is written in the formula editor:
IF {? Bh}<> "" THEN
{GG_BPBJ. Jlbh}={? BH}
ELSE
{GG_BPBJ. jlbh}= "1" OR {gg_bpbj.jlbh}<> "1"
The above code means that {GG_BPBJ is displayed if the BH parameter is not empty. JLBH} equals the record of the parameter BH, otherwise all records are displayed
(You may think {gg_bpbj.) jlbh}= "1" OR {gg_bpbj. Jlbh}<> "1" is ridiculous, yes, I began to write "true", in the Crystal Report editor can be very good to run, but side to the web display on the
Question, helpless, only so,:()

Five: Create a new Web project in WebLogic Workshop, right-click the project folder, select "New", select "Crystal", then automatically create a new one in the Web project root directory
CRYSTALREPORTVIEWERS10 directory, which has the page elements that show the Crystal report the system automatically adds the jar package needed to develop the Crystal Report, a total of 13 jar files (located in the Web_inf\lib directory),
and added some special labels for Crystal Reports (under WEB_INF\SRC)

VI: Special attention needs to be paid to the following changes in the Web.xml documents under Web_inf, Web.xml documents as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3. DTD ">
<!--the Web.xml file is a configuration file used to the behavior of WebLogic server.
In most cases, you won't 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 This because Struts takes the *last* mapping here as the extension to add to
The 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>


which

<context-param>
<param-name>crystal_image_uri</param-name>
<param-value>/webmis/crystalreportviewers10</param-value>
</context-param>
Very key, it set the Crystal Report dedicated CRYSTALREPORTVIEWERS10 path (fifth step automatically built Crystalreportviewers10 directory), if set wrong, the future click on the Crystal Report from the
Export and print buttons will be reported page errors, and Crystal report their own various pages, export and other icons can not be displayed normally.

Seven: the editor of the Crystal Report (suffix rpt) placed in a directory (I built a report directory, dedicated to the reporting file)

Eight: Can write the program:
1. Write the Reportinit class. function: Used for passing parameters BH. This class is critical, and is responsible for passing Java parameter values to the Crystal Report file.
2. Increase the showrpt operation in the page flow: In fact, it is an instance of a Reportinit class.
3. Write viewer.jsp file for dynamic display of Crystal Report.


I have the source code behind, please criticize correct.

The problems that exist:
1: Crystal Report exported to PDF file problems: Chinese characters appear as garbled, somehow? This problem has been bothering me for months.
2: Unable to print/Export page in Chinese: In the CRYSTALREPORTVIEWERS10\JS directory has a series of strings named JS file, I guess is used to internationalize the page, but I modified
Strings_zh,js,strings_en.js, no effect.
3: After displaying the Crystal report in the browser, close the display page and the Crystal Report is still connected to the database (the number of Oracle connections can be viewed by the select * from V$session). If the
To join a disconnected statement in viewer.jsp:
Viewer.dispose ();
Rptsource.dispose ();
Only one page of the report can be displayed. and printing and exporting do not work.

The above problem has been bothering me for a long time, although it does not affect the use, but the heart is not very cool. Hope to master the guidance.


SOURCE program:

/*
* Create date:2004-8-14 11:06
* Create by: Li Chunlei
* Purpose: Set the Crystal Report query parameters (currently supports 7 parameters of the query, 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 criteria
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 (); People
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 first parameter: number
Pfield1.setreportname ("");
Pfield1.setname ("BH");
Pfielddv1.setvalue (BH);
Pfielddv1.setdescription ("number");
Vals1.add (pfieldDV1);
Pfield1.setcurrentvalues (VALS1);
Set second parameter: Unit department
Pfield2.setreportname ("");
Pfield2.setname ("DW");
Pfielddv2.setvalue ("");
Pfielddv2.setdescription ("billing Department");
Vals2.add (pfieldDV2);
Pfield2.setcurrentvalues (VALS2);
Set third parameter: person
Pfield3.setreportname ("");
Pfield3.setname ("REN");
Pfielddv3.setvalue ("");
Pfielddv3.setdescription ("applicant, release person, operator, person in charge, etc.");
Vals3.add (pfieldDV3);
Pfield3.setcurrentvalues (VALS3);
Set a 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 a sixth query field
Pfield6.setreportname ("");
Pfield6.setname ("FIELD6");
Pfielddv6.setvalue ("");
Pfielddv6.setdescription ("query field 6: Change reason, work content, etc.");
Vals6.add (PFIELDDV6);
Pfield6.setcurrentvalues (VALS6);
Set 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 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);
Parameter fields and report associations
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 actions in the page flow:
/**
* @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 ("Success");
} else {
return new Forward ("false");
}
}
return new Forward ("false");
}



Viewer.jsp is used to dynamically display Crystal Reports. The contents of viewer.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.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 = (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 ();
%>



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.