Search code examples
javafx-2riaurl-parameters

Is it possible to get URL parameters in JavaFX?


I'm building a JavaFX application which will run in browser. Is it possible to get the app URL, like localhost/Java/MyApp/dist/index.html?x=123, in JavaFX?

I need to receive that "x" parameter.


Solution

  • You can get url parameters from the page: Get escaped URL parameter

    function getURLParameter(name) {
        return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
    }
    

    Once you have the parameters you can pass the parameters to the embedded JavaFX application using the JavaFX deployment toolkits DTJava.js functions. See section 7.3.3 Pass Parameters to a Web Application of the JavaFX deployment guide.

    <!-- Example 7-7 Pass Parameters to an Embedded Application -->
    
    <head>
        <script type="text/javascript" src="http://java.com/js/dtjava.js"></script>
        <script>
            function deployIt() {
                // deployment guide sample modified by me to show 
                // getting the zipcode parameter from the url instead of hardcoding it.
                //var zipcode = 95054; 
                var zipcode = getURLParameter("zipcode");
    
                dtjava.embed(
                    {            id: "myApp",
                                url: "Map.jnlp",
                              width: 300,
                             height: 200,
                        placeholder: "place",
                        params: {
                                  mode: "streetview",
                                  zip: zipcode
                        }
                    },
                    { javafx: "2.1+" },
                    {}
                );
            }
            dtjava.addOnloadCallback(deployIt);
        </script>
    </head>
    <body>
        <div id="place"></div>
    </body>
    

    http://docs.oracle.com/javafx/2/deployment/deployment_toolkit.htm#BABJHEJA

    As the JavaFX deployment guide says, to access parameters in the application code, use the getParameters() method of the Application class. For example:

    String zipcode = app.getParameters().getNamed("zip");