KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servlets > PdfServlet


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2005 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 185, Berry Street, Suite 6200
25  * San Francisco CA 94107
26  * http://www.jaspersoft.com
27  */

28 package servlets;
29
30 import datasource.*;
31 import net.sf.jasperreports.engine.*;
32
33 import java.io.*;
34 import java.util.*;
35 import javax.servlet.*;
36 import javax.servlet.http.*;
37
38
39 /**
40  * @author Teodor Danciu (teodord@users.sourceforge.net)
41  * @version $Id: PdfServlet.java,v 1.8 2005/07/14 12:40:23 ionutned Exp $
42  */

43 public class PdfServlet 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         File reportFile = new File(context.getRealPath("/reports/WebappReport.jasper"));
58         if (!reportFile.exists())
59             throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");
60
61         Map parameters = new HashMap();
62         parameters.put("ReportTitle", "Address Report");
63         parameters.put("BaseDir", reportFile.getParentFile());
64                     
65         byte[] bytes = null;
66
67         try
68         {
69             bytes =
70                 JasperRunManager.runReportToPdf(
71                     reportFile.getPath(),
72                     parameters,
73                     new WebappDataSource()
74                     );
75         }
76         catch (JRException e)
77         {
78             response.setContentType("text/html");
79             PrintWriter out = response.getWriter();
80             out.println("<html>");
81             out.println("<head>");
82             out.println("<title>JasperReports - Web Application Sample</title>");
83             out.println("<link rel=\"stylesheet\" type=\"text/css\" HREF=\"../stylesheet.css\" title=\"Style\">");
84             out.println("</head>");
85             
86             out.println("<body bgcolor=\"white\">");
87
88             out.println("<span class=\"bnew\">JasperReports encountered this error :</span>");
89             out.println("<pre>");
90
91             e.printStackTrace(out);
92
93             out.println("</pre>");
94
95             out.println("</body>");
96             out.println("</html>");
97
98             return;
99         }
100
101         if (bytes != null && bytes.length > 0)
102         {
103             response.setContentType("application/pdf");
104             response.setContentLength(bytes.length);
105             ServletOutputStream ouputStream = response.getOutputStream();
106             ouputStream.write(bytes, 0, bytes.length);
107             ouputStream.flush();
108             ouputStream.close();
109         }
110         else
111         {
112             response.setContentType("text/html");
113             PrintWriter out = response.getWriter();
114             out.println("<html>");
115             out.println("<head>");
116             out.println("<title>JasperReports - Web Application Sample</title>");
117             out.println("<link rel=\"stylesheet\" type=\"text/css\" HREF=\"../stylesheet.css\" title=\"Style\">");
118             out.println("</head>");
119             
120             out.println("<body bgcolor=\"white\">");
121     
122             out.println("<span class=\"bold\">Empty response.</span>");
123     
124             out.println("</body>");
125             out.println("</html>");
126         }
127     }
128
129
130 }
131
Popular Tags