How can I include a JFreeChart servlet image in a JSP

风流意气都作罢 提交于 2019-12-20 03:09:08

问题


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="" />

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

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.

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.

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.


回答1:


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

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.

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");  


来源:https://stackoverflow.com/questions/12354614/how-can-i-include-a-jfreechart-servlet-image-in-a-jsp

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