转自:https://wenku.baidu.com/view/104156f9770bf78a65295462.html

 

1.

 

2.导入ireport的所有jar包和创建report包导入编译好的sample.jasper和sample.jrxml文件

 

3.testPDF.jsp文件

 1 <%@page import="net.sf.jasperreports.engine.JasperRunManager"%>
 2 <%@page import="java.sql.DriverManager"%>
 3 <%@page import="java.sql.Connection"%>
 4 <%@page import="java.util.HashMap"%>
 5 <%@page import="java.util.Map"%>
 6 <%@page import="java.io.File"%>
 7 <%@ page language="java" contentType="application/pdf; charset=UTF-8"
 8     pageEncoding="UTF-8"%>
 9 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
10 <html>
11 <head>
12 <meta http-equiv="Content-Type" content="application/pdf; charset=ISO-8859-1">
13 <title>Insert title here</title>
14 </head>
15 <body>
16 
17     <%
18         response.reset();
19         File reportFile = new File(this.getServletConfig().getServletContext().getRealPath(
20                 "/report/sample.jasper"));
21         String url = "jdbc:oracle:thin:@192.168.100.9:1521:Alpha";
22         Class.forName("oracle.jdbc.driver.OracleDriver");
23         Map parameters = new HashMap(); 
24         //"SQLSTR"是报表中定义的一个参数名称,其类型为String 型  
25         parameters.put("SQLSTR",
26                 "select * from employee where employee_id like 'Z%'");
27         Connection conn = DriverManager.getConnection(url, "MLP_BONDED",
28                 "password");
29         byte[] bytes = JasperRunManager.runReportToPdf(
30                 reportFile.getPath(), parameters, conn);
31         response.setContentType("application/pdf");
32         response.setContentLength(bytes.length);
33         ServletOutputStream outStream = response.getOutputStream();
34         outStream.write(bytes, 0, bytes.length);
35         outStream.flush();
36         outStream.close();
37         out.clear();
38         out = pageContext.pushBody();
39     %>
40 </body>
41 </html>

 

posted on 2016-11-22 14:34  Sharpest  阅读(663)  评论(0编辑  收藏  举报