2012-05-23 65 views
0

我想在jsp頁面中顯示jfreechart圖表。我寫的代碼如下 -在jsp頁面中顯示jfreechart

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

其中createCategoryChart()方法創建所需的jpg。它存儲在eclipse文件夾中(我沒有在我的文件名中放置任何路徑)。

我無法在jsp頁面查看圖表,但創建了該文件。

我在做什麼錯?

回答

5

我會建議使用Servlet來創建圖表。

JSP主要用於演示(View)。

創建一個創建圖表並將其作爲響應發回的servlet。

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調用Servlet。

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