Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  200] [ 7]  / answers: 1 / hits: 129751  / 13 Years ago, mon, july 11, 2011, 12:00:00

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


More From » java

 Answers
17

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>

[#91255] Friday, July 8, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrence

Total Points: 120
Total Questions: 115
Total Answers: 87

Location: England
Member since Fri, May 22, 2020
4 Years ago
;