KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > ebank > XlsServlet


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2005 Teodor Danciu teodord@users.sourceforge.net
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  * Teodor Danciu
24  * 173, Calea Calarasilor, Bl. 42, Sc. 1, Ap. 18
25  * Postal code 030615, Sector 3
26  * Bucharest, ROMANIA
27  * Email: teodord@users.sourceforge.net
28  */

29 package net.sf.jasperreports.ebank;
30
31 import java.io.*;
32 import java.util.*;
33 import javax.servlet.*;
34 import javax.servlet.http.*;
35
36
37 /**
38  * @author Teodor Danciu (teodord@users.sourceforge.net)
39  * @version $Id: PdfServlet.java,v 1.5 2005/02/03 15:31:47 teodord Exp $
40  */

41 public class XlsServlet extends HttpServlet
42 {
43
44
45     /**
46      *
47      */

48     public void service(
49         HttpServletRequest request,
50         HttpServletResponse response
51         ) throws IOException, ServletException
52     {
53         byte[] bytes = null;
54
55         ReportBean reportBean = (ReportBean)request.getSession().getAttribute("reportBean");
56         if (reportBean != null)
57         {
58             bytes = reportBean.getXls();
59         }
60
61         if (bytes != null && bytes.length > 0)
62         {
63             response.setContentType("application/vnd.ms-excel");
64             response.setHeader("Content-Disposition", "inline; filename=\"file.xls\"");
65             response.setContentLength(bytes.length);
66             ServletOutputStream ouputStream = response.getOutputStream();
67             
68             try
69             {
70                 ouputStream.write(bytes, 0, bytes.length);
71                 ouputStream.flush();
72             }
73             catch (IOException e)
74             {
75                 if (ouputStream != null)
76                 {
77                     try
78                     {
79                         ouputStream.close();
80                     }
81                     catch (IOException ex)
82                     {
83                     }
84                 }
85             }
86         }
87         else
88         {
89             response.setContentType("text/html");
90             PrintWriter out = response.getWriter();
91             out.println("<html>");
92             out.println("<body bgcolor=\"white\">");
93             out.println("<span class=\"bold\">Empty response.</span>");
94             out.println("</body>");
95             out.println("</html>");
96         }
97     }
98
99
100 }
101
Popular Tags