Home > Computer Tutorials > Computer Knowledge > How to call JAVA class method in js

How to call JAVA class method in js

王林
Release: 2024-01-15 17:48:11
forward
1538 people have browsed it

How to call JAVA class method in js

How does js call methods of JAVA classes? Specific code

Use ajax method.

First, pass the class name and method name to be called as parameters to a servlet. This step can be accomplished using a framework or directly using the XMLHttpRequest object.

In addition, the class name to be called and the complete package path of the class are best written in the configuration file

Assume here that the class name is Hello, the method name is sayHello, and the sayHello method takes no parameters, and the class path is com.demo.Hello

Configuration fileAjaxConfig.properties

Hello = com.demo.Hello

So the parameters passed in are class=Hello&method=sayHello

Do the following processing in the servlet:

String className=request.getParameter("classname");

String methodName=request.getParameter("method");

String classPath=null;

.

Read the configuration file, take out the value corresponding to className and put it into the classPath variable (there are many ways to do this. How to read the configuration file, you can find information online, there are many, I will not go into details)

.

Class c=Class.forName(classPath); //Load the class you specified

Class param[]=new Class[0]; //The parameters of the method are 0

Method m=null;

String returnValue=null; //Return value

try {

m = c.getMethod("sayHello",param); //Get the specified method in the class you specified

} catch (SecurityException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (NoSuchMethodException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

returnValue=(String)m.invoke(c.newInstance(), new Object[0]); //Call the method you specified

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InvocationTargetException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InstantiationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Finally, return the value of returnValue to the client

Class Hello.java

public class Hello

{

public String sayHello()

{

return "hello";

}

}

If you don’t understand, add 67919823 and we’ll discuss it together

How to use objects in java in javascript

In order to facilitate the interaction between web pages and Android applications, the Android system provides a mechanism for JavaScript web scripts in WebView to call Java class methods. Just call the addJavascriptInterface method to map a Java object to a JavaScript object.

1. Mapping Java objects to JavaScript objects

code show as below:

mWebView = (WebView) findViewById(R.id.wv_content);

mWebView.setVerticalScrollbarOverlay(true);

final WebSettings settings = mWebView.getSettings();

settings.setSupportZoom(true);

//WebView enables Javascript script execution

settings.setJavaScriptEnabled(true);

settings.setJavaScriptCanOpenWindowsAutomatically(true);

//Mapping Java objects to a Javascript object named "js2java"

//JavaScript can call Java object methods through "window.js2java"

mWebView.addJavascriptInterface(new JSInvokeClass(), "js2java");

code show as below:

/**Web page Javascript calling interface**/

class JSInvokeClass {

public void back() {

activity.finish();

}

}

2. Example of JavaScript calling Java object

Call the back method of the above JSInvokeClass class object, as follows:

Copy code The code is as follows:

window.js2java.back();

How to use methods in java classes in JavaScript

It’s hard! One is the front desk and the other is the back desk, but AJAX realizes this idea! . Below is an AJAX example:

// JavaScript Document

var xmlHttp;

function GetXmlHttpObject()

{

var xhr=null;

try

{

// Firefox, Opera 8.0, Safari

xhr=new XMLHttpRequest();

}catch (e)

{

//Internet Explorer

try

{

xhr=new ActiveXObject("Msxml2.XMLHTTP");

}catch (e)

{

xhr=new ActiveXObject("Microsoft.XMLHTTP");

}

}

return xhr;

}

function getDataByDept(str)

{

xmlHttp = GetXmlHttpObject();

if(xmlHttp==null)

{

alert("Sorry! Your browser does not support AJAX!");

return;

}

var url = "/intcard/jsp/employee.do?method=getByDept"

xmlHttp.onReadyStateChange=stateChanged;

xmlHttp.open("post",url,true);

xmlHttp.setRequestHeader('Content-Type', 'application/x--form-urlencoded;charset=UTF-8');

xmlHttp.send("dept=" str);

}

function stateChanged()

{

if(xmlHttp.readyState == 4)

{

var result = xmlHttp.responseText;

document.getElementById("select_employees").innerHTML = result;

}

}

The idea is to call the JS method through the event of the HTML control, and call the server script through the open method of the httprequest object in the JS process ----- In the server script, you can use the javabean method and pass the calculation results to JS.Curve realizes your thoughts

The above is the detailed content of How to call JAVA class method in js. For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template