001: /*
002: JOpenChart Java Charting Library and Toolkit
003: Copyright (C) 2001 Sebastian Müller
004: http://jopenchart.sourceforge.net
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: ChartServlet.java
021: Created on 2. September 2001, 14:44
022: */
023:
024: package de.progra.charting.servlet;
025:
026: import javax.servlet.*;
027: import javax.servlet.http.*;
028: import de.progra.charting.*;
029: import java.io.*;
030:
031: /**
032: * This Servlet returns image files of diagrams to be embedded in HTML/JSP
033: * pages. The Servlet expects the following parameters:<br>
034: * <ul>
035: * <li>"imageType" Parameter (either GET or POST), one of "gif", "jpeg", "png"
036: * - defines the image format to be returned
037: * <li>"chart" Attribute (stored in SessionContext), <code>DefaultChart</code>
038: * object to be rendered
039: * </ul>
040: * <p>This class will be extended to allow easier rendering without assembling
041: * the whole DefaultChart object. If the ChartServlet is deployed correctly
042: * you can embed it e.g. with
043: * <code><img src="/charting/ChartServlet?imageType=gif" alt="PieChart as GIF Image"></code>.
044: *
045: * @author mueller
046: * @version 1.0
047: */
048: public class ChartServlet extends HttpServlet {
049:
050: /** Initializes the servlet.
051: */
052: public void init(ServletConfig config) throws ServletException {
053: super .init(config);
054:
055: }
056:
057: /** Destroys the servlet.
058: */
059: public void destroy() {
060:
061: }
062:
063: /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
064: * @param request servlet request
065: * @param response servlet response
066: */
067: protected void processRequest(HttpServletRequest request,
068: HttpServletResponse response) throws ServletException,
069: java.io.IOException {
070:
071: String imgType = (String) request.getParameter("imageType");
072: DefaultChart chart = (DefaultChart) request.getSession(true)
073: .getAttribute("chart");
074:
075: //response.setContentType("image/"+imgType);
076: ServletOutputStream out = response.getOutputStream();
077: try {
078: if (chart != null) {
079: if (imgType == null)
080: ChartEncoder.createGIF(out, chart);
081: else if (imgType.equals("gif"))
082: ChartEncoder.createGIF(out, chart);
083: else if (imgType.equals("jpeg"))
084: ChartEncoder.createJPEG(out, chart);
085: else
086: ChartEncoder.createPNG(out, chart);
087: }
088: } catch (EncodingException p) {
089: throw new IOException(
090: "An Error occurred encoding the Image file.");
091: }
092:
093: /* output your page here
094: out.println("<html>");
095: out.println("<head>");
096: out.println("<title>Servlet</title>");
097: out.println("</head>");
098: out.println("<body>");
099:
100: out.println("</body>");
101: out.println("</html>");
102: */
103: out.close();
104: }
105:
106: /** Handles the HTTP <code>GET</code> method.
107: * @param request servlet request
108: * @param response servlet response
109: */
110: protected void doGet(HttpServletRequest request,
111: HttpServletResponse response) throws ServletException,
112: java.io.IOException {
113: processRequest(request, response);
114: }
115:
116: /** Handles the HTTP <code>POST</code> method.
117: * @param request servlet request
118: * @param response servlet response
119: */
120: protected void doPost(HttpServletRequest request,
121: HttpServletResponse response) throws ServletException,
122: java.io.IOException {
123: processRequest(request, response);
124: }
125:
126: /** Returns a short description of the servlet.
127: */
128: public String getServletInfo() {
129: return "Short description";
130: }
131:
132: }
|