0

jfreechartjspページにグラフを表示したい。私は次のようにコードを書きました -

...
<%
ChartCreator chart = new ChartCreator();
chart.createCategoryChart();
%>
<img src = "chart.jpg"/>

createCategoryChart()メソッドは必要な jpg を作成します。それはEclipseフォルダーに保存されます(ファイル名にパスを入れていません)。

jsp ページでグラフを表示できませんが、ファイルは作成されています。

私は何を間違っていますか?

4

1 に答える 1

5

サーブレットを使用してグラフを作成することをお勧めします。

JSPは主にプレゼンテーション(表示)に使用されます。

チャートを作成するサーブレットを作成し、それを応答として送り返します。

import javax.imageio.ImageIO;


protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        OutputStream out = response.getOutputStream(); /* Get the output stream from the response object */
        response.setContentType("image/png"); /* Set the HTTP Response Type */
        ChartCreator chart = new ChartCreator(); // Create chart
        chart.createCategoryChart(); 
        ChartUtilities.writeChartAsPNG(out, chart, 400, 300);/* Write the data to the output stream */
    }

JSPからサーブレットを呼び出します。

<img src="/drawChartServlet?type=myDesiredChart&width=..and other processed parameters" ..>>

于 2012-05-23T05:22:00.290 に答える