Retrieve images from a folder outside web root folder using servlet

走远了吗. 提交于 2019-11-29 08:42:11

You need to understand how HTTP and HTML work:

  1. The browser asks for an HTML page (first request)
  2. The server sends back HTML (and HTML only), containing 3 <img src="..."/> tags
  3. The browser sends a request to get the bytes of the first image (second request)
  4. The server sends back the bytes of the first image
  5. The browser sends a request to get the bytes of the second image (third request)
  6. The server sends back the bytes of the second image
  7. The browser sends a request to get the bytes of the third image (fourth request)
  8. The server sends back the bytes of the third image

So, you need a servlet or JSP which generates the HTML page, containing all your <img src="..."/> tags. Each of this tag should have the following form:

<img src="imageServlet?imageId=564"/>

And you need a second servlet, mapped to imageServlet, which reads the bytes of the image identified by the imageId parameter value from the file system, and write those bytes to the response output stream.

them I need them to be a part of a JSP which will have other content,

Use image tag in JSP

<image src="/context-root/YourServlet?param=value" alt="blah blah"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!