Seen the amazing size reduction of WebP pictures (about 28% smaller than "pngcrushed" PNG for lossless + alpha pictures), we'd like to serve WebP pictures to browsers supporting WebP.
How can I detect from a Java webapp server if the client's browser is supporting WebP?
There's been a question here about how to do it from JavaScript:
But I'd like to know how to do it from Java. If doing from Java means calling JavaScript on the client side then I'd like to know how to to that.
You can't determine web browser features using Java alone. Java/JSP runs in webserver, not in webbrowser. You really need to use JavaScript for this. You can use the code for this which you already found on the related question. Easiest what you can do, is to let JS check on page load if there isn't already some cookie present indicating that the browser supports WebP and then do the feature detection accordingly and finally set a long living cookie by document.cookie
and reload the page by location.reload()
.
In the server side, you'd just have to check for the presence of the cookie and/or its value by HttpServletRequest#getCookies()
in servlet or ${cookie}
in JSP before sending/setting the appropriate image URL.
Other than setting a cookie, you can always let JS to send the data as request parameters of an ajax request, but this means that you have to do it on every request or session.