PDA

View Full Version : Call Java methods from javascript



cabster
21 Jun 2010, 12:19 PM
Hello all,

I made a GWT and EXT GWT test application and it worked oke for me.
Now i want to use GWT components on a normal javascript + php website.
Problem is that i can't call Java classes in in javascript.

I have aome code bellow..is this possible?

My php file (this is not working....documentation siad that it should work)


<script type="text/javascript" language="javascript" src="phptester/phptester.nocache.js"></script>
<script type="text/javascript">
var test = @com.fnk.client.PhpTester::test()();
</script>




My java code


public class PhpTester implements EntryPoint {
private static final int STATUS_CODE_OK = 200;
/**
* This is the entry point method.
*/
public void onModuleLoad() {

}

public void test(){
DialogBox d = new DialogBox();
d.setText("LOOOOOOOOOOOOOOOOOOO");
d.setHTML("assdafasdfasdfasdf<br/> asdasdasdaassdafasdfasdfasdf<br/> asdasdasdaassdafasdfasdfasdf<br/> asdasdasda");
// d.setSize("300px", "200px");
d.show();
}

}



I just want to call the test method from javascript. Does anybody know how to do this?

sven
21 Jun 2010, 12:24 PM
Think about it. It cannot work.

Make your method static.

cabster
21 Jun 2010, 12:36 PM
Well it was static before but still didn't work.

And is it possible to call a non static method? ( something like -> var test = new @com.fnk.client.PhpTester::test()(); )

And if you see what i'm doing wrong please tell me.

sven
21 Jun 2010, 1:04 PM
test should be static. And as test returns nothing, you dont need a vairable for it.


And yes zyou can call methods on instances, if you have a reference to the instance. Same as in java.

cabster
21 Jun 2010, 1:25 PM
I changed the test method to:


public static String test(String test) {
DialogBox d = new DialogBox();
d.setText("Text");
d
.setHTML(test + "<br/> asdasdasdaassdafasdfasdfasdf<br/> <br/> asdasdasda");
d.show();
return test + " new test";
}



And my javascript now looks like this:



<script type="text/javascript" language="javascript" src="phptester/phptester.nocache.js"></script>
<script type="text/javascript">
var test = @com.fnk.client.PhpTester::test(Ljava/lang/String)("Hello sven ");
//var test2 = this.@com.fnk.client.PhpTester::STATUS_CODE_OK;
//alert("--> " + test2);
</script>



and i still i get this error:


reference to undefined XML name @com
[Break on this error] var test = @com.fnk.client.PhpTest...st(Ljava/lang/String)("Hello sven ");

cabster
21 Jun 2010, 1:26 PM
I changed the test method to:


public static String test(String test) {
DialogBox d = new DialogBox();
d.setText("Text");
d
.setHTML(test + "<br/> asdasdasdaassdafasdfasdfasdf<br/> <br/> asdasdasda");
d.show();
return test + " new test";
}



And my javascript now looks like this:



<script type="text/javascript" language="javascript" src="phptester/phptester.nocache.js"></script>
<script type="text/javascript">
var test = @com.fnk.client.PhpTester::test(Ljava/lang/String)("Hello sven ");
//var test2 = this.@com.fnk.client.PhpTester::STATUS_CODE_OK;
//alert("--> " + test2);
</script>



and i still i get this error:


reference to undefined XML name @com
[Break on this error] var test = @com.fnk.client.PhpTest...st(Ljava/lang/String)("Hello sven ");

cabster
22 Jun 2010, 2:41 AM
Ehm..i added a message but admin would check it before adding the message to this topic? Why is it not added?

EDIT:

i made the method static ( it was like that before) But still didn't work.
can you explain how to do that? + how to instanciate a method?

sven
22 Jun 2010, 6:05 AM
sorry, i did not see that you directly call jsni from javascript. That is not going to work.

You need to export a javascript metods with jsni that internly calls the static method

cabster
22 Jun 2010, 11:34 AM
Hello Sven,

Thank you for the info..but could you give me an example?
I just need to know how to call a method from my javascript.
Could you give me a JNSI example?

thank you