一、新建web工程,导入jasperreports所需的jar包,配置web.xml
1
<servlet>
2
<servlet-name>PdfServlet</servlet-name>
3
<servlet-class>net.sf.jasperreports.j2ee.servlets.PdfServlet</servlet-class>
4
</servlet>
5
<servlet-mapping>
6
<servlet-name>PdfServlet</servlet-name>
7
<url-pattern>/servlets/pdf</url-pattern>
8
</servlet-mapping> 二、 编译jrxml文件,生成jasper文件
1
/**//*
2
* JasperReports - Free Java Reporting Library.
3
* Copyright (C) 2001 - 2013 Jaspersoft Corporation. All rights reserved.
4
* http://www.jaspersoft.com
5
*
6
* Unless you have purchased a commercial license agreement from Jaspersoft,
7
* the following license terms apply:
8
*
9
* This program is part of JasperReports.
10
*
11
* JasperReports is free software: you can redistribute it and/or modify
12
* it under the terms of the GNU Lesser General Public License as published by
13
* the Free Software Foundation, either version 3 of the License, or
14
* (at your option) any later version.
15
*
16
* JasperReports is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU Lesser General Public License for more details.
20
*
21
* You should have received a copy of the GNU Lesser General Public License
22
* along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23
*/
24
package servlets;
25
26
import java.io.IOException;
27
import java.io.PrintWriter;
28
29
import javax.servlet.ServletContext;
30
import javax.servlet.ServletException;
31
import javax.servlet.http.HttpServlet;
32
import javax.servlet.http.HttpServletRequest;
33
import javax.servlet.http.HttpServletResponse;
34
35
import net.sf.jasperreports.engine.JRException;
36
import net.sf.jasperreports.engine.JasperCompileManager;
37
38
39
/** *//**
40
* @author Teodor Danciu (teodord@users.sourceforge.net)
41
* @version $Id: CompileServlet.java 5876 2013-01-07 19:05:05Z teodord $
42
*/
43
public class CompileServlet extends HttpServlet
44

{
45
46
47
/** *//**
48
*
49
*/
50
public void service(
51
HttpServletRequest request,
52
HttpServletResponse response
53
) throws IOException, ServletException
54
{
55
ServletContext context = this.getServletConfig().getServletContext();
56
57
response.setContentType("text/html");
58
PrintWriter out = response.getWriter();
59
60
try
61
{
62
JasperCompileManager.compileReportToFile(context.getRealPath("/reports/WebappReport.jrxml"));
63
}
64
catch (JRException e)
65
{
66
out.println("<html>");
67
out.println("<head>");
68
out.println("<title>JasperReports - Web Application Sample</title>");
69
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
70
out.println("</head>");
71
72
out.println("<body bgcolor=\"white\">");
73
74
out.println("<span class=\"bnew\">JasperReports encountered this error :</span>");
75
out.println("<pre>");
76
77
e.printStackTrace(out);
78
79
out.println("</pre>");
80
81
out.println("</body>");
82
out.println("</html>");
83
84
return;
85
}
86
87
out.println("<html>");
88
out.println("<head>");
89
out.println("<title>JasperReports - Web Application Sample</title>");
90
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
91
out.println("</head>");
92
93
out.println("<body bgcolor=\"white\">");
94
95
out.println("<span class=\"bold\">The JRXML report design was successfully compiled.</span>");
96
97
out.println("</body>");
98
out.println("</html>");
99
}
100
101
102
}
103
三、填充数据
1
/**//*
2
* JasperReports - Free Java Reporting Library.
3
* Copyright (C) 2001 - 2013 Jaspersoft Corporation. All rights reserved.
4
* http://www.jaspersoft.com
5
*
6
* Unless you have purchased a commercial license agreement from Jaspersoft,
7
* the following license terms apply:
8
*
9
* This program is part of JasperReports.
10
*
11
* JasperReports is free software: you can redistribute it and/or modify
12
* it under the terms of the GNU Lesser General Public License as published by
13
* the Free Software Foundation, either version 3 of the License, or
14
* (at your option) any later version.
15
*
16
* JasperReports is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU Lesser General Public License for more details.
20
*
21
* You should have received a copy of the GNU Lesser General Public License
22
* along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23
*/
24
package servlets;
25
26
import java.io.File;
27
import java.io.IOException;
28
import java.io.PrintWriter;
29
import java.util.HashMap;
30
import java.util.Map;
31
32
import javax.servlet.ServletContext;
33
import javax.servlet.ServletException;
34
import javax.servlet.http.HttpServlet;
35
import javax.servlet.http.HttpServletRequest;
36
import javax.servlet.http.HttpServletResponse;
37
38
import net.sf.jasperreports.engine.JRException;
39
import net.sf.jasperreports.engine.JRRuntimeException;
40
import net.sf.jasperreports.engine.JasperFillManager;
41
import net.sf.jasperreports.engine.JasperPrint;
42
import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;
43
import datasource.WebappDataSource;
44
45
46
/** *//**
47
* @author Teodor Danciu (teodord@users.sourceforge.net)
48
* @version $Id: FillServlet.java 5876 2013-01-07 19:05:05Z teodord $
49
*/
50
public class FillServlet extends HttpServlet
51

{
52
53
54
/** *//**
55
*
56
*/
57
public void service(
58
HttpServletRequest request,
59
HttpServletResponse response
60
) throws IOException, ServletException
61
{
62
ServletContext context = this.getServletConfig().getServletContext();
63
64
response.setContentType("text/html");
65
PrintWriter out = response.getWriter();
66
67
try
68
{
69
String reportFileName = context.getRealPath("/reports/WebappReport.jasper");
70
File reportFile = new File(reportFileName);
71
if (!reportFile.exists())
72
throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");
73
74
Map parameters = new HashMap();
75
parameters.put("ReportTitle", "Address Report");
76
parameters.put("BaseDir", reportFile.getParentFile());
77
78
JasperPrint jasperPrint =
79
JasperFillManager.fillReport(
80
reportFileName,
81
parameters,
82
new WebappDataSource()
83
);
84
85
request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
86
}
87
catch (JRException e)
88
{
89
out.println("<html>");
90
out.println("<head>");
91
out.println("<title>JasperReports - Web Application Sample</title>");
92
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
93
out.println("</head>");
94
95
out.println("<body bgcolor=\"white\">");
96
97
out.println("<span class=\"bnew\">JasperReports encountered this error :</span>");
98
out.println("<pre>");
99
100
e.printStackTrace(out);
101
102
out.println("</pre>");
103
104
out.println("</body>");
105
out.println("</html>");
106
}
107
108
out.println("<html>");
109
out.println("<head>");
110
out.println("<title>JasperReports - Web Application Sample</title>");
111
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
112
out.println("</head>");
113
114
out.println("<body bgcolor=\"white\">");
115
116
out.println("<span class=\"bold\">The compiled report design was successfully filled with data.</span>");
117
118
out.println("</body>");
119
out.println("</html>");
120
}
121
122
123
}
124