使用servlet将动态映像传递给JSP [英] Pass dynamic image to JSP with servlet
问题描述
我有一个桌面应用程序,用于创建图形2D对象,将其粘贴在面板中并绘制它。我试图使用servlets和jsps将此应用程序转换为网页。我已经在网上阅读了2天,无法解决如何做到这一点,我发现每个例子似乎都遗漏了一个重要的部分。我不想将图像保存到文件,因为每个用户将获得一个新的/不同的图像。
I have a desktop app that creates a graphics 2D object, sticks it in a panel and draws it. I am trying to convert this app to a webpage using servlets and jsps. I have been reading online for 2 days and can't wrap my head around how to do this, each example I find seems to leave out an important piece. I don't want to save the image to file because each user will get a new/different image.
我可以在servlet中创建图像以及其他响应变量并同时将它们传递给jsp吗?然后使用类似$ {response.image}的东西来调用图像。这似乎更可取,但似乎不起作用。
Can I create the image in the servlet along with the other response variables and pass them to the jsp at the same time? Then call the image using something like ${response.image}.This seems be preferable but doesn't seem to work.
或者我是否只需要一个单独的servlet图像(这看起来更难)?另外我如何准备要在HTML中用作图像的Graphics2D对象?
Or do I need a separate servlet just for the image (this seems harder)? Also how do I prepare the Graphics2D object to be used as an image in HTML?
推荐答案
您需要了解它是webbrowser谁必须根据检索到的HTML代码中找到的< img>
元素的网址下载所请求的图片,并且不网络服务器必须以某种方式在生成的HTML代码中内联图像的原始内容。
You need to understand that it's the webbrowser who has got to download the invididual images based on the URLs of the <img>
elements found in the retrieved HTML code and that it's not the webserver who has got to inline the image's raw content in the produced HTML code somehow.
你真的需要为此创建一个独立的图像servlet来监听那些特定的URL < img>
元素。您可以通过在生成HTML代码期间在请求查询字符串中提供唯一的图像idenfitier或请求路径信息来使servlet可重用。
You really need to create a standalone image servlet for this which listens on those specific URLs of the <img>
elements. You can make the servlet reuseable by providing an unique image idenfitier in the request query string or request path info during generating the HTML code.
例如
<img src="imageServlet?param1=value1¶m2=value2" />
带
@WebServlet("/imageServlet")
public class ImageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Create image based on request.getParameter() information.
// Set proper content type by response.setContentType().
// Write image to response.getOutputStream().
}
}
参见:
- 如何从JSP页面中的数据库中检索和显示图像? - 遵循类似的方法
- How to retrieve and display images from a database in a JSP page? - follows a similiar approach
See also:
这篇关于使用servlet将动态映像传递给JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!