tags:

views:

95

answers:

5

Hi JavaScript experts,

I was wondering how to import my java packages into a js file and then call class methods on created object.

The reason I need to import is I wish to get data from a connection to my database, regarding dates and I have a class that lets me do so. So I need to import the Package, then create a class object, then call methods on my object.

I would appreciate your help, Thank you, John

A: 

You'll have to store the Java as a Java server page on your server to do the connection, and feed javascript the database data via ajax, because javascript is incapable of connecting to any protocols other than http, file and ftp.

Andrew Dunn
A: 

Calling java methods from javascript is not possible.

JSP file:

function doSomething {
   <% SomeClass.someMethod(); %>
}

<%!
class SomeClass {
  public static void someMethod() {
    // 
  }
}
%>

someMethod will not run on invocation of doSomething but it will run when jsp generates response.

Jaydeep
Not true. Sorry.
mkoistinen
That blanket statement is not true. There are several ways for accessing Java methods from JavaScript. However the original question is not clear enough to be able determinate which way (if any at all) should be used.
RoToRa
I think it's not very straightforward to make a fool out of the questioner's knowledge gap.
Manuel Faux
I did not intended to make him fool. Added few more lines. Does this makes sense now?
Jaydeep
@Jaydeep: Making up syntax that doesn't work, doesn't prove that it's not possible.
RoToRa
A: 

You can use JSP and use Java basic types as parameter for your javascript.

Import your Java classes in JSP, e.g:

<%@page import="com.acme.MyClass"%>

Then in your JS which also in the JSP:

<script type="text/javascript">
var f = function(param){
 console.log(param);
}

f('<%= MyClass.foo().toString() %>');
</script>

Would that be something you're looking at?

Hope that helps.

jpartogi
Thank you,I beleive this helps I'll try it
John
Don't forget to vote it up and accept it if it works ;)
jpartogi
+1  A: 

I think solution for you problem is DWR technology. As I guessed you think that it is too easy to invoke Java methods(Server side) from JavaScript(Client side). DWR is exactly what you are looking for :)

Zango
A: 

Actually you CAN do this. You can create a Java Applet with public methods. Once you add this to your page you can access these methods via JavaScript. I'll see if I can dig up a reference for you.

Here's one! http://www.google.co.uk/m/url?client=safari&amp;ei=ONaZTNCFLZiPjAfMqovBAw&amp;hl=en&amp;oe=UTF-8&amp;q=http://java.sun.com/products/plugin/1.3/docs/jsobject.html&amp;ved=0CBwQFjAE&amp;usg=AFQjCNE39TUH5rTny7bnwvmYlmD85IWsfg

I've done this myself, years ago. It works well. I don't know if I would do it again for the reason you specify, but it is possible.

Also in addition to this method and the many other fine suggestions, have a look at GWT. http://code.google.com/webtoolkit/overview.html

mkoistinen
That is probably not going to helpful. The original poster is mentioning database access, which usually is not sensibly possible in a Java Applet.
RoToRa
@rotora It's better than your suggestions. And I can imagine some scenarios where this IS the most useful suggestion here.
mkoistinen