gpt4 book ai didi

java - jasper 报告什么也不显示,并在 Netbeans 上收到错误 NullPointer 错误

转载 作者:行者123 更新时间:2023-12-01 14:04:31 25 4
gpt4 key购买 nike

report.java

     byte[] pdfbyte = null;
try {
dbcon connect = new dbcon();
ServletContext context = this.getServletConfig().getServletContext();
String compiledFileName = context.getRealPath("/jasper/report3.jasper");
System.out.print(compiledFileName);
// String var = request.getParameter("var").toString();
//String res = JOptionPane.showInputDialog(null, "What is your name?", "Enter your name",JOptionPane.QUESTION_MESSAGE);

File reportFile = new File(compiledFileName);
HashMap parameters = new HashMap();
// // parameters.put("itemName", var);
parameters.put("ReportTitle", "MyReport");
// parameters.put("BaseDir", reportFile.getParentFile());
// parameters.put("EmpNo", 32504);
JasperPrint jasperPrint = null;
Connection conn = connect.dbstate().getConnection();
try {

try {
jasperPrint = JasperFillManager.fillReport(compiledFileName, parameters, conn);
pdfbyte = JasperExportManager.exportReportToPdf(jasperPrint);
} catch (Exception e) {
System.out.print("E" + e);
}
} catch (Exception ex) {
System.out.println("*** Could not create compiled jasper file: " + ex.getMessage());
} finally {
try {
connect.dbstate().close();
} catch (SQLException ex) {
System.out.println("could not close databse after filling the report ");
}
}
ServletOutputStream outstream = response.getOutputStream();

response.setContentType("application/pdf");
response.setContentLength(pdfbyte.length);
response.setHeader("Content-disposition", "inline; filename=\"Report.pdf\"");
outstream.write(pdfbyte);
} catch (Exception e) {
System.out.print(e);
e.printStackTrace();
}

图书馆

enter image description here

我在 Netbeans 上遇到以下错误

SEVERE: A web application registered the JBDC driver [oracle.jdbc.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Ejava.lang.NullPointerExceptionjava.lang.NullPointerExceptionjava.lang.NullPointerException
at jasper.Report.processRequest(Report.java:63)
at jasper.Report.doGet(Report.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:619)

最佳答案

我建议您预编译报告,就像 @MadProgrammer 所说的那样。要将报告从 servlet 发送到客户端(在网络浏览器上预览或直接下载),我使用以下命令:

...
// trying get the report
try {

// getting de real path
String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath(reportFileName);

//
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

// here you can specify the condition to print, if conditionVoolean is true it will do a direct download
if (conditionBoolean) {
fileName = "your file name to direct download";
httpServletResponse.addHeader("Content-disposition", "attachment; filename=" + fileName +".pdf");
}

//
byte[] bytes;

// get the filled report in bytes
bytes = JasperRunManager.runReportToPdf(jasperReport, parameter, conn);

if (bytes != null && bytes.length > 0) {

// specifying the type of file/content to browser
httpServletResponse.setContentType("application/pdf");

httpServletResponse.setContentLength(bytes.length);

ServletOutputStream ouputStream = httpServletResponse.getOutputStream();

ouputStream.write(bytes, 0, bytes.length);

ouputStream.flush();

// closing the stream line
ouputStream.close();
}
FacesContext.getCurrentInstance().responseComplete();
}
catch (JRException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
...

Att:对葡萄牙语评论感到抱歉。这是我的母语。

希望对你有帮助^^

关于java - jasper 报告什么也不显示,并在 Netbeans 上收到错误 NullPointer 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19023585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com