简体   繁体   English

从Javascript调用Java方法

[英]Call a Java method from Javascript

Need to call a Java method whenever I click an HTML button. 每次单击HTML按钮时都需要调用Java方法。 Basically I need to send over some text to a Java class when I click a button. 基本上我需要在单击按钮时将一些文本发送到Java类。 I have tried applets to achieve this behaviour but didn't manage. 我已经尝试过applet来实现这种行为但是没有管理。

Thanks 谢谢

UPDATE Solution: Java Web services UPDATE解决方案:Java Web服务

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是一个Java库,它使服务器上的Java和浏览器中的JavaScript能够尽可能简单地进行交互和相互调用。

DWR is Easy Ajax for Java DWR是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. DWR将生成JavaScript以允许Web浏览器安全地调用Java代码,就像它在本地运行一样。 It can marshal virtually any data including collections, POJOs, XML and binary data like images and PDF files. 它几乎可以封送任何数据,包括集合,POJO,XML和二进制数据,如图像和PDF文件。 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. 使用Reverse Ajax,DWR允许在服务器上运行的Java代码使用客户端API将更新发布到任意浏览器组。 This allows interaction 2 ways - browser calling server and server calling browser. 这允许交互2种方式 - 浏览器调用服务器和服务器调用浏览器。 DWR supports Comet, Polling and Piggyback (sending data in with normal requests) as ways to publish to browsers. DWR支持Comet,Polling和Piggyback(以正常请求发送数据)作为发布到浏览器的方式。

DWR provides integration with Dojo, TIBCO GI, Scriptaculous in the browser, and with Spring, Struts, Guice, Hibernate and others on the server. DWR在浏览器中提供与Dojo,TIBCO GI,Scriptaculous以及服务器上的Spring,Struts,Guice,Hibernate等的集成。

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 . 编写Java类时,它部署在服务器端 When you write a JavaScript function, it is deployed on the client side . 编写JavaScript函数时,它部署在客户端

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. 服务器端Java类可以部署为应用程序服务器(例如Apache tomcat )中的Web应用程序,并通过URL公开。 The interface for doing so is to write what is known as a Servlet . 这样做的接口是编写所谓的Servlet A servlet is nothing but another Java class deployed on the same application server. servlet只是部署在同一应用程序服务器上的另一个Java类。 This servlet class can invoke your Java method. 这个servlet类可以调用你的Java方法。 (Note: There are other technologies (JSP or Java Server Pages , for example) which eventually get re-engineered to a servlet.) (注意:还有其他技术(例如JSP或Java Server Pages )最终会重新设计为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. 关于servlet如何工作的文献很多 ,但简而言之,一旦部署在应用程序服务器中,当从浏览器调用应用程序URL时,可以调用servlet中的代码。

So, here are the steps: 那么,以下是步骤:

  1. You invoke a URL from your browser for the application deployed on the application server. 您可以从浏览器调用应用程序服务器上部署的应用程序的URL For example, you invoke http://localhost:8080/myapp/doSomething 例如,您调用http://localhost:8080/myapp/doSomething
  2. Your web application (myapp) has a deployment descriptor (web.xml) with its own URL mapping . 您的Web应用程序(myapp)具有部署描述符 (web.xml),其中包含自己的URL映射 By referencing this descriptor, your webapp realizes that for the URL: "/doSomething" it should invoke MyServlet (your servlet deployed on the server) 通过引用此描述符,您的webapp意识到对于URL:“/ doSomething”它应该调用MyServlet(您的servlet部署在服务器上)
  3. Your servlet (MyServlet) will now implement some of the standard doXXX methods (for GET, POST, etc. operation). 您的servlet(MyServlet)现在将实现一些标准的doXXX方法 (用于GET,POST等操作)。
  4. Within this implementation of the doXXX method, you can invoke your Java class method . 在doXXX方法的此实现中,您可以调用Java类方法
  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 . 一旦您的servlet完成执行,它就会将响应写入servlet的输出流,然后应用程序服务器将通过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. 现在,在您的回复中,您可以选择编写任何您想要的内容 - 这是您在调用URL时可以在浏览器上使用的内容。

Having said the above steps, from JavaScript, you can invoke a URL and get a response using technologies like AJAX . 完成上述步骤后,您可以使用JavaScript调用URL并使用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. 如果你的函数没有返回值,只是“做某事”,那么你真的不关心响应,但是,使用AJAX,你也可以检查响应的状态 ,看看是否有任何异常发生时在服务器端执行您的功能。

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

You can not call Java methods from Javascript. 您无法从Javascript调用Java方法。

You can, however, use Javascript to do a HTTP request to a server which will run the Java method for you. 但是,您可以使用Javascript向服务器发出HTTP请求,该服务器将为您运行Java方法。

From Real Gagnon post , youc an always call a public method of the main class of an applet from javascript. Real Gagnon帖子中 ,你总是从javascript中调用applet主类的公共方法。

But nowadays, this kind of code has been deprecated in favor of REST calls to the Java server backend. 但是现在,这种代码已被弃用,转而支持对Java服务器后端的REST调用。

You know, that you have to create a Web-Application for that? 你知道,你必须为此创建一个Web应用程序吗? You'll need an Application Server (eg Tomcat) for that. 你需要一个Application Server(例如Tomcat)。 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. 使用Web应用程序,您可以 - 粗略地说 - 将服务器上的Action的URL放入包含输入框的表单的action-Attribute中。 The Action would be a Java-Class of a Method (depending on the Framework you use). Action将是一个方法的Java类(取决于您使用的框架)。

Have a look at the Java Servlet Specification or one of the countless Java Webapp Frameworks. 查看Java Servlet规范或无数Java Webapp框架之一。

I don't know where this Java is running. 我不知道这个Java在哪里运行。 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. 如果服务器然后你不直接调用任何东西,你使用某种异步调用来获得响应 - JSON,DWR,GWT RPC,Soap,Ad Hoc XML等。

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. 如果代码在浏览器中作为applet运行,那么Java Plugin提供了允许Java调用JS和JS来调用Java的入口点。 The Java object could invoke a method in JS, passing it's own callback interface as a parameter. Java对象可以调用JS中的方法,将其自己的回调接口作为参数传递。 Once JS has that interface, two way communication is possible. 一旦JS具有该接口,就可以进行双向通信。 A similar thing is possible with Flash and Silverlight too. Flash和Silverlight也可以实现类似的功能。

This document explains how - http://download-llnw.oracle.com/javase/1.4.2/docs/guide/plugin/developer_guide/java_js.html 本文档解释了如何 - http://download-llnw.oracle.com/javase/1.4.2/docs/guide/plugin/developer_guide/java_js.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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