Displaying a jfreechart in a jsp page

前端 未结 1 1390
傲寒
傲寒 2021-01-28 18:27

I want to display a jfreechart chart in a jsp page. I have written code as follows -

...
<%
ChartCreator chart = new ChartCreator();
chart.create         


        
相关标签:
1条回答
  • 2021-01-28 19:28

    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" ..>

    0 讨论(0)
提交回复
热议问题