how to call method from java file in javascript? - java

I am new in JavaScript. I want to know how to call a simple method in Java file from JavaScript program. Could you please give me a simple example as well?
Thank you

Javascript will not be able to access server side java code directly.
It will need to make an ajax call to access a method or make a http call.
If you have applets you will be able to access the applet java code.
See example here
http://download.oracle.com/javase/1.5.0/docs/guide/plugin/developer_guide/js_java.html

When you say "java file" I presume you mean a Java applet. In that case, say you have this applet code on your page:
<applet id="myapplet" class="MyApplet.class" ... />
If you wanted to call the helloWorld() method in that applet, you simply need to do this:
<script type="text/javascript>
document.getElementById['myapplet'].helloWorld();
</script>

Related

call applet method from javascript

i want to call a method startApp() ,located in the java applet file from a java script file.
i tried the following method
<APPLET name="drops" code="BigDrops.class" width=700 height=650 ></APPLET>
document.drops.startApp();
but it shows "has no method startApp()" when i looked in the console of the browser
But the strange fact i noticed is that this problem is occured in all computers except mine..the files are loaded in the dropbox.
When i run the applet using the dropbox url from my computer, it works fine
tell me why it does not work and also give the solution
LiveConnect calls from JavaScript are limited under some security level from 7u21 and 7u25.

Javascript connection with a java applet

I have a java applet with a main class at Eclipse IDE! I would like to call a function on a javascript. I have a variable for example x and i would like it to give it at the javascript and i want to receive another variable from the javascript for example y on my java applet.
Here is an example of how to invoke javascript from java Applet here and how to invoke java Applet from javascript here
Your question a little unclear about exact setup. So I give a couple answers:
If you have JavaScript function and want to execute function in Java, maybe this help: https://developer.mozilla.org/en-US/docs/Rhino
If you have JavaScript run in web browser and web browser also load applet, look here: http://docs.oracle.com/javase/tutorial/deployment/applet/invokingJavaScriptFromApplet.html
If you have Java Applet run in Eclipse IDE and JavaScript run in website in other location, you must make Applet provide HTTP(s) server and use XmlHttpRequest in JavaScript for call applet. For this you must use polling from JavaScript to Applet as Applet cannot call function on JavaScript this way, but JavaScript can connect to Applet.
If you have different setup from three mentioned here, please post and maybe we can find better answer.

Taking a "screenshot" of a java applet

No. Not pressing PRT Screen. But here is what I want. And I need to know if it can be done and by what means :D
Okay so I have a java applet that runs and displays a walking man.
I need it so when I access a script (in some web scripting language) it takes a "screenshot" of that applet and then saves it on the server. Is this possible? and by what scripting means could I do it?
Thanks in advance!
If there are heavyweight components they might not come out, but the obvious way is:
Create a java.awt.image.BufferedImage
Call createGraphics to create a Graphics instance.
Call update(Graphics) on the applet.
Find a tutorial for converting the BufferedImage to a wire file format.
POST back to the web server.
You can use LiveConnect to connect both ways between Java and JavaScript. It's usually easy to add Java code to an applet simply by adding another reference to the archive attribute of the applet tag.
There are plenty of tutorials on the various parts. I've never done it myself.

how can you make sure that your applet isn't being used outside a specific page url?

I have a java applet, I want to make sure that nobody use it outside a specific url. How can I achieve that?
The applet connects to a Java server for data exchange. I want to check on the server side the page url that contains the applet. Is that possible?
I have a java applet, I want to make sure that nobody use it outside a specific url. How can I achieve that?
You can't.
You can make it less easy.
Plain Java is trivially decompiled and manipulated by any decent developer. Even after obfuscation, it wouldn't be very difficult to circumvent any protection; changing "boolean checkAppletOrigin()" to always return true is not very difficult.
Think about what you are trying to achieve, and you might be able to find an alternative solution.
In your applet you can call the applet method getCodeBase() which will give you what URL the applet is running on pass that along with the data passed to the server that will give you a check that the applet is being run from where you wanted.
There are a few problems such that the web traffic to the server if not encrypted or protect somehow could be spoofed and this check could be gotten around. But it would be much harder to do then just reposting the applet somewhere new.

load ActiveX object in Applet

I have a web application that processes events and audio received from a specialised microphone. The audio is processed by a Java applet that runs in the web page, but other events (microphone connected, microphone disconnected, microphone button pressed) are handled by an ActiveX object.
The ActiveX object traps these events and calls JavaScript code to handle them
<!-- Load the ActiveX control -->
<object id="PhilipsSpeechMikeCtrl" width="0" height="0" tabindex="-1"
classid="CLSID:AAA44754-CC81-4692-91AF-7064E58EB22A"
standby="Loading Philips SpeechMike component..."
type="application/x-oleobject">
</object>
<script type="text/javascript">
// This is Microsofts javascript way of trapping ActiveX object events.
function PhilipsSpeechMikeCtrl::SPMEventDeviceConnected(deviceID) {
// Call JavaScript code to handle the microphone connected event
}
function PhilipsSpeechMikeCtrl::SPMEventDeviceDisconnected(deviceID) {
// Call JavaScript code to handle the microphone disconnected event
}
function PhilipsSpeechMikeCtrl::SPMEventButton(deviceID, eventId) {
// Call JavaScript code to handle the microphone button pressed event
}
</script>
Of course a problem with this approach is that it's completely IE dependent. I would prefer to load the ActiveX object within the applet, trap the events there and handle the events either within the applet, or JavaScript code called from the applet. This should then enable me to run the app in any browser that supports applets.
I'm not entirely sure how to go about implementing the solution I've proposed above, any suggestions?
Update: I realise the solution I've proposed above would still be IE dependent, that's fine. My immediate goal is to support all browsers on Windows.
It has been suggested that instead of using ActiveX, I could use JNI (or JNA) to access the DLLs underlying the ActiveX object. However, I don't actually want to call the functions in the DLLs, I want the DLLs to call me, i.e. register an event handler.
Thanks,
Don
ActiveX are not supported by an another browser than IE, so there is no way for your application to support all browsers, even on Windows only.
An attempt (plugin) to port ActiveX under Firefox 1 was made, but wasn't really useful so as far as I know, there is today no "emulation" solution to your question.
Sorry...
(see here for Mozilla comments)
JACOB is supposed to let you call COM from Java. It looks like it supports events too.
You can probably access the dlls in the activeX component directly,
so you can write a jni wrapper that calls the native functions,
and then build a signed applet that can get permission to use jni.
Check this:
http://www.raditha.com/java/jni/
Ahh. You can do want you desire, but may have to eschew Javascript and instead leverage VBScript. It is about the ability to send "events" between two components.
You can use use JavaScript to directly call public methods in the applet or access public variables. JavaScript treats the embedded applet as an object. In the applet tag give the applet a name id.
Consider the example below where the applet has a method public void myMethodInMyApplet();
The HTML page would look something like :
<APPLET CODE="MyApplet.class"
width=200 height=200
name=counter ID=counter>
</APPLET>
<script type="text/javascript">
// This is Microsofts javascript way of trapping ActiveX object events.
function PhilipsSpeechMikeCtrl::SPMEventDeviceConnected(deviceID) {
document.applets[0].myMethodInMyApplet();
}
</script>
Wouldn't that still be Windows- or even IE-dependent, given that Java applets are executed on the client side? Just wondering...
You will obviously have to pass the events twice if you want them to end up in JavaScript.
There is a version of SWT that can be used in applets and can embed ActiveX controls. There are also commercial libraries like Coroutine who can do this as well (and are smaller in jar size). Someone else mentioned JACOB here, which would be another choice.
So, use any of these components to wrap your ActiveX control. These libraries will call a Java method when a registered event occurs.
To pass events from Java to JavaScript, you can use the netscape.javascript.JSObject class which is supported by all major browsers.
If you have the source code for the COM component, it might be a good idea to rewrite it to use JNI, as COM wrappers use up a lot of resources (which is especially important in applets), and most probably there is also some overhead inside the COM component for COM interop.
is activexobjects always hitting the acivex sites like activex.microsoft.com

Categories