简体   繁体   English

从servlet到JSP获取图像

[英]Getting image from servlet to JSP

I want to get a photo in my jsp pages. 我想在我的jsp页面中获取照片。 I implemented the servlet in this way (in doGet method): 我以这种方式(在doGet方法中)实现了servlet:

{...

byte[] imageData = u.getFoto();

response.setContentType("image/jpg");

response.getOutputStream().write(imageData);

..}

where u is a User type. 其中uUser类型。

My question is: how can I set the src path in my jsp page to retrieve the image from Servlet?? 我的问题是:如何在jsp页面中设置src路径以从Servlet检索图像?

You would specify the mount point in your web.xml , with something like this: 您可以在web.xml指定挂载点,如下所示:

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/img/myservlet</url-pattern>
</servlet-mapping>

That will take the servlet named MyServlet and mount it to /img/myservlet . 这将使用名为MyServlet的servlet并将MyServlet安装到/img/myservlet Then, in your jsp you would just use an img tag pointing to the url-pattern specified above. 然后,在jsp中,您将只使用指向上面指定的url-patternimg标签。

<img src="/img/myservlet" />

Note: if your webapp is not mounted to /, you will also need to specify the contextPath for the application in the path. 注意:如果您的Web应用程序未安装到/,则还需要在路径中指定应用程序的contextPath。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM