I am trying to call a java method in a javascript. java class resides in server side.
The Sample Java Code is:
public class deleteconfig
{
static boolean check = true;
public static void initiate()
{
check = false;
}
}
I would like to call my deleteconfig.initiate() method in my javascript
Any help is greatly appreciated.
Cheers
解决方案
As javascript is a client side script, it cannot invoke java methods directly which resides on the server
Without any particular java frameworks, you could make use of Java Server Pages (JSP) to invoke deleteconfig.initiate() when it receives a GET request from javascript.
Sending Request
You might also want to use JQuery (a javscript plugin - http://jquery.com/) to send an asynchronous GET request to the server like this
//javascript code
function callInititiate(){
//This sends a get request to executeInit.jsp
//
$.get('localhost/myWebbApp/executeInit.jsp');
}
$(callInitiate);
Receive Request
On the server side, you should have executeInit.jsp that calls deleteconfig.initiate() static method
//in executeInit.jsp
// executes initiate() static method
deleteconfig.initiate();
%>
Perhaps reading more about Java Server Pages can get you started!