简体   繁体   中英

Call a Java method from Javascript

Need to call a Java method whenever I click an HTML button. Basically I need to send over some text to a Java class when I click a button. I have tried applets to achieve this behaviour but didn't manage.

Thanks

UPDATE Solution: Java Web services

There is a better way to do it,

DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible.

DWR is Easy Ajax for Java

See the demo

DWR will generate the JavaScript to allow web browsers to securely call into Java code almost as if it was running locally. It can marshal virtually any data including collections, POJOs, XML and binary data like images and PDF files. All that is required is a security policy that defines what is allowed.

With Reverse Ajax, DWR allows Java code running on a server to use client side APIs to publish updates to arbitrary groups of browsers. This allows interaction 2 ways - browser calling server and server calling browser. DWR supports Comet, Polling and Piggyback (sending data in with normal requests) as ways to publish to browsers.

DWR provides integration with Dojo, TIBCO GI, Scriptaculous in the browser, and with Spring, Struts, Guice, Hibernate and others on the server.

Your question involves multiple technologies. I will try to give you some direction towards all of them in steps.

When you write a Java class, it is deployed on the server side . When you write a JavaScript function, it is deployed on the client side .

The server side Java classes may be deployed as a web application in an application server ( Apache tomcat for example), and exposed over a URL. The interface for doing so is to write what is known as a Servlet . A servlet is nothing but another Java class deployed on the same application server. This servlet class can invoke your Java method. (Note: There are other technologies (JSP or Java Server Pages , for example) which eventually get re-engineered to a servlet.)

There is abundant literature on how servlets work, but in short, once deployed in an application server, the code inside the servlet can be invoked when the application URL is invoked from a browser.

So, here are the steps:

  1. You invoke a URL from your browser for the application deployed on the application server. For example, you invoke http://localhost:8080/myapp/doSomething
  2. Your web application (myapp) has a deployment descriptor (web.xml) with its own URL mapping . By referencing this descriptor, your webapp realizes that for the URL: "/doSomething" it should invoke MyServlet (your servlet deployed on the server)
  3. Your servlet (MyServlet) will now implement some of the standard doXXX methods (for GET, POST, etc. operation).
  4. Within this implementation of the doXXX method, you can invoke your Java class method .
  5. Once your servlet completes execution, it writes a response to the servlet's output stream, which the application server then streams back to your browser over http . Now, within your response, you can choose to write anything you want - this is what will be available to you on the browser when you invoke the URL.

Having said the above steps, from JavaScript, you can invoke a URL and get a response using technologies like AJAX .

If your function does not return a value, but simply "does something", then you really do not care about the response, However, using AJAX, you can also check the status on the response to see if there were any exceptions that occurred while executing your function on the server side.

是的,它是可能的..您可以http请求和响应调用java方法并获得响应..

You can not call Java methods from Javascript.

You can, however, use Javascript to do a HTTP request to a server which will run the Java method for you.

From Real Gagnon post , youc an always call a public method of the main class of an applet from javascript.

But nowadays, this kind of code has been deprecated in favor of REST calls to the Java server backend.

You know, that you have to create a Web-Application for that? You'll need an Application Server (eg Tomcat) for that. With a Web-Application, you can - roughly speaking - put the URL of an Action on the server into the action-Attribute of the form that contains your input box. The Action would be a Java-Class of a Method (depending on the Framework you use).

Have a look at the Java Servlet Specification or one of the countless Java Webapp Frameworks.

I don't know where this Java is running. Is it in the browser, or is it on the server? If the server then you don't call anything directly, you use an asynchronous call of some kind to get a response - JSON, DWR, GWT RPC, Soap, Ad Hoc XML etc.

If the code is running in the browser as an applet then the Java Plugin provides entry points that allows Java to invoke JS and JS to invoke Java. The Java object could invoke a method in JS, passing it's own callback interface as a parameter. Once JS has that interface, two way communication is possible. A similar thing is possible with Flash and Silverlight too.

This document explains how - http://download-llnw.oracle.com/javase/1.4.2/docs/guide/plugin/developer_guide/java_js.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM