Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invoke java method from JavaScript (not applet!)

 
Vlad Golodov
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to invoke a simple method from users site.
For example, it could be

public class Test{
public Test(){
}

public String getString(String parameter){
return "String";
}
}

From JS i need to getString from Test. My class will work with Oracle and will be located on a different ip-address.

Is it possible without Applet?
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To invoke a java method from javascript - have a look at http://www.rgagnon.com/howto.html
Then click on java and go to Javascript interaction.

Cheers,
Rachel
 
Vlad Golodov
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the good resourse, but i can't use applets in my task. Even if i would use applets, they wouldn't permit to make a call from other domain

Maybe there are more possibilities how to get variable from java to JS?

I have to check

if(variable == true)
1
else
2

And i have to take this variable from java located on different site. On client site can use only small JS code.
 
M. Gagnon
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using JSP, you might be able to do it with an expression. I did this once with ASP. For example, if your javascript code needs to use a JSP variable:

function myJsFunction () {
var jsVar = <%= jspVar %>;
}

The jspVar could be a value returned from a Java method. I haven't tried this in JSP, but it did work in ASP.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic