Generate image and display it inside JSP along with other content

岁酱吖の 提交于 2019-12-20 07:29:57

问题


I'm using JFreeChart to generate a dynamic chart depending on the user input. I have a JSP with some textbox and combobox, the user makes the input and submits it, and the Action process it, generating an image of a chart. I need to display this image on the same JSP as before, below the textbox/combobox.

If I use response.setContentType("image/jpeg"); etc... then I get a page with the image alone. I thought of saving the image to a file and then access it with <img >, but I'm not sure that will work (need to save it to WebContent and I may not be able to access it always?).

Is there a way to somehow cache the image and then access it inside the JSP through an <img> or something? Maybe JFreeChart has an easy way to do what I want?

If it matters, I'm also using struts and spring on my webapp.

Thanks in advance.


回答1:


I've not tried it, but you might look into org.jfree.chart.imagemap and a suitable URL generator from org.jfree.chart.urls. An outline of implementing a PieURLGenerator is illustrated here.




回答2:


Well, if you generate the image on the server side, you could always just store it in a temp directory using something like a UUID to generate a unique name for it, and concatenating the image file extension on the end of it.

Make sure that the directory the image is generated is accessible on the webserver, and then send the URL path to the image file on the server back to the JSP using ajax (Direct Web Remoting), for display using Javascript.

Just make sure you also have a chron job or service to clear the older files out of the directory now and again.




回答3:


You should have a servlet that can create the image you want solely from the URL. The URL can then contain an id, which maps back to an object in your program containing raw data in memory. The servlet then generates the image and returns it.

You can then simply set the url of the image in your current web page in Javascript, and it should be loaded.

This is because JSP's are character oriented which do not lend well to binary data so you need to have a servlet do it.



来源:https://stackoverflow.com/questions/9927303/generate-image-and-display-it-inside-jsp-along-with-other-content

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