KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > j2ee > servlets > PdfServlet


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 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  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.j2ee.servlets;
29
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.util.List JavaDoc;
34
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.ServletOutputStream JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39
40 import net.sf.jasperreports.engine.JRException;
41 import net.sf.jasperreports.engine.JRExporterParameter;
42 import net.sf.jasperreports.engine.export.JRPdfExporter;
43
44
45 /**
46  * @author Teodor Danciu (teodord@users.sourceforge.net)
47  * @version $Id: PdfServlet.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
48  */

49 public class PdfServlet extends BaseHttpServlet
50 {
51
52
53     /**
54      *
55      */

56     public void service(
57         HttpServletRequest JavaDoc request,
58         HttpServletResponse JavaDoc response
59         ) throws IOException JavaDoc, ServletException JavaDoc
60     {
61         List JavaDoc jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
62
63         if (jasperPrintList == null)
64         {
65             throw new ServletException JavaDoc("No JasperPrint documents found on the HTTP session.");
66         }
67         
68         Boolean JavaDoc isBuffered = Boolean.valueOf(request.getParameter(BaseHttpServlet.BUFFERED_OUTPUT_REQUEST_PARAMETER));
69         if (isBuffered.booleanValue())
70         {
71             JRPdfExporter exporter = new JRPdfExporter();
72             exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
73
74             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
75             exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
76             
77             try
78             {
79                 exporter.exportReport();
80             }
81             catch (JRException e)
82             {
83                 throw new ServletException JavaDoc(e);
84             }
85
86             byte[] bytes = baos.toByteArray();
87             
88             if (bytes != null && bytes.length > 0)
89             {
90                 response.setContentType("application/pdf");
91                 response.setContentLength(bytes.length);
92                 ServletOutputStream JavaDoc ouputStream = response.getOutputStream();
93
94                 try
95                 {
96                     ouputStream.write(bytes, 0, bytes.length);
97                     ouputStream.flush();
98                 }
99                 finally
100                 {
101                     if (ouputStream != null)
102                     {
103                         try
104                         {
105                             ouputStream.close();
106                         }
107                         catch (IOException JavaDoc ex)
108                         {
109                         }
110                     }
111                 }
112             }
113 // else
114
// {
115
// response.setContentType("text/html");
116
// PrintWriter out = response.getWriter();
117
// out.println("<html>");
118
// out.println("<body bgcolor=\"white\">");
119
// out.println("<span class=\"bold\">Empty response.</span>");
120
// out.println("</body>");
121
// out.println("</html>");
122
// }
123
}
124         else
125         {
126             response.setContentType("application/pdf");
127
128             JRPdfExporter exporter = new JRPdfExporter();
129             exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
130             
131             OutputStream JavaDoc ouputStream = response.getOutputStream();
132             exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
133
134             try
135             {
136                 exporter.exportReport();
137             }
138             catch (JRException e)
139             {
140                 throw new ServletException JavaDoc(e);
141             }
142             finally
143             {
144                 if (ouputStream != null)
145                 {
146                     try
147                     {
148                         ouputStream.close();
149                     }
150                     catch (IOException JavaDoc ex)
151                     {
152                     }
153                 }
154             }
155         }
156     }
157
158
159 }
160
Popular Tags