Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching in JavaFX WebView

We're developing a JavaFX 2.x application, which needs to provide some GIS support. We've come to conclusion that using GoogleMaps via an embedded WebView is the quickest option. The problem with it is that every time our application starts, the corresponding JavaScript libraries are downloaded. This makes development very difficult as it takes a couple of second before anything interactive can be done on the WebView panel.

The first thing that comes to mind is to have some sort of caching like web browsers do in order to store the libraries and read them locally when needed. How can this be achieved with WebView? Are there any alternatives to caching in our case?

Thanx.

like image 943
01es Avatar asked Aug 15 '12 11:08

01es


People also ask

What is WebView class in JavaFX?

WebView class is a part of JavaFX. WebView can create and manage a WebEngine and display its content. The associated WebEngine is created automatically at construction time and cannot be changed. WebView manages keyboard and mouse events and automatically adds a scrollbar to the WebView.

How to display JavaScript and CSS code inside a JavaFX application?

It is capable of showing CSS, HTML, and JAVASCRIPT code on the webView, inside the JavaFX application. To use this we need to import the library from the JavaFX while programming.

How do i Zoom in on a WebView in JavaFX?

WebView Zoom. It is possible to set the zoom level of a JavaFX WebView. For instance, you can specify that the WebView should always zoom in 25%, or zoom out 10% etc. Zooming in scales up or down all of the content displayed inside the WebView. You set the WebView zoom level via the setZoom() method.

What is JavaFX embedded browser?

JavaFX provides a web component that can be used as an embedded web browser in a JavaFX application. It is based on WebKit, which is an open source web browser engine. It supports:


1 Answers

The WebView component doesn't provide caching of web resources out of the box. However, it does utilize the java.net stack for network communications. What this means is you can install your own URL Handler that talks to a cache and serves resources from that cache. For example, put something like this block in your main() method before the JavaFX launch call:

URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
    public URLStreamHandler createURLStreamHandler(String protocol) {
        if ( "http".equals(protocol) ) {
            return new URLStreamHandler() {
                protected URLConnection openConnection(URL u) throws IOException {
                    if ( isCached(u) ) {
                        return new CachedStreamConnection(u);
                    }
                    return new MyURLConnection(u);
                }
            };
        }
        // Don't handle a non-http protocol, so just return null and let
        // the system return the default one.
        return null;
    }
});

Of course the devil is in the details. You should take the cache policies returned by the HTTP headers (like ETags) into consideration when storing a resource in your cache. Another consideration is HTML meta tags. Here is a good resource on caching.

You may also want to consider a cookie management system to complement this cache management system.

like image 109
disrvptor Avatar answered Oct 17 '22 23:10

disrvptor