KickJava   Java API By Example, From Geeks To Geeks.

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


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 PdfServlet 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.getPdf();
59         }
60
61         if (bytes != null && bytes.length > 0)
62         {
63             response.setContentType("application/pdf");
64             response.setContentLength(bytes.length);
65             ServletOutputStream ouputStream = response.getOutputStream();
66             
67             try
68             {
69                 ouputStream.write(bytes, 0, bytes.length);
70                 ouputStream.flush();
71             }
72             catch (IOException e)
73             {
74                 if (ouputStream != null)
75                 {
76                     try
77                     {
78                         ouputStream.close();
79                     }
80                     catch (IOException ex)
81                     {
82                     }
83                 }
84             }
85         }
86         else
87         {
88             response.setContentType("text/html");
89             PrintWriter out = response.getWriter();
90             out.println("<html>");
91             out.println("<body bgcolor=\"white\">");
92             out.println("<span class=\"bold\">Empty response.</span>");
93             out.println("</body>");
94             out.println("</html>");
95         }
96     }
97
98
99 }
100
Popular Tags