问题描述
我想使用servlet,.jasper文件创建pdf格式的报告,但是有一个例外:
I want to create a report in pdf format by using servlet, .jasper file,but there is a exception:
--------------------------------------------------------------------------------
type Exception report
message Servlet execution threw an exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException:Servlet执行引发异常
javax.servlet.ServletException: Servlet execution threw an exception
根本原因
java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JasperRunManager one.Patient.doGet(Patient.java:46) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JasperRunManager one.Patient.doGet(Patient.java:46) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note Apache中提供了根本原因的完整堆栈跟踪 Tomcat/7.0.34日志.
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.
servlet代码是:
servlet code is:
package one;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.HashMap;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
/**
* Servlet implementation class Patient
*/
@WebServlet("/Patient")
public class Patient extends HttpServlet implements Servlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Patient() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Connection con=DBCon.getConnection();
InputStream inS=getServletContext().getResourceAsStream("/report-src/ganeshaji.jashper");
OutputStream outS=response.getOutputStream();
response.setContentType("application/pdf");
try {
JasperRunManager.runReportToPdfStream(inS, outS, new HashMap<String, Object>(),con);
outS.flush();
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
response.setContentType("text/html");
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
pls.为我提供解决方案
pls. provide me solution
推荐答案
通过调查根本原因
java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JasperRunManager
似乎您错过了jasper jar或版本错误,不支持JasperRunManager.
it seems like you either miss the jasper jar or have the wrong version which doesn't support JasperRunManager.
在类路径中包含适当的jar将解决您的问题.
Including the proper jar in the class path will solve your issue.
请下载这些罐子:
itext-4.2.0.jar
itextpdf-5.3.4.jar
jasperreports-4.0.1.jar
jasperreports-fonts-4.0.0.jar
并编写类似以下代码:
try {
FileInputStream fis = new FileInputStream(YOURREPORTPATH.jasper");
BufferedInputStream bufferedInputStream = new BufferedInputStream(fis);
jasperReport = (JasperReport) JRLoader.loadObject(bufferedInputStream);
} catch (Exception e2) {
e2.printStackTrace();
}
,然后在填充参数和数据源之后,创建pdf如下:
and then after filling the parameters and the datasource create pdf like this:
JasperExportManager.exportReportToPdfFile(jasperPrint, OUTPUTFILENAME+".pdf");
谢谢...
777先生
这篇关于通过使用jasper和servlet以pdf格式创建报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!