It is possible. Just create a frame, for example this example here. To the point, just get the image as an InputStream from DB ResultSet#getBinaryStream() and write it in the OutputStream response, as received by HttpServletResponse#getOutputStream() usual way of Java IO . Remember to add HTTP content headers and content length headers. If you omit the content type, the browser does not know what to do with the information. If you omit the length of the content, it will be sent with encoding with a short transmission, which is less efficient.
As for the servlet link in the CSS file, just provide the relative URL in the CSS file. This way you do not need to worry about the context. Determining the relative URL is not so difficult, it works the same way as when accessing local disk file paths in the command console. cd ../../foo/bar/file.ext etc. Have you ever found out about this in schools, huh?
OK, suppose the imageervlet is located at http://example.com/context/image?id=x and that the CSS file is located at http://example.com/context/css/globalstyle.css (thus the current folder css ), and then the correct relative image URL inside the CSS file will be:
background-image: url('../image?id=123');
../ takes a step back in the directory structure, so you go from the http://example.com/context/css folder to http://example.com/context . If you still find it difficult to find the correct relative path, tell us the absolute URL of both the servlet and CSS file, then we will choose the correct relative path for you.
source share