在jsp页面中显示jfreechart [英] Displaying a jfreechart in a jsp page

查看:214
本文介绍了在jsp页面中显示jfreechart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在jsp页面中显示jfreechart图表.我写了如下代码-

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

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

其中,createCategoryChart()方法创建所需的jpg.它存储在eclipse文件夹中(我没有在文件名中添加任何路径).

where the createCategoryChart() method creates the required jpg. It is stored in the eclipse folder(i have not put any path in my filename).

我无法在jsp页面中查看图表,但是文件已创建.

I am not able to view the chart in the jsp page, but the file is created.

我在做什么错了?

推荐答案

我建议使用Servlet创建Chart.

I would suggest to use Servlet to create Chart.

JSP主要用于表示(视图).

JSP is mainly used for presentation (View).

创建一个servlet,该servlet会创建图表并将其作为响应发回.

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

从JSP调用Servlet.

Call Servlet from JSP.

>

这篇关于在jsp页面中显示jfreechart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆