Q:

CSS Background Image From Database

How can I use an image from a database as a CSS background-image?
I can get an image from the resource library (file), but not the DB.
<h:outputStylesheet>
   body {
      background-image:url("#{resource[imageBean.getImage().getUrl()]}"); 
   }
</h:outputStylesheet>

resource[imageBean.getImage().getUrl()
Is there a way I can get the URL of the image that is in the imageBean?
I display DB images using an application scoped bean: 
Displaying images from MySQL database in JSF datatable

A:

Use a normal servlet.
@WebServlet("/image")
public class ImageServlet extends HttpServlet {

    @EJB
    private ImageService service;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Image image = service.find(request.getParameter("id"));
        response.setContentType(image.getContentType());
        response.setContentLength(image.getContent().length);
        response.setHeader("Content-Disposition", "inline;filename=\"" + URLEncoder.encode(image.getFilename(), "UTF-8") + "\"");
        response.getOutputStream().write(image.getContent());
    }

}

body {
   background-image:url("#{request.contextPath}/image?id=#{bean.imageId}"); 
}

See also:

Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag

