最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I import my Java packages into a js file - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited Sep 22, 2010 at 9:58 John asked Sep 22, 2010 at 9:51 JohnJohn 691 gold badge1 silver badge2 bronze badges 4
  • 5 java != javascript REMEMBER IT! – Randy the Dev Commented Sep 22, 2010 at 9:53
  • 12 java is to javascript what ham is to hamsters ;) – tim_yates Commented Sep 22, 2010 at 10:00
  • Also, Java (usually) runs on the server, JavaScript on the client. They can't interact directly with each other. You'll probably need to learn the basics of web development and/or client/server development before you can actually explain to us what you want to do. – RoToRa Commented Sep 22, 2010 at 10:00
  • ** java.equals(javascript)** will always return false – Jigar Joshi Commented Sep 22, 2010 at 10:24
Add a comment  | 

6 Answers 6

Reset to default 8

The questions does not specify that this is a browser-based question. Java ships with Rhino since version 6. So, on the off chance that someone wants to know how to do this - e.g., using JavaScript as a scripting language for 3rd party tools like soapUI or PTC Integrity - it's incredibly simple.

importPackage(java.io);

Check out Wikipedia:Rhino (JavaScript engine) as a good starting point.

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.

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 :)

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.

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 link!

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

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.

发布评论

评论列表(0)

  1. 暂无评论