Displaying a jfreechart in a jsp page

故事扮演 提交于 2019-12-02 10:54:34

I would suggest to use Servlet to create Chart.

JSP is mainly used for presentation (View).

Create a servlet which creates the chart and send back it as response.

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 */
    }

Call Servlet from JSP.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!