如何在JSP中包含JFreeChart servlet映像 [英] How can I include a JFreeChart servlet image in a JSP

查看:101
本文介绍了如何在JSP中包含JFreeChart servlet映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了几个使用Servlet动态生成图表的示例,使用JFreeChart,然后使用img标记在JSP中包含该图像。例如:

I have seen several examples of using a Servlet to dynamically generate a chart using JFreeChart, and subsequently including that image in a JSP using an img tag. For example:

<img src="/MyChartServlet" width="400" height="300" border="0" alt="" />

我使用JFreeChart生成图像的servlet效果很好,如果我调用它,我可以看到图像直接在浏览器中如下:

My servlet which generates the image using JFreeChart works great, and I can see the image if I call it directly in the browser as in:

http:/myurl/MyChartServlet?id=274

问题是我的JSP没有显示图像。实际上,JSP甚至没有调用servlet。我知道这是因为我没有看到调用servlet时出现的日志中的调试条目。

The problem is that my JSP does not display the image. In fact, the JSP is not even invoking the servlet. I know this because I do not see the debug entries in the log that appear when the servlet is called.

在Servlet中我使用JFreeChart ChartUtilities.writeChartAsJPEG()方法将图像写入输出流响应,因为我不想将图像写入磁盘。如上所述,servlet在直接调用时工作正常。

In the Servlet I am using the JFreeChart ChartUtilities.writeChartAsJPEG() method to write the image to the output stream of the response, because I do not want to write the image to disk. As mentioned the servlet works fine when called directly.

我缺少什么?或者有更好的方法吗?也许一个普通的旧对象可以生成图表,我可以在JSP中包含它?任何帮助将不胜感激。

What am I missing? Or is there a better way to do this? Maybe a plain old object can generate the chart and I can include that in the JSP? Any help would be appreciated.

推荐答案

你正在以正确的方式去做。您可能从您所处的上下文中遇到某种相对路径问题。尝试

You're going about it the right way. You may be having some kind of relative path issue from the context you're in. Try

<img src="http://<full path to your servlet>" ...

此外,您的示例中有一个?id = 274,但不是您的img SRC。如果需要,也可以将它放在那里。

Also, you have a ?id=274 in your example, but not in your img src. If that's required, put that in there as well.

如果你发布了servlet代码,这可能有所帮助,但也要确保你的内容类型设置正确servlet

If you posted your servlet code, that could help, but also make sure you have your content type set properly in your servlet

response.setContentType("image/jpeg");  

这篇关于如何在JSP中包含JFreeChart servlet映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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