KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > servlet > ChartServlet


1 /*
2     JOpenChart Java Charting Library and Toolkit
3     Copyright (C) 2001 Sebastian Müller
4     http://jopenchart.sourceforge.net
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20     ChartServlet.java
21     Created on 2. September 2001, 14:44
22 */

23
24 package de.progra.charting.servlet;
25
26 import javax.servlet.*;
27 import javax.servlet.http.*;
28 import de.progra.charting.*;
29 import java.io.*;
30
31 /**
32  * This Servlet returns image files of diagrams to be embedded in HTML/JSP
33  * pages. The Servlet expects the following parameters:<br>
34  * <ul>
35  * <li>"imageType" Parameter (either GET or POST), one of "gif", "jpeg", "png"
36  * - defines the image format to be returned
37  * <li>"chart" Attribute (stored in SessionContext), <code>DefaultChart</code>
38  * object to be rendered
39  * </ul>
40  * <p>This class will be extended to allow easier rendering without assembling
41  * the whole DefaultChart object. If the ChartServlet is deployed correctly
42  * you can embed it e.g. with
43  * <code>&lt;img SRC="../../../../../charting/ChartServlet?imageType=gif" alt="PieChart as GIF Image"&gt;</code>.
44  *
45  * @author mueller
46  * @version 1.0
47  */

48 public class ChartServlet extends HttpServlet {
49    
50     /** Initializes the servlet.
51     */

52     public void init(ServletConfig config) throws ServletException {
53         super.init(config);
54
55     }
56
57     /** Destroys the servlet.
58     */

59     public void destroy() {
60
61     }
62
63     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
64     * @param request servlet request
65     * @param response servlet response
66     */

67     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
68         throws ServletException, java.io.IOException JavaDoc {
69         
70             String JavaDoc imgType = (String JavaDoc)request.getParameter("imageType");
71             DefaultChart chart = (DefaultChart)request.getSession(true).getAttribute("chart");
72
73             //response.setContentType("image/"+imgType);
74
ServletOutputStream out = response.getOutputStream();
75             try {
76                 if(chart != null) {
77                     if(imgType == null)
78                         ChartEncoder.createGIF(out, chart);
79                     else if(imgType.equals("gif"))
80                         ChartEncoder.createGIF(out, chart);
81                     else if(imgType.equals("jpeg"))
82                         ChartEncoder.createJPEG(out, chart);
83                     else
84                         ChartEncoder.createPNG(out, chart);
85                 }
86             } catch(EncodingException p) {
87                 throw new IOException("An Error occurred encoding the Image file.");
88             }
89         
90         /* output your page here
91         out.println("<html>");
92         out.println("<head>");
93         out.println("<title>Servlet</title>");
94         out.println("</head>");
95         out.println("<body>");
96
97         out.println("</body>");
98         out.println("</html>");
99         */

100             out.close();
101     }
102
103     /** Handles the HTTP <code>GET</code> method.
104     * @param request servlet request
105     * @param response servlet response
106     */

107     protected void doGet(HttpServletRequest request, HttpServletResponse response)
108     throws ServletException, java.io.IOException JavaDoc {
109         processRequest(request, response);
110     }
111
112     /** Handles the HTTP <code>POST</code> method.
113     * @param request servlet request
114     * @param response servlet response
115     */

116     protected void doPost(HttpServletRequest request, HttpServletResponse response)
117     throws ServletException, java.io.IOException JavaDoc {
118         processRequest(request, response);
119     }
120
121     /** Returns a short description of the servlet.
122     */

123     public String JavaDoc getServletInfo() {
124         return "Short description";
125     }
126
127 }
128
Popular Tags