简体   繁体   中英

Getting image from servlet to JSP

I want to get a photo in my jsp pages. I implemented the servlet in this way (in doGet method):

{...

byte[] imageData = u.getFoto();

response.setContentType("image/jpg");

response.getOutputStream().write(imageData);

..}

where u is a User type.

My question is: how can I set the src path in my jsp page to retrieve the image from Servlet??

You would specify the mount point in your web.xml , with something like this:

<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 . Then, in your jsp you would just use an img tag pointing to the url-pattern specified above.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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