KickJava   Java API By Example, From Geeks To Geeks.

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


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.JRXlsAbstractExporter;
43 import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
44
45 /**
46  * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
47  * @version $Id: AbstractXlsServlet.java 1479 2006-11-10 18:48:35 +0200 (Fri, 10 Nov 2006) shertage $
48  */

49 public abstract class AbstractXlsServlet 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             JRXlsAbstractExporter exporter = getXlsExporter();
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             exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
78             exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
79
80             try
81             {
82                 exporter.exportReport();
83             }
84             catch (JRException e)
85             {
86                 throw new ServletException JavaDoc(e);
87             }
88
89             byte[] bytes = baos.toByteArray();
90             
91             if (bytes != null && bytes.length > 0)
92             {
93                 response.setContentType("application/xls");
94                 response.setHeader("Content-Disposition", "inline; filename=\"file.xls\"");
95                 response.setContentLength(bytes.length);
96                 ServletOutputStream JavaDoc ouputStream = response.getOutputStream();
97
98                 try
99                 {
100                     ouputStream.write(bytes, 0, bytes.length);
101                     ouputStream.flush();
102                 }
103                 finally
104                 {
105                     if (ouputStream != null)
106                     {
107                         try
108                         {
109                             ouputStream.close();
110                         }
111                         catch (IOException JavaDoc ex)
112                         {
113                         }
114                     }
115                 }
116             }
117 // else
118
// {
119
// response.setContentType("text/html");
120
// PrintWriter out = response.getWriter();
121
// out.println("<html>");
122
// out.println("<body bgcolor=\"white\">");
123
// out.println("<span class=\"bold\">Empty response.</span>");
124
// out.println("</body>");
125
// out.println("</html>");
126
// }
127
}
128         else
129         {
130             response.setContentType("application/xls");
131             response.setHeader("Content-Disposition", "inline; filename=\"file.xls\"");
132
133             JRXlsAbstractExporter exporter = getXlsExporter();
134             exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
135             
136             OutputStream JavaDoc ouputStream = response.getOutputStream();
137             exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
138
139             exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
140             exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
141
142             try
143             {
144                 exporter.exportReport();
145             }
146             catch (JRException e)
147             {
148                 throw new ServletException JavaDoc(e);
149             }
150             finally
151             {
152                 if (ouputStream != null)
153                 {
154                     try
155                     {
156                         ouputStream.close();
157                     }
158                     catch (IOException JavaDoc ex)
159                     {
160                     }
161                 }
162             }
163         }
164     }
165
166     
167     /**
168      *
169      */

170     protected abstract JRXlsAbstractExporter getXlsExporter();
171
172     
173 }
174
Popular Tags