I’m trying to move all the images stored in web application folder to a database. And calling them with a servlet. Is it possible to call a servlet from my css ?? or is there any way to call a remotely stored image file from css??
I tried to call a servlet method from CSS.But couldn’t succeed. Is it possible to call a method like this?
background-image: url(servlet/com.abc.servlet.GetImage?name=home&GetImage(‘abc’,’123′));
It is possible. Just create an imageservlet like this example here. To the point just obtain the image as
InputStreamfrom DB byResultSet#getBinaryStream()and write it to theOutputStreamof the response as obtained byHttpServletResponse#getOutputStream()the usual Java IO way. Don’t forget to add the HTTP content type and content length headers. If you omit the content type, the browser don’t know what to do with the information. If you omit the content length, it will be sent with chunked transfer encoding, which is a tad less efficient.As to referencing the servlet in the CSS file, just specify the URL relative to the CSS file. This way you don’t need to worry about the context path. Determining the relative URL isn’t that hard, it works the same way as with accessing local disk filesystem paths in the command console.
cd ../../foo/bar/file.extand so on. You’ve ever learnt that at schools, yes?OK, assume that the imageservlet is located at
http://example.com/context/image?id=xand that the CSS file is located athttp://example.com/context/css/globalstyle.css(thus, the current folder iscss), then the right relative URL to the imageservlet from inside the CSS file would be:The
../goes a step backwards in the directory structure so that you go from the folderhttp://example.com/context/csstohttp://example.com/context. If you still have a hard time in figuring the right relative path, then let us know the absolute URL of both the servlet and the CSS file, then we’ll extract the correct relative path for you.