public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e1) {
e1.printStackTrace();
System.out.println("数据库驱动错误!");
}
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/crmsure";
try {
conn = DriverManager.getConnection(url, "root", "admin");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接错误!");
}
InputStream stream = Jreport.class
.getResourceAsStream("/myreport.jasper");
Map paramers = new HashMap();
paramers.put("Name", "客户信息");
byte[] bytes = null;
try {
bytes = JasperRunManager.runReportToPdf(stream, null, conn);
} catch (JRException e) {
e.printStackTrace();
System.out.println("jasper解析错误!");
}
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
}