帮助从Servlet到JSP页面获取图像 [英] Help getting image from Servlet to JSP page

查看:84
本文介绍了帮助从Servlet到JSP页面获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前必须生成一个显示字符串文本的图像,我需要在Servlet上创建这个图像,然后以某种方式将图像传递给JSP页面,以便它可以显示它。我试图避免保存图像,而是以某种方式将图像流式传输到JSP。

I currently have to generate an image that displays the text of a string, i need to make this image on a Servlet and then somehow pass the image to a JSP page so that it can display it. I'm trying to avoid saving the image, and instead somehow stream the image to the JSP.

自从我开始以来,我还没有找到生成图像的方法找到如何将图像从Servlet传递到JSP adn卡住了。

I haven't found a way of generating the image since i started with finding how to pass an image from the Servlet to the JSP adn got stuck.

编辑:
jsp页面已经制作完成并且不是由servlet,我必须将图像传递给已经存在的jsp

The jsp page is already made and isn't created by the servlet, i have to pass the image into an already existing jsp

任何帮助都表示赞赏。

推荐答案

您需要将图像作为字节数组写入响应的输出流。这样的事情:

You need to write the image as a byte array to the response's output stream. Something like this:

byte[] imageBytes = getImageAsBytes();

response.setContentType("image/jpeg");
response.setContentLength(imageBytes.length);

response.getOutputStream().write(imageBytes);

然后在JSP中你只需使用标准的 img 元素:

Then in you JSP you just use a standard img element:

<img src="url to your servlet">

这篇关于帮助从Servlet到JSP页面获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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