2009-05-17 74 views
2

我在使用Eclipse的Crystal Report中遇到问题。java中的Crystal Report DB身份验证

我使用一个servlet通过编写观众对象,如响应呈现水晶报表查看器:

public class ReportViewer extends HttpServlet { 

    @SuppressWarnings("deprecation") 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
     try { 
      String reportName = "WEB-INF/includes/reports/"+request.getParameter("ReportName"); 
      ReportClientDocument clientDoc = (ReportClientDocument) request.getSession().getAttribute(reportName); 

      if (clientDoc == null) { 
       // Report can be opened from the relative location specified in the CRConfig.xml, or the report location 
       // tag can be removed to open the reports as Java resources or using an absolute path 
       // (absolute path not recommended for Web applications). 

      clientDoc = new ReportClientDocument(); 

       // Open report 
       clientDoc.open(reportName, OpenReportOptions._discardSavedData); 
       // Store the report document in session 
       ConnectionInfo info = new ConnectionInfo(); 
       info.setUserName("sa"); 
       info.setPassword("sa"); 
       Tables t = clientDoc.getDatabaseController().getDatabase().getTables(); 
       for (com.crystaldecisions.sdk.occa.report.data.ITable table : t) { 
        IConnectionInfo Ic = table.getConnectionInfo(); 
        Ic.setPassword("sa"); 
        Ic.setUserName("sa"); 
        table.setConnectionInfo(Ic);      
       } 
       request.getSession().setAttribute(reportName, clientDoc); 

      } 



        // Create the CrystalReportViewer object 
        CrystalReportViewer crystalReportPageViewer = new CrystalReportViewer(); 

        // set the reportsource property of the viewer 
        IReportSource reportSource = clientDoc.getReportSource();    
        crystalReportPageViewer.setReportSource(reportSource); 

        // set viewer attributes 
        crystalReportPageViewer.setOwnPage(true); 
        crystalReportPageViewer.setOwnForm(true); 

        // Apply the viewer preference attributes 



        // Process the report 
        crystalReportPageViewer.processHttpRequest(request, response, request.getSession(false).getServletContext(), null); 


     } catch (ReportSDKExceptionBase e) { 
      System.out.println(e); 
     } 
    } 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException { 
     doGet(request, response); 
    } 
} 

当我试图请求这个servlet重定向我的JDBC的登录页面,然后它去回到这个servlet。我需要通过在某处硬编码来避免jdbc登录步骤。请帮助我,每一个评论将不胜感激。

回答

1

你有没有过在https://wiki.sdn.sap.com/wiki/display/BOBJ/Crystal%20Reports%20Java%20%20SDK%20Samples

检查了维基有一个JSP示例中,您也许可以使用:

<%@ page import="com.crystaldecisions.sdk.occa.report.application.OpenReportOptions, 
       com.crystaldecisions.sdk.occa.report.application.ReportClientDocument, 
       com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat, 
       java.io.ByteArrayInputStream, 
       java.util.Calendar" 
%><% 
String reportPath; 
String db_username; 
String db_password; 
ReportClientDocument reportClientDocument; 
ByteArrayInputStream byteArrayInputStream; 
byte[] byteArray; 
int bytesRead; 

reportPath = request.getParameter("report_path"); 
db_username = request.getParameter("db_username"); 
db_password = request.getParameter("db_password"); 

/* 
* Instantiate ReportClientDocument and specify the Java Print Engine as the report processor. 
* Open a rpt file. 
*/ 

reportClientDocument = new ReportClientDocument(); 
reportClientDocument.setReportAppServer(ReportClientDocument.inprocConnectionString); 
reportClientDocument.open(reportPath, OpenReportOptions._openAsReadOnly); 


/* 
* Set the database logon for all connections in main report. 
*/ 

reportClientDocument.getDatabaseController().logon(db_username, db_password); 


/* 
* Retrieve PDF format of report and stream out to web browser. 
*/ 

byteArrayInputStream = (ByteArrayInputStream) reportClientDocument 
     .getPrintOutputController().export(ReportExportFormat.PDF); 

response.reset(); 

response.setHeader("Content-disposition", "inline;filename=crreport.pdf"); 
response.setContentType("application/pdf"); 

byteArray = new byte[1024]; 
while((bytesRead = byteArrayInputStream.read(byteArray)) != -1) { 
    response.getOutputStream().write(byteArray, 0, bytesRead); 
} 

response.getOutputStream().flush(); 
response.getOutputStream().close(); 

reportClientDocument.close(); 

%> 
+0

我知道这是一个非常晚了,但我有维护代码的一个旧代码。如果可以,请你帮助我。 'reportClientDocument.getDatabaseController().logon(db_username,db_password);'。我在这里给出的证书无关紧要,报告要求在浏览器中查看凭证。它说需要额外的信息,并要求输入数据库的用户名和密码。当我输入用户名,密码它的作品。如何避免在浏览器中为查看器输入用户名和密码。我在这花了一天,谷歌并没有真正的帮助 – developernaren 2015-11-04 08:47:41