请帮帮我解决servlet上的jasper报告问题,下面提到错误
SEVERE:为servlet JaspersJob_servlet分配异常 抛出java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JRRuntimeException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680) 在 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526) at java.lang.Class.getDeclaredConstructors0(Native Method)at java.lang.Class.privateGetDeclaredConstructors(未知来源)at java.lang.Class.getConstructor0(未知来源)at java.lang.Class.newInstance(未知来源)at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1149) 在 org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 在 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) 在 org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:606) 在 org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:489) 在java.lang.Thread.run(未知来源)
Servlet代码:
package jasper_job_Print;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JRDesignQuery;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
public class JaspersJob_servlet extends HttpServlet {
private static final long serialVersionUID = -3478873702336546526L;
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection con=null;
File reportFile = null;
JasperPrint jasperPrint = null;
ResultSet rs=null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cloudinterdataset", "root","cosmos123");
reportFile = new File("C:\\abd\\JasperJava\\report10.jasper");
String sql1 = "SELECT * FROM userdetails "
+ "ORDER BY Lastname ";
statement = con.createStatement();
rs = statement.executeQuery(sql1);
if (!reportFile.exists())
throw new JRRuntimeException(
"File WebappReport.jasper not found. The report design must be compiled first.");
} catch(Exception e) {
System.out.println("Exception : "+e);
}
try {
JRResultSetDataSource resultSetDataSource = new
JRResultSetDataSource(rs);
System.out.println("Filling Jasper PDF report...");
JasperReport jasperReport = (JasperReport) JRLoader
.loadObject(reportFile.getPath());
jasperPrint = JasperFillManager.fillReport(jasperReport,null,resultSetDataSource);
System.out.println("Done!");
if (jasperPrint != null)
{
JRExporter exporter = null;
exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,
jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
response.getOutputStream());
exporter.exportReport();
} else {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>JasperReports </title>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<span class=\"bold\">Empty response.</span>");
out.println("</body>");
out.println("</html>");
}
} catch(Exception e) {
System.out.println("Exception :"+e);
}
}
}
答案 0 :(得分:0)
这是一个非常简单的解决方案我没有把Jar Files放在我的项目库中。它现在正在工作,只是有时间发表我的答案