calling java methods in javascript code

Nadya Nux :

i created a java class content method return a String, my question is how to call this function in my javascript code to use the returned value from the java method. I want to call client-side Java code embedded in browser.

here is an exemple of what im talking about:

in my webpage i have a javascript code, here is some of it:

    function createChartControl(htmlDiv1)
{
    // Initialize Gantt data structures
    //project 1
    var parentTask1 = new GanttTaskInfo(1, "Old code review", new Date(2010, 5, 11), 208, 50, "");
......................

i want to create a java class content methods to provide data to this javascript function "GanttTaskInfo". for exemple function to get name, get id and date. well i think this time im clear :D i searched a way to call java methods in javascript, and i found applets as you said, but i think its not usefull to me. thanks again

zacheusz :

When it is on server side, use web services - maybe RESTful with JSON.

  • create a web service (for example with Tomcat)
  • call its URL from JavaScript (for example with JQuery or dojo)

When Java code is in applet you can use JavaScript bridge. The bridge between the Java and JavaScript programming languages, known informally as LiveConnect, is implemented in Java plugin. Formerly Mozilla-specific LiveConnect functionality, such as the ability to call static Java methods, instantiate new Java objects and reference third-party packages from JavaScript, is now available in all browsers.

Below is example from documentation. Look at methodReturningString.

Java code:

public class MethodInvocation extends Applet {
    public void noArgMethod() { ... }
    public void someMethod(String arg) { ... }
    public void someMethod(int arg) { ... }
    public int  methodReturningInt() { return 5; }
    public String methodReturningString() { return "Hello"; }
    public OtherClass methodReturningObject() { return new OtherClass(); }
}

public class OtherClass {
    public void anotherMethod();
}

Web page and JavaScript code:

<applet id="app"
        archive="examples.jar"
        code="MethodInvocation" ...>
</applet>
<script language="javascript">
    app.noArgMethod();
    app.someMethod("Hello");
    app.someMethod(5);
    var five = app.methodReturningInt();
    var hello = app.methodReturningString();
    app.methodReturningObject().anotherMethod();
</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling object methods in Javascript

From Dev

Javascript: calling methods in classes with this

From Dev

Calling Java code from JavaScript (Spring boot)

From Dev

XPages - Calling Java methods

From Dev

Calling the methods in java

From Dev

Calling Java Methods in XSLT

From Java

Calling multiple methods in Java

From Java

Calling the methods in a class in Java

From Dev

Calling methods on objects Java

From Dev

Javascript registry for calling methods and objects

From Dev

Calling JavaScript methods from typescript

From Java

Calling methods on string literals (Java)

From Java

Calling methods of "parent" component in Java

From Java

Calling two Java methods asynchronously

From Dev

Calling Java methods with React locally

From Dev

Calling methods in a Java Fx ActionEvent

From Dev

Generics in Java preventing calling of methods

From Dev

Calling methods for my java class

From Java

Calling Javascript function of .js file from java GWT code

From Dev

Java Code using Methods?

From Dev

Calling class methods inside javascript class

From Dev

Clojure: calling a sequence of methods on a Java object

From Java

Java: Printing triangles and spaces by calling methods

From Dev

Programatically calling all methods in a Java class

From Dev

Compiler error with calling Generic methods java

From Dev

Calling methods of a Java subclass using Jython

From Java

Calling Java Methods from Shell Scripts

From Dev

calling objects from other methods in java

From Dev

Using variables for creating objects and calling methods in Java