Is it possible to call a servlet from css?

I am trying to move all images stored in a web application folder to a database. And call them a servlet. Is it possible to call a servlet from my css ?? or is there a way to call a remotely saved image file from css ??

I tried calling the servlet method from CSS. But failed. Is it possible to call such a method?

background-image: url (servlet / com.abc.servlet.GetImage? name = home & GetImage ('abc', '123'));

+4
source share
3 answers

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.

+2
source

Yes. As long as images have URLs, you can use them in your CSS.

For instance:

 background-image:url('/getimage.ashx?id=3'); 

You can even take one more step to reassign your URLs - you can use the same URLs that you have today, but your server processes the request and downloads files from the database.

One more tip: make sure you set the correct headers. You want to use the right type of content, and you probably want the images to be cached correctly on the client side.

+5
source

Yes. The CSS rule that the image indicates can contain any URL that the browser can parse and select:

 body { background-image: url(http://www.domain.com/servlets/my_servlet.jsp?argument=value) } 
+5
source

Source: https://habr.com/ru/post/1307638/


All Articles