我有一个简单的碧玉报告,其中显示了数据库中的一些数据。jasper 报告在 ireport 设计器上运行良好。问题是当我尝试从 servlet 运行报告时。从 servlet 运行时,我试图将报告导出为 pdf。我包括依赖项,如
- jasperreports-4.6.0.jar
- itextpdf-5.1.1.jar
- commons-logging-1.1.1.jar
- commons-collections-3.2.1.jar
- commons-beanutils-1.8.3.jar
- commons-digester3-3.2.jar
- cglib-2.2.jar
我已将我的 report1.jasper 文件放入 /web/report1.jasper 这是我的 servlet 文件:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection connection;
ServletOutputStream servletOutputStream =response.getOutputStream();
InputStream reportStream =getServletConfig().getServletContext().getResourceAsStream("/report1.jasper");
HashMap parameterMap = new HashMap();
String stdID="510BCT2010";
String semester="1";
parameterMap.put("stdID", stdID);
parameterMap.put("semester", semester);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection ("jdbc:odbc:ACEM","","");
JasperRunManager.runReportToPdfStream(reportStream,servletOutputStream, parameterMap, connection);
connection.close();
response.setContentType("application/pdf");
servletOutputStream.flush();
servletOutputStream.close();
}
catch (Exception e)
{
// display stack trace in the browser
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
response.setContentType("text/plain");
response.getOutputStream().print(stringWriter.toString());
}
}
当我第一次运行 servlet 时,出现以下错误:
java.lang.NoClassDefFoundError: org/apache/commons/digester/Rule
net.sf.jasperreports.components.ComponentsExtensionsRegistryFactory.<clinit>(ComponentsExtensionsRegistryFactory.java:100)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:247)
net.sf.jasperreports.engine.util.JRClassLoader.loadClassForRealName(JRClassLoader.java:157)
net.sf.jasperreports.engine.util.JRClassLoader.loadClassForName(JRClassLoader.java:115)
net.sf.jasperreports.engine.util.ClassUtils.instantiateClass(ClassUtils.java:53)
net.sf.jasperreports.extensions.DefaultExtensionsRegistry.instantiateRegistry(DefaultExtensionsRegistry.java:237)
net.sf.jasperreports.extensions.DefaultExtensionsRegistry.loadRegistries(DefaultExtensionsRegistry.java:214)
net.sf.jasperreports.extensions.DefaultExtensionsRegistry.loadRegistries(DefaultExtensionsRegistry.java:162)
net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getRegistries(DefaultExtensionsRegistry.java:133)
net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getExtensions(DefaultExtensionsRegistry.java:105)
net.sf.jasperreports.engine.util.JRStyledTextParser.<clinit>(JRStyledTextParser.java:83)
net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:121)
net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:88)
net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:103)
net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:61)
net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:153)
net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:55)
net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:446)
net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:407)
net.sf.jasperreports.engine.JasperRunManager.runToPdfStream(JasperRunManager.java:201)
net.sf.jasperreports.engine.JasperRunManager.runReportToPdfStream(JasperRunManager.java:720)
com.reports.reportGenerator.doGet(reportGenerator.java:54)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
之后当我运行 servlet 时,出现以下错误
java.lang.NoClassDefFoundError: Could not initialize class net.sf.jasperreports.engine.util.JRStyledTextParser
net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:121)
net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:88)
net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:103)
net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:61)
net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:153)
net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:55)
net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:446)
net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:407)
net.sf.jasperreports.engine.JasperRunManager.runToPdfStream(JasperRunManager.java:201)
net.sf.jasperreports.engine.JasperRunManager.runReportToPdfStream(JasperRunManager.java:720)
com.reports.reportGenerator.doGet(reportGenerator.java:54)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
它说 java.lang.NoClassDefFoundError。我错过了一些依赖项吗?我的报告没有图表,只从数据库中获取数据