How to reuse application constants written inside Java classes in javascript?

Is there a way to reuse constants public static finaldefined inside Java classes with javascript files?

Or is there a better way to define such constants so that they can be reused by both java and JavaScript?

+3
source share
2 answers

A very simple solution would be to save your configuration as JSON on the server side, and then add the server in the HTML response to the client.

The server can read JSON at startup and store it in a singleton object.

+3
source

, java:

SimpleApplet.class:

import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet
{
    public static final int someInt = 1;
    public static int getInt()
    {
        return someInt;
    }
}

JavaScript:

...
<SCRIPT>
function GetFromJava() { 
   alert("The value of SimpleApplet.someInt is " + document.simple.someInt + "\n"
         "Returned value from SimpleApplet.getInt() is " +  document.simple.getInt());
   }
</SCRIPT>
...
<APPLET CODE="SimpleApplet.class" NAME="simple"  HEIGHT=0 WIDTH=0>
...

, Oracle.

0

All Articles