File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Invoke java method from JavaScript (not applet!) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Invoke java method from JavaScript (not applet!)" Watch "Invoke java method from JavaScript (not applet!)" New topic

Invoke java method from JavaScript (not applet!)

Vlad Golodov
Ranch Hand

Joined: Jul 05, 2004
Posts: 50
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?

SCJP, SCJWSD prepairing<br /><a href="" target="_blank" rel="nofollow"></a>
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
I think you are going to have a JavaScript security problem on the browser side. Using JS to load stuff from different IP addresses was determined to be a gaping security hole many years ago. The same restrictions apply to applets trying to open connections to different addresses.

You can have a server-side function effectively relay the query and send the results back to JS.

Vlad Golodov
Ranch Hand

Joined: Jul 05, 2004
Posts: 50
Thanks for the answer

It's not possible to execute something on user's site. I can put only small JS code into his page.

Problem is that i need to show two types of buttons on site. One type will be shown if statement from my server returns true, another if false.

It's possible to write a jsp or servlet that will return the result on my site. Then from JS i can execute URL and get the text of document like variable. But i don't know how to execute url and get this text in JS. Maybe it's not possible too. I have searched about it, but found nothing helpfull.
[ July 06, 2004: Message edited by: Vlad Golodov ]
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
just put an iframe with a fixed source to your JSP that will display the correct buttons. I see no other way.
[ July 09, 2004: Message edited by: Anton Golovin ]

Anton Golovin ( SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
I agree. Here's the link:
subject: Invoke java method from JavaScript (not applet!)
It's not a secret anymore!